OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "platform/globals.h" | 5 #include "platform/globals.h" |
6 #if defined(HOST_OS_LINUX) | 6 #if defined(HOST_OS_LINUX) |
7 | 7 |
8 #include "bin/directory.h" | 8 #include "bin/directory.h" |
9 | 9 |
10 #include <dirent.h> // NOLINT | 10 #include <dirent.h> // NOLINT |
11 #include <errno.h> // NOLINT | 11 #include <errno.h> // NOLINT |
| 12 #include <fcntl.h> // NOLINT |
12 #include <stdlib.h> // NOLINT | 13 #include <stdlib.h> // NOLINT |
13 #include <string.h> // NOLINT | 14 #include <string.h> // NOLINT |
14 #include <sys/param.h> // NOLINT | 15 #include <sys/param.h> // NOLINT |
15 #include <sys/stat.h> // NOLINT | 16 #include <sys/stat.h> // NOLINT |
16 #include <unistd.h> // NOLINT | 17 #include <unistd.h> // NOLINT |
17 | 18 |
| 19 #include "bin/crypto.h" |
18 #include "bin/dartutils.h" | 20 #include "bin/dartutils.h" |
| 21 #include "bin/fdutils.h" |
19 #include "bin/file.h" | 22 #include "bin/file.h" |
| 23 #include "bin/namespace.h" |
20 #include "bin/platform.h" | 24 #include "bin/platform.h" |
21 #include "platform/signal_blocker.h" | 25 #include "platform/signal_blocker.h" |
22 | 26 |
23 namespace dart { | 27 namespace dart { |
24 namespace bin { | 28 namespace bin { |
25 | 29 |
26 PathBuffer::PathBuffer() : length_(0) { | 30 PathBuffer::PathBuffer() : length_(0) { |
27 data_ = calloc(PATH_MAX + 1, sizeof(char)); // NOLINT | 31 data_ = calloc(PATH_MAX + 1, sizeof(char)); // NOLINT |
28 } | 32 } |
29 | 33 |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 dev_t dev; | 78 dev_t dev; |
75 ino64_t ino; | 79 ino64_t ino; |
76 LinkList* next; | 80 LinkList* next; |
77 }; | 81 }; |
78 | 82 |
79 ListType DirectoryListingEntry::Next(DirectoryListing* listing) { | 83 ListType DirectoryListingEntry::Next(DirectoryListing* listing) { |
80 if (done_) { | 84 if (done_) { |
81 return kListDone; | 85 return kListDone; |
82 } | 86 } |
83 | 87 |
| 88 if (fd_ == -1) { |
| 89 ASSERT(lister_ == 0); |
| 90 NamespaceScope ns(listing->namespc(), listing->path_buffer().AsString()); |
| 91 const int listingfd = |
| 92 TEMP_FAILURE_RETRY(openat64(ns.fd(), ns.path(), O_DIRECTORY)); |
| 93 if (listingfd < 0) { |
| 94 done_ = true; |
| 95 return kListError; |
| 96 } |
| 97 fd_ = listingfd; |
| 98 } |
| 99 |
84 if (lister_ == 0) { | 100 if (lister_ == 0) { |
85 do { | 101 do { |
86 lister_ = reinterpret_cast<intptr_t>( | 102 lister_ = reinterpret_cast<intptr_t>(fdopendir(fd_)); |
87 opendir(listing->path_buffer().AsString())); | |
88 } while ((lister_ == 0) && (errno == EINTR)); | 103 } while ((lister_ == 0) && (errno == EINTR)); |
89 | |
90 if (lister_ == 0) { | 104 if (lister_ == 0) { |
91 done_ = true; | 105 done_ = true; |
92 return kListError; | 106 return kListError; |
93 } | 107 } |
94 if (parent_ != NULL) { | 108 if (parent_ != NULL) { |
95 if (!listing->path_buffer().Add(File::PathSeparator())) { | 109 if (!listing->path_buffer().Add(File::PathSeparator())) { |
96 return kListError; | 110 return kListError; |
97 } | 111 } |
98 } | 112 } |
99 path_length_ = listing->path_buffer().length(); | 113 path_length_ = listing->path_buffer().length(); |
(...skipping 28 matching lines...) Expand all Loading... |
128 if (!listing->follow_links()) { | 142 if (!listing->follow_links()) { |
129 return kListLink; | 143 return kListLink; |
130 } | 144 } |
131 // Else fall through to next case. | 145 // Else fall through to next case. |
132 // Fall through. | 146 // Fall through. |
133 case DT_UNKNOWN: { | 147 case DT_UNKNOWN: { |
134 // On some file systems the entry type is not determined by | 148 // On some file systems the entry type is not determined by |
135 // readdir. For those and for links we use stat to determine | 149 // readdir. For those and for links we use stat to determine |
136 // the actual entry type. Notice that stat returns the type of | 150 // the actual entry type. Notice that stat returns the type of |
137 // the file pointed to. | 151 // the file pointed to. |
| 152 NamespaceScope ns(listing->namespc(), |
| 153 listing->path_buffer().AsString()); |
138 struct stat64 entry_info; | 154 struct stat64 entry_info; |
139 int stat_success; | 155 int stat_success; |
140 stat_success = TEMP_FAILURE_RETRY( | 156 stat_success = TEMP_FAILURE_RETRY( |
141 lstat64(listing->path_buffer().AsString(), &entry_info)); | 157 fstatat64(ns.fd(), ns.path(), &entry_info, AT_SYMLINK_NOFOLLOW)); |
142 if (stat_success == -1) { | 158 if (stat_success == -1) { |
143 return kListError; | 159 return kListError; |
144 } | 160 } |
145 if (listing->follow_links() && S_ISLNK(entry_info.st_mode)) { | 161 if (listing->follow_links() && S_ISLNK(entry_info.st_mode)) { |
146 // Check to see if we are in a loop created by a symbolic link. | 162 // Check to see if we are in a loop created by a symbolic link. |
147 LinkList current_link = {entry_info.st_dev, entry_info.st_ino, link_}; | 163 LinkList current_link = {entry_info.st_dev, entry_info.st_ino, link_}; |
148 LinkList* previous = link_; | 164 LinkList* previous = link_; |
149 while (previous != NULL) { | 165 while (previous != NULL) { |
150 if ((previous->dev == current_link.dev) && | 166 if ((previous->dev == current_link.dev) && |
151 (previous->ino == current_link.ino)) { | 167 (previous->ino == current_link.ino)) { |
152 // Report the looping link as a link, rather than following it. | 168 // Report the looping link as a link, rather than following it. |
153 return kListLink; | 169 return kListLink; |
154 } | 170 } |
155 previous = previous->next; | 171 previous = previous->next; |
156 } | 172 } |
157 stat_success = TEMP_FAILURE_RETRY( | 173 stat_success = |
158 stat64(listing->path_buffer().AsString(), &entry_info)); | 174 TEMP_FAILURE_RETRY(fstatat64(ns.fd(), ns.path(), &entry_info, 0)); |
159 if (stat_success == -1) { | 175 if (stat_success == -1) { |
160 // Report a broken link as a link, even if follow_links is true. | 176 // Report a broken link as a link, even if follow_links is true. |
161 return kListLink; | 177 return kListLink; |
162 } | 178 } |
163 if (S_ISDIR(entry_info.st_mode)) { | 179 if (S_ISDIR(entry_info.st_mode)) { |
164 // Recurse into the subdirectory with current_link added to the | 180 // Recurse into the subdirectory with current_link added to the |
165 // linked list of seen file system links. | 181 // linked list of seen file system links. |
166 link_ = new LinkList(current_link); | 182 link_ = new LinkList(current_link); |
167 if ((strcmp(entry->d_name, ".") == 0) || | 183 if ((strcmp(entry->d_name, ".") == 0) || |
168 (strcmp(entry->d_name, "..") == 0)) { | 184 (strcmp(entry->d_name, "..") == 0)) { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 } | 219 } |
204 | 220 |
205 return kListDone; | 221 return kListDone; |
206 } | 222 } |
207 | 223 |
208 DirectoryListingEntry::~DirectoryListingEntry() { | 224 DirectoryListingEntry::~DirectoryListingEntry() { |
209 ResetLink(); | 225 ResetLink(); |
210 if (lister_ != 0) { | 226 if (lister_ != 0) { |
211 VOID_NO_RETRY_EXPECTED(closedir(reinterpret_cast<DIR*>(lister_))); | 227 VOID_NO_RETRY_EXPECTED(closedir(reinterpret_cast<DIR*>(lister_))); |
212 } | 228 } |
| 229 if (fd_ != -1) { |
| 230 FDUtils::SaveErrorAndClose(fd_); |
| 231 } |
213 } | 232 } |
214 | 233 |
215 void DirectoryListingEntry::ResetLink() { | 234 void DirectoryListingEntry::ResetLink() { |
216 if ((link_ != NULL) && ((parent_ == NULL) || (parent_->link_ != link_))) { | 235 if ((link_ != NULL) && ((parent_ == NULL) || (parent_->link_ != link_))) { |
217 delete link_; | 236 delete link_; |
218 link_ = NULL; | 237 link_ = NULL; |
219 } | 238 } |
220 if (parent_ != NULL) { | 239 if (parent_ != NULL) { |
221 link_ = parent_->link_; | 240 link_ = parent_->link_; |
222 } | 241 } |
223 } | 242 } |
224 | 243 |
225 static bool DeleteRecursively(PathBuffer* path); | 244 static bool DeleteRecursively(int dirfd, PathBuffer* path); |
226 | 245 |
227 static bool DeleteFile(char* file_name, PathBuffer* path) { | 246 static bool DeleteFile(int dirfd, char* file_name, PathBuffer* path) { |
228 return path->Add(file_name) && | 247 return path->Add(file_name) && |
229 (NO_RETRY_EXPECTED(unlink(path->AsString())) == 0); | 248 (NO_RETRY_EXPECTED(unlinkat(dirfd, path->AsString(), 0)) == 0); |
230 } | 249 } |
231 | 250 |
232 static bool DeleteDir(char* dir_name, PathBuffer* path) { | 251 static bool DeleteDir(int dirfd, char* dir_name, PathBuffer* path) { |
233 if ((strcmp(dir_name, ".") == 0) || (strcmp(dir_name, "..") == 0)) { | 252 if ((strcmp(dir_name, ".") == 0) || (strcmp(dir_name, "..") == 0)) { |
234 return true; | 253 return true; |
235 } | 254 } |
236 return path->Add(dir_name) && DeleteRecursively(path); | 255 return path->Add(dir_name) && DeleteRecursively(dirfd, path); |
237 } | 256 } |
238 | 257 |
239 static bool DeleteRecursively(PathBuffer* path) { | 258 static bool DeleteRecursively(int dirfd, PathBuffer* path) { |
240 // Do not recurse into links for deletion. Instead delete the link. | 259 // Do not recurse into links for deletion. Instead delete the link. |
241 // If it's a file, delete it. | 260 // If it's a file, delete it. |
242 struct stat64 st; | 261 struct stat64 st; |
243 if (TEMP_FAILURE_RETRY(lstat64(path->AsString(), &st)) == -1) { | 262 if (TEMP_FAILURE_RETRY( |
| 263 fstatat64(dirfd, path->AsString(), &st, AT_SYMLINK_NOFOLLOW)) == -1) { |
244 return false; | 264 return false; |
245 } else if (!S_ISDIR(st.st_mode)) { | 265 } else if (!S_ISDIR(st.st_mode)) { |
246 return (NO_RETRY_EXPECTED(unlink(path->AsString())) == 0); | 266 return (NO_RETRY_EXPECTED(unlinkat(dirfd, path->AsString(), 0)) == 0); |
247 } | 267 } |
248 | 268 |
249 if (!path->Add(File::PathSeparator())) { | 269 if (!path->Add(File::PathSeparator())) { |
250 return false; | 270 return false; |
251 } | 271 } |
252 | 272 |
253 // Not a link. Attempt to open as a directory and recurse into the | 273 // Not a link. Attempt to open as a directory and recurse into the |
254 // directory. | 274 // directory. |
| 275 const int fd = |
| 276 TEMP_FAILURE_RETRY(openat64(dirfd, path->AsString(), O_DIRECTORY)); |
| 277 if (fd < 0) { |
| 278 return false; |
| 279 } |
255 DIR* dir_pointer; | 280 DIR* dir_pointer; |
256 do { | 281 do { |
257 dir_pointer = opendir(path->AsString()); | 282 dir_pointer = fdopendir(fd); |
258 } while ((dir_pointer == NULL) && (errno == EINTR)); | 283 } while ((dir_pointer == NULL) && (errno == EINTR)); |
259 if (dir_pointer == NULL) { | 284 if (dir_pointer == NULL) { |
| 285 FDUtils::SaveErrorAndClose(fd); |
260 return false; | 286 return false; |
261 } | 287 } |
262 | 288 |
263 // Iterate the directory and delete all files and directories. | 289 // Iterate the directory and delete all files and directories. |
264 int path_length = path->length(); | 290 int path_length = path->length(); |
265 while (true) { | 291 while (true) { |
266 // In case `readdir()` returns `NULL` we distinguish between end-of-stream | 292 // In case `readdir()` returns `NULL` we distinguish between end-of-stream |
267 // and error by looking if `errno` was updated. | 293 // and error by looking if `errno` was updated. |
268 errno = 0; | 294 errno = 0; |
269 // In glibc 2.24+, readdir_r is deprecated. | 295 // In glibc 2.24+, readdir_r is deprecated. |
270 // According to the man page for readdir: | 296 // According to the man page for readdir: |
271 // "readdir(3) is not required to be thread-safe. However, in modern | 297 // "readdir(3) is not required to be thread-safe. However, in modern |
272 // implementations (including the glibc implementation), concurrent calls to | 298 // implementations (including the glibc implementation), concurrent calls to |
273 // readdir(3) that specify different directory streams are thread-safe." | 299 // readdir(3) that specify different directory streams are thread-safe." |
274 dirent* entry = readdir(dir_pointer); | 300 dirent* entry = readdir(dir_pointer); |
275 if (entry == NULL) { | 301 if (entry == NULL) { |
276 // Failed to read next directory entry. | 302 // Failed to read next directory entry. |
277 if (errno != 0) { | 303 if (errno != 0) { |
278 break; | 304 break; |
279 } | 305 } |
280 // End of directory. | 306 // End of directory. |
281 return (NO_RETRY_EXPECTED(closedir(dir_pointer)) == 0) && | 307 int status = NO_RETRY_EXPECTED(closedir(dir_pointer)); |
282 (NO_RETRY_EXPECTED(remove(path->AsString())) == 0); | 308 FDUtils::SaveErrorAndClose(fd); |
| 309 if (status != 0) { |
| 310 return false; |
| 311 } |
| 312 status = |
| 313 NO_RETRY_EXPECTED(unlinkat(dirfd, path->AsString(), AT_REMOVEDIR)); |
| 314 return status == 0; |
283 } | 315 } |
284 bool ok = false; | 316 bool ok = false; |
285 switch (entry->d_type) { | 317 switch (entry->d_type) { |
286 case DT_DIR: | 318 case DT_DIR: |
287 ok = DeleteDir(entry->d_name, path); | 319 ok = DeleteDir(dirfd, entry->d_name, path); |
288 break; | 320 break; |
289 case DT_BLK: | 321 case DT_BLK: |
290 case DT_CHR: | 322 case DT_CHR: |
291 case DT_FIFO: | 323 case DT_FIFO: |
292 case DT_SOCK: | 324 case DT_SOCK: |
293 case DT_REG: | 325 case DT_REG: |
294 case DT_LNK: | 326 case DT_LNK: |
295 // Treat all links as files. This will delete the link which | 327 // Treat all links as files. This will delete the link which |
296 // is what we want no matter if the link target is a file or a | 328 // is what we want no matter if the link target is a file or a |
297 // directory. | 329 // directory. |
298 ok = DeleteFile(entry->d_name, path); | 330 ok = DeleteFile(dirfd, entry->d_name, path); |
299 break; | 331 break; |
300 case DT_UNKNOWN: { | 332 case DT_UNKNOWN: { |
301 if (!path->Add(entry->d_name)) { | 333 if (!path->Add(entry->d_name)) { |
302 break; | 334 break; |
303 } | 335 } |
304 // On some file systems the entry type is not determined by | 336 // On some file systems the entry type is not determined by |
305 // readdir. For those we use lstat to determine the entry | 337 // readdir. For those we use lstat to determine the entry |
306 // type. | 338 // type. |
307 struct stat64 entry_info; | 339 struct stat64 entry_info; |
308 if (TEMP_FAILURE_RETRY(lstat64(path->AsString(), &entry_info)) == -1) { | 340 if (TEMP_FAILURE_RETRY(fstatat64(dirfd, path->AsString(), &entry_info, |
| 341 AT_SYMLINK_NOFOLLOW)) == -1) { |
309 break; | 342 break; |
310 } | 343 } |
311 path->Reset(path_length); | 344 path->Reset(path_length); |
312 if (S_ISDIR(entry_info.st_mode)) { | 345 if (S_ISDIR(entry_info.st_mode)) { |
313 ok = DeleteDir(entry->d_name, path); | 346 ok = DeleteDir(dirfd, entry->d_name, path); |
314 } else { | 347 } else { |
315 // Treat links as files. This will delete the link which is | 348 // Treat links as files. This will delete the link which is |
316 // what we want no matter if the link target is a file or a | 349 // what we want no matter if the link target is a file or a |
317 // directory. | 350 // directory. |
318 ok = DeleteFile(entry->d_name, path); | 351 ok = DeleteFile(dirfd, entry->d_name, path); |
319 } | 352 } |
320 break; | 353 break; |
321 } | 354 } |
322 default: | 355 default: |
323 // We should have covered all the bases. If not, let's get an error. | 356 // We should have covered all the bases. If not, let's get an error. |
324 FATAL1("Unexpected d_type: %d\n", entry->d_type); | 357 FATAL1("Unexpected d_type: %d\n", entry->d_type); |
325 break; | 358 break; |
326 } | 359 } |
327 if (!ok) { | 360 if (!ok) { |
328 break; | 361 break; |
329 } | 362 } |
330 path->Reset(path_length); | 363 path->Reset(path_length); |
331 } | 364 } |
332 // Only happens if an error. | 365 // Only happens if an error. |
333 ASSERT(errno != 0); | 366 ASSERT(errno != 0); |
334 int err = errno; | 367 int err = errno; |
335 VOID_NO_RETRY_EXPECTED(closedir(dir_pointer)); | 368 VOID_NO_RETRY_EXPECTED(closedir(dir_pointer)); |
| 369 FDUtils::SaveErrorAndClose(fd); |
336 errno = err; | 370 errno = err; |
337 return false; | 371 return false; |
338 } | 372 } |
339 | 373 |
340 Directory::ExistsResult Directory::Exists(const char* dir_name) { | 374 Directory::ExistsResult Directory::Exists(Namespace* namespc, |
| 375 const char* dir_name) { |
| 376 NamespaceScope ns(namespc, dir_name); |
341 struct stat64 entry_info; | 377 struct stat64 entry_info; |
342 int success = TEMP_FAILURE_RETRY(stat64(dir_name, &entry_info)); | 378 int success = |
| 379 TEMP_FAILURE_RETRY(fstatat64(ns.fd(), ns.path(), &entry_info, 0)); |
343 if (success == 0) { | 380 if (success == 0) { |
344 if (S_ISDIR(entry_info.st_mode)) { | 381 if (S_ISDIR(entry_info.st_mode)) { |
345 return EXISTS; | 382 return EXISTS; |
346 } else { | 383 } else { |
347 // An OSError may be constructed based on the return value of this | 384 // An OSError may be constructed based on the return value of this |
348 // function, so set errno to something that makes sense. | 385 // function, so set errno to something that makes sense. |
349 errno = ENOTDIR; | 386 errno = ENOTDIR; |
350 return DOES_NOT_EXIST; | 387 return DOES_NOT_EXIST; |
351 } | 388 } |
352 } else { | 389 } else { |
353 if ((errno == EACCES) || (errno == EBADF) || (errno == EFAULT) || | 390 if ((errno == EACCES) || (errno == EBADF) || (errno == EFAULT) || |
354 (errno == ENOMEM) || (errno == EOVERFLOW)) { | 391 (errno == ENOMEM) || (errno == EOVERFLOW)) { |
355 // Search permissions denied for one of the directories in the | 392 // Search permissions denied for one of the directories in the |
356 // path or a low level error occured. We do not know if the | 393 // path or a low level error occured. We do not know if the |
357 // directory exists. | 394 // directory exists. |
358 return UNKNOWN; | 395 return UNKNOWN; |
359 } | 396 } |
360 ASSERT((errno == ELOOP) || (errno == ENAMETOOLONG) || (errno == ENOENT) || | 397 ASSERT((errno == ELOOP) || (errno == ENAMETOOLONG) || (errno == ENOENT) || |
361 (errno == ENOTDIR)); | 398 (errno == ENOTDIR)); |
362 return DOES_NOT_EXIST; | 399 return DOES_NOT_EXIST; |
363 } | 400 } |
364 } | 401 } |
365 | 402 |
366 char* Directory::CurrentNoScope() { | 403 char* Directory::CurrentNoScope() { |
367 return getcwd(NULL, 0); | 404 return getcwd(NULL, 0); |
368 } | 405 } |
369 | 406 |
370 const char* Directory::Current() { | 407 bool Directory::Create(Namespace* namespc, const char* dir_name) { |
371 char buffer[PATH_MAX]; | 408 NamespaceScope ns(namespc, dir_name); |
372 if (getcwd(buffer, PATH_MAX) == NULL) { | |
373 return NULL; | |
374 } | |
375 return DartUtils::ScopedCopyCString(buffer); | |
376 } | |
377 | |
378 bool Directory::SetCurrent(const char* path) { | |
379 return (NO_RETRY_EXPECTED(chdir(path)) == 0); | |
380 } | |
381 | |
382 bool Directory::Create(const char* dir_name) { | |
383 // Create the directory with the permissions specified by the | 409 // Create the directory with the permissions specified by the |
384 // process umask. | 410 // process umask. |
385 int result = NO_RETRY_EXPECTED(mkdir(dir_name, 0777)); | 411 const int result = NO_RETRY_EXPECTED(mkdirat(ns.fd(), ns.path(), 0777)); |
386 // If the directory already exists, treat it as a success. | 412 // If the directory already exists, treat it as a success. |
387 if ((result == -1) && (errno == EEXIST)) { | 413 if ((result == -1) && (errno == EEXIST)) { |
388 return (Exists(dir_name) == EXISTS); | 414 return (Exists(namespc, dir_name) == EXISTS); |
389 } | 415 } |
390 return (result == 0); | 416 return (result == 0); |
391 } | 417 } |
392 | 418 |
393 const char* Directory::SystemTemp() { | 419 const char* Directory::SystemTemp(Namespace* namespc) { |
394 PathBuffer path; | 420 PathBuffer path; |
395 const char* temp_dir = getenv("TMPDIR"); | 421 const char* temp_dir = getenv("TMPDIR"); |
396 if (temp_dir == NULL) { | 422 if (temp_dir == NULL) { |
397 temp_dir = getenv("TMP"); | 423 temp_dir = getenv("TMP"); |
398 } | 424 } |
399 if (temp_dir == NULL) { | 425 if (temp_dir == NULL) { |
400 temp_dir = "/tmp"; | 426 temp_dir = "/tmp"; |
401 } | 427 } |
402 if (!path.Add(temp_dir)) { | 428 NamespaceScope ns(namespc, temp_dir); |
| 429 if (!path.Add(ns.path())) { |
403 return NULL; | 430 return NULL; |
404 } | 431 } |
405 | 432 |
406 // Remove any trailing slash. | 433 // Remove any trailing slash. |
407 char* result = path.AsString(); | 434 char* result = path.AsString(); |
408 int length = strlen(result); | 435 int length = strlen(result); |
409 if ((length > 1) && (result[length - 1] == '/')) { | 436 if ((length > 1) && (result[length - 1] == '/')) { |
410 result[length - 1] = '\0'; | 437 result[length - 1] = '\0'; |
411 } | 438 } |
412 return path.AsScopedString(); | 439 return path.AsScopedString(); |
413 } | 440 } |
414 | 441 |
415 const char* Directory::CreateTemp(const char* prefix) { | 442 // Returns a new, unused directory name, adding characters to the end |
416 // Returns a new, unused directory name, adding characters to the end | 443 // of prefix. Creates the directory with the permissions specified |
417 // of prefix. Creates the directory with the permissions specified | 444 // by the process umask. |
418 // by the process umask. | 445 // The return value is Dart_ScopeAllocated. |
419 // The return value is Dart_ScopeAllocated. | 446 const char* Directory::CreateTemp(Namespace* namespc, const char* prefix) { |
420 PathBuffer path; | 447 PathBuffer path; |
| 448 const int firstchar = 'A'; |
| 449 const int numchars = 'Z' - 'A' + 1; |
| 450 uint8_t random_bytes[7]; |
| 451 |
| 452 // mkdtemp doesn't have an "at" variant, so we have to simulate it. |
421 if (!path.Add(prefix)) { | 453 if (!path.Add(prefix)) { |
422 return NULL; | 454 return NULL; |
423 } | 455 } |
424 if (!path.Add("XXXXXX")) { | 456 intptr_t prefix_length = path.length(); |
425 // Pattern has overflowed. | 457 while (true) { |
426 return NULL; | 458 Crypto::GetRandomBytes(6, random_bytes); |
427 } | 459 for (intptr_t i = 0; i < 6; i++) { |
428 char* result; | 460 random_bytes[i] = (random_bytes[i] % numchars) + firstchar; |
429 do { | |
430 result = mkdtemp(path.AsString()); | |
431 } while ((result == NULL) && (errno == EINTR)); | |
432 if (result == NULL) { | |
433 return NULL; | |
434 } | |
435 return path.AsScopedString(); | |
436 } | |
437 | |
438 bool Directory::Delete(const char* dir_name, bool recursive) { | |
439 if (!recursive) { | |
440 if ((File::GetType(dir_name, false) == File::kIsLink) && | |
441 (File::GetType(dir_name, true) == File::kIsDirectory)) { | |
442 return NO_RETRY_EXPECTED(unlink(dir_name)) == 0; | |
443 } | 461 } |
444 return NO_RETRY_EXPECTED(rmdir(dir_name)) == 0; | 462 random_bytes[6] = '\0'; |
445 } else { | 463 if (!path.Add(reinterpret_cast<char*>(random_bytes))) { |
446 PathBuffer path; | 464 return NULL; |
447 if (!path.Add(dir_name)) { | |
448 return false; | |
449 } | 465 } |
450 return DeleteRecursively(&path); | 466 NamespaceScope ns(namespc, path.AsString()); |
| 467 const int result = NO_RETRY_EXPECTED(mkdirat(ns.fd(), ns.path(), 0777)); |
| 468 if (result == 0) { |
| 469 return path.AsScopedString(); |
| 470 } else if (errno == EEXIST) { |
| 471 path.Reset(prefix_length); |
| 472 } else { |
| 473 return NULL; |
| 474 } |
451 } | 475 } |
452 } | 476 } |
453 | 477 |
454 bool Directory::Rename(const char* path, const char* new_path) { | 478 bool Directory::Delete(Namespace* namespc, |
455 ExistsResult exists = Exists(path); | 479 const char* dir_name, |
| 480 bool recursive) { |
| 481 NamespaceScope ns(namespc, dir_name); |
| 482 if (!recursive) { |
| 483 if ((File::GetType(namespc, dir_name, false) == File::kIsLink) && |
| 484 (File::GetType(namespc, dir_name, true) == File::kIsDirectory)) { |
| 485 return NO_RETRY_EXPECTED(unlinkat(ns.fd(), ns.path(), 0)) == 0; |
| 486 } |
| 487 return NO_RETRY_EXPECTED(unlinkat(ns.fd(), ns.path(), AT_REMOVEDIR)) == 0; |
| 488 } else { |
| 489 PathBuffer path; |
| 490 if (!path.Add(ns.path())) { |
| 491 return false; |
| 492 } |
| 493 return DeleteRecursively(ns.fd(), &path); |
| 494 } |
| 495 } |
| 496 |
| 497 bool Directory::Rename(Namespace* namespc, |
| 498 const char* old_path, |
| 499 const char* new_path) { |
| 500 ExistsResult exists = Exists(namespc, old_path); |
456 if (exists != EXISTS) { | 501 if (exists != EXISTS) { |
457 return false; | 502 return false; |
458 } | 503 } |
459 return (NO_RETRY_EXPECTED(rename(path, new_path)) == 0); | 504 NamespaceScope oldns(namespc, old_path); |
| 505 NamespaceScope newns(namespc, new_path); |
| 506 return (NO_RETRY_EXPECTED(renameat(oldns.fd(), oldns.path(), newns.fd(), |
| 507 newns.path())) == 0); |
460 } | 508 } |
461 | 509 |
462 } // namespace bin | 510 } // namespace bin |
463 } // namespace dart | 511 } // namespace dart |
464 | 512 |
465 #endif // defined(HOST_OS_LINUX) | 513 #endif // defined(HOST_OS_LINUX) |
OLD | NEW |