deno.land / std@0.201.0 / fs / exists.ts

نووسراو ببینە
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
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
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;}
/** * Test whether or not the given path exists by checking with the file system. Please consider to check if the path is readable and either a file or a directory by providing additional `options`: * * ```ts * import { exists } from "https://deno.land/std@$STD_VERSION/fs/mod.ts"; * const isReadableDir = await exists("./foo", { * isReadable: true, * isDirectory: true * }); * const isReadableFile = await exists("./bar", { * isReadable: true, * isFile: true * }); * ``` * * 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. * * Bad: * ```ts * import { exists } from "https://deno.land/std@$STD_VERSION/fs/mod.ts"; * * if (await exists("./foo")) { * await Deno.remove("./foo"); * } * ``` * * Good: * ```ts * // Notice no use of exists * try { * await Deno.remove("./foo", { recursive: true }); * } catch (error) { * if (!(error instanceof Deno.errors.NotFound)) { * throw error; * } * // Do nothing... * } * ``` * @see https://en.wikipedia.org/wiki/Time-of-check_to_time-of-use */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) { if (stat.mode === null) { return true; // Exclusive on Non-POSIX systems } 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? } } 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; }}
/** * Test whether or not the given path exists by checking with the file system. Please consider to check if the path is readable and either a file or a directory by providing additional `options`: * * ```ts * import { existsSync } from "https://deno.land/std@$STD_VERSION/fs/mod.ts"; * const isReadableDir = existsSync("./foo", { * isReadable: true, * isDirectory: true * }); * const isReadableFile = existsSync("./bar", { * isReadable: true, * isFile: true * }); * ``` * * 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. * * Bad: * ```ts * import { existsSync } from "https://deno.land/std@$STD_VERSION/fs/mod.ts"; * * if (existsSync("./foo")) { * Deno.removeSync("./foo"); * } * ``` * * Good: * ```ts * // Notice no use of existsSync * try { * Deno.removeSync("./foo", { recursive: true }); * } catch (error) { * if (!(error instanceof Deno.errors.NotFound)) { * throw error; * } * // Do nothing... * } * ``` * @see https://en.wikipedia.org/wiki/Time-of-check_to_time-of-use */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) { if (stat.mode === null) { return true; // Exclusive on Non-POSIX systems } 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? } } 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; }}
std

Version Info

Tagged at
a year ago