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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314 |
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x23
x23
x23
 
x46
x46
x46
x46
x46
x46
x58
x59
x59
 
x59
x58
x58
x58
x58
x62
x62
x58
x61
x61
x58
x58
x46
x50
x52
x52
x53
x54
x216
x54
x54
 
x54
x54
x54
x54
x54
x46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
x23
x23
x23
 
x79
x79
x79
x79
x79
x79
x146
x147
x147
 
x147
x146
x146
x146
x146
x150
x150
x146
x149
x149
x146
x165
x79
x97
x113
x113
x100
x101
x404
x101
 
x101
x101
x101
x101
x101
x79
 
x29
 
 
 
x29
 
 
 
 
x29 |
I
I
|
// Copyright 2018-2025 the Deno authors. MIT license.
/** Options for {@linkcode exists} and {@linkcode existsSync.} */
export interface ExistsOptions {
/**
* When `true`, will check if the path is readable by the user as well.
*
* @default {false}
*/
isReadable?: boolean;
/**
* When `true`, will check if the path is a directory as well. Directory
* symlinks are included.
*
* @default {false}
*/
isDirectory?: boolean;
/**
* When `true`, will check if the path is a file as well. File symlinks are
* included.
*
* @default {false}
*/
isFile?: boolean;
}
/**
* Asynchronously test whether or not the given path exists by checking with
* the file system.
*
* Note: Do not use this function if performing a check before another operation
* on that file. Doing so creates a race condition. Instead, perform the actual
* file operation directly. This function is not recommended for this use case.
* See the recommended method below.
*
* @see {@link https://en.wikipedia.org/wiki/Time-of-check_to_time-of-use} for
* more information on the time-of-check to time-of-use bug.
*
* Requires `--allow-read` permissions, and in some cases, `--allow-sys`
* permissions if `options.isReadable` is `true`.
*
* @see {@link https://docs.deno.com/runtime/manual/basics/permissions#file-system-access}
* for more information on Deno's permissions system.
*
* @param path The path to the file or directory, as a string or URL.
* @param options Additional options for the check.
*
* @returns A promise that resolves with `true` if the path exists, `false`
* otherwise.
*
* @example Recommended method
* ```ts ignore
* // Notice no use of exists
* try {
* await Deno.remove("./foo", { recursive: true });
* } catch (error) {
* if (!(error instanceof Deno.errors.NotFound)) {
* throw error;
* }
* // Do nothing...
* }
* ```
*
* Notice that `exists()` is not used in the above example. Doing so avoids a
* possible race condition. See the above note for details.
*
* @example Basic usage
* ```ts ignore
* import { exists } from "@std/fs/exists";
*
* await exists("./exists"); // true
* await exists("./does_not_exist"); // false
* ```
*
* @example Check if a path is readable
*
* Requires `--allow-sys` permissions in some cases.
*
* ```ts ignore
* import { exists } from "@std/fs/exists";
*
* await exists("./readable", { isReadable: true }); // true
* await exists("./not_readable", { isReadable: true }); // false
* ```
*
* @example Check if a path is a directory
* ```ts ignore
* import { exists } from "@std/fs/exists";
*
* await exists("./directory", { isDirectory: true }); // true
* await exists("./file", { isDirectory: true }); // false
* ```
*
* @example Check if a path is a file
* ```ts ignore
* import { exists } from "@std/fs/exists";
*
* await exists("./file", { isFile: true }); // true
* await exists("./directory", { isFile: true }); // false
* ```
*
* @example Check if a path is a readable directory
*
* Requires `--allow-sys` permissions in some cases.
*
* ```ts ignore
* import { exists } from "@std/fs/exists";
*
* await exists("./readable_directory", { isReadable: true, isDirectory: true }); // true
* await exists("./not_readable_directory", { isReadable: true, isDirectory: true }); // false
* ```
*
* @example Check if a path is a readable file
*
* Requires `--allow-sys` permissions in some cases.
*
* ```ts ignore
* import { exists } from "@std/fs/exists";
*
* await exists("./readable_file", { isReadable: true, isFile: true }); // true
* await exists("./not_readable_file", { isReadable: true, isFile: true }); // false
* ```
*/
export async function exists(
path: string | URL,
options?: ExistsOptions,
): Promise<boolean> {
try {
const stat = await Deno.stat(path);
if (
options &&
(options.isReadable || options.isDirectory || options.isFile)
) {
if (options.isDirectory && options.isFile) {
throw new TypeError(
"ExistsOptions.options.isDirectory and ExistsOptions.options.isFile must not be true together",
);
}
if (
(options.isDirectory && !stat.isDirectory) ||
(options.isFile && !stat.isFile)
) {
return false;
}
if (options.isReadable) {
return fileIsReadable(stat);
}
}
return true;
} catch (error) {
if (error instanceof Deno.errors.NotFound) {
return false;
}
if (error instanceof Deno.errors.PermissionDenied) {
if (
(await Deno.permissions.query({ name: "read", path })).state ===
"granted"
) {
// --allow-read not missing
return !options?.isReadable; // PermissionDenied was raised by file system, so the item exists, but can't be read
}
}
throw error;
}
}
/**
* Synchronously test whether or not the given path exists by checking with
* the file system.
*
* Note: Do not use this function if performing a check before another operation
* on that file. Doing so creates a race condition. Instead, perform the actual
* file operation directly. This function is not recommended for this use case.
* See the recommended method below.
*
* @see {@link https://en.wikipedia.org/wiki/Time-of-check_to_time-of-use} for
* more information on the time-of-check to time-of-use bug.
*
* Requires `--allow-read` permissions, and in some cases, `--allow-sys`
* permissions if `options.isReadable` is `true`.
*
* @see {@link https://docs.deno.com/runtime/manual/basics/permissions#file-system-access}
* for more information on Deno's permissions system.
*
* @param path The path to the file or directory, as a string or URL.
* @param options Additional options for the check.
*
* @returns `true` if the path exists, `false` otherwise.
*
* @example Recommended method
* ```ts ignore
* // Notice no use of exists
* try {
* Deno.removeSync("./foo", { recursive: true });
* } catch (error) {
* if (!(error instanceof Deno.errors.NotFound)) {
* throw error;
* }
* // Do nothing...
* }
* ```
*
* Notice that `existsSync()` is not used in the above example. Doing so avoids
* a possible race condition. See the above note for details.
*
* @example Basic usage
* ```ts ignore
* import { existsSync } from "@std/fs/exists";
*
* existsSync("./exists"); // true
* existsSync("./does_not_exist"); // false
* ```
*
* @example Check if a path is readable
*
* Requires `--allow-sys` permissions in some cases.
*
* ```ts ignore
* import { existsSync } from "@std/fs/exists";
*
* existsSync("./readable", { isReadable: true }); // true
* existsSync("./not_readable", { isReadable: true }); // false
* ```
*
* @example Check if a path is a directory
* ```ts ignore
* import { existsSync } from "@std/fs/exists";
*
* existsSync("./directory", { isDirectory: true }); // true
* existsSync("./file", { isDirectory: true }); // false
* ```
*
* @example Check if a path is a file
* ```ts ignore
* import { existsSync } from "@std/fs/exists";
*
* existsSync("./file", { isFile: true }); // true
* existsSync("./directory", { isFile: true }); // false
* ```
*
* @example Check if a path is a readable directory
*
* Requires `--allow-sys` permissions in some cases.
*
* ```ts ignore
* import { existsSync } from "@std/fs/exists";
*
* existsSync("./readable_directory", { isReadable: true, isDirectory: true }); // true
* existsSync("./not_readable_directory", { isReadable: true, isDirectory: true }); // false
* ```
*
* @example Check if a path is a readable file
*
* Requires `--allow-sys` permissions in some cases.
*
* ```ts ignore
* import { existsSync } from "@std/fs/exists";
*
* existsSync("./readable_file", { isReadable: true, isFile: true }); // true
* existsSync("./not_readable_file", { isReadable: true, isFile: true }); // false
* ```
*/
export function existsSync(
path: string | URL,
options?: ExistsOptions,
): boolean {
try {
const stat = Deno.statSync(path);
if (
options &&
(options.isReadable || options.isDirectory || options.isFile)
) {
if (options.isDirectory && options.isFile) {
throw new TypeError(
"ExistsOptions.options.isDirectory and ExistsOptions.options.isFile must not be true together",
);
}
if (
(options.isDirectory && !stat.isDirectory) ||
(options.isFile && !stat.isFile)
) {
return false;
}
if (options.isReadable) {
return fileIsReadable(stat);
}
}
return true;
} catch (error) {
if (error instanceof Deno.errors.NotFound) {
return false;
}
if (error instanceof Deno.errors.PermissionDenied) {
if (
Deno.permissions.querySync({ name: "read", path }).state === "granted"
) {
// --allow-read not missing
return !options?.isReadable; // PermissionDenied was raised by file system, so the item exists, but can't be read
}
}
throw error;
}
}
function fileIsReadable(stat: Deno.FileInfo) {
if (stat.mode === null) {
return true; // Exclusive on Non-POSIX systems
} else if (Deno.uid() === stat.uid) {
return (stat.mode & 0o400) === 0o400; // User is owner and can read?
} else if (Deno.gid() === stat.gid) {
return (stat.mode & 0o040) === 0o040; // User group is owner and can read?
}
return (stat.mode & 0o004) === 0o004; // Others can read?
}
|