-
Notifications
You must be signed in to change notification settings - Fork 31.1k
/
Copy pathsync.js
106 lines (89 loc) Β· 2.2 KB
/
sync.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
'use strict';
const pathModule = require('path');
const {
getValidatedPath,
stringToFlags,
getValidMode,
getStatsFromBinding,
getStatFsFromBinding,
getValidatedFd,
} = require('internal/fs/utils');
const { parseFileMode, isInt32 } = require('internal/validators');
const binding = internalBinding('fs');
/**
* @param {string} path
* @param {number} flag
* @return {string}
*/
function readFileUtf8(path, flag) {
if (!isInt32(path)) {
path = pathModule.toNamespacedPath(getValidatedPath(path));
}
return binding.readFileUtf8(path, stringToFlags(flag));
}
function exists(path) {
try {
path = getValidatedPath(path);
} catch {
return false;
}
return binding.existsSync(pathModule.toNamespacedPath(path));
}
function access(path, mode) {
path = getValidatedPath(path);
mode = getValidMode(mode, 'access');
binding.accessSync(pathModule.toNamespacedPath(path), mode);
}
function copyFile(src, dest, mode) {
src = getValidatedPath(src, 'src');
dest = getValidatedPath(dest, 'dest');
binding.copyFileSync(
pathModule.toNamespacedPath(src),
pathModule.toNamespacedPath(dest),
getValidMode(mode, 'copyFile'),
);
}
function stat(path, options = { bigint: false, throwIfNoEntry: true }) {
path = getValidatedPath(path);
const stats = binding.statSync(
pathModule.toNamespacedPath(path),
options.bigint,
options.throwIfNoEntry,
);
if (stats === undefined) {
return undefined;
}
return getStatsFromBinding(stats);
}
function statfs(path, options = { bigint: false }) {
path = getValidatedPath(path);
const stats = binding.statfsSync(pathModule.toNamespacedPath(path), options.bigint);
return getStatFsFromBinding(stats);
}
function open(path, flags, mode) {
path = getValidatedPath(path);
return binding.openSync(
pathModule.toNamespacedPath(path),
stringToFlags(flags),
parseFileMode(mode, 'mode', 0o666),
);
}
function close(fd) {
fd = getValidatedFd(fd);
return binding.closeSync(fd);
}
function unlink(path) {
path = pathModule.toNamespacedPath(getValidatedPath(path));
return binding.unlinkSync(path);
}
module.exports = {
readFileUtf8,
exists,
access,
copyFile,
stat,
statfs,
open,
close,
unlink,
};