| 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 = TEMP_FAILURE_RETRY(openat64( |
| 92 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(fstatat64( |
| 141 lstat64(listing->path_buffer().AsString(), &entry_info)); | 157 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 = TEMP_FAILURE_RETRY(fstatat64( |
| 158 stat64(listing->path_buffer().AsString(), &entry_info)); | 174 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(fstatat64(dirfd, |
| 263 path->AsString(), |
| 264 &st, |
| 265 AT_SYMLINK_NOFOLLOW)) == -1) { |
| 244 return false; | 266 return false; |
| 245 } else if (!S_ISDIR(st.st_mode)) { | 267 } else if (!S_ISDIR(st.st_mode)) { |
| 246 return (NO_RETRY_EXPECTED(unlink(path->AsString())) == 0); | 268 return (NO_RETRY_EXPECTED(unlinkat(dirfd, path->AsString(), 0)) == 0); |
| 247 } | 269 } |
| 248 | 270 |
| 249 if (!path->Add(File::PathSeparator())) { | 271 if (!path->Add(File::PathSeparator())) { |
| 250 return false; | 272 return false; |
| 251 } | 273 } |
| 252 | 274 |
| 253 // Not a link. Attempt to open as a directory and recurse into the | 275 // Not a link. Attempt to open as a directory and recurse into the |
| 254 // directory. | 276 // directory. |
| 277 const int fd = TEMP_FAILURE_RETRY(openat64( |
| 278 dirfd, path->AsString(), O_DIRECTORY)); |
| 279 if (fd < 0) { |
| 280 return false; |
| 281 } |
| 255 DIR* dir_pointer; | 282 DIR* dir_pointer; |
| 256 do { | 283 do { |
| 257 dir_pointer = opendir(path->AsString()); | 284 dir_pointer = fdopendir(fd); |
| 258 } while ((dir_pointer == NULL) && (errno == EINTR)); | 285 } while ((dir_pointer == NULL) && (errno == EINTR)); |
| 259 if (dir_pointer == NULL) { | 286 if (dir_pointer == NULL) { |
| 287 FDUtils::SaveErrorAndClose(fd); |
| 260 return false; | 288 return false; |
| 261 } | 289 } |
| 262 | 290 |
| 263 // Iterate the directory and delete all files and directories. | 291 // Iterate the directory and delete all files and directories. |
| 264 int path_length = path->length(); | 292 int path_length = path->length(); |
| 265 while (true) { | 293 while (true) { |
| 266 // In case `readdir()` returns `NULL` we distinguish between end-of-stream | 294 // In case `readdir()` returns `NULL` we distinguish between end-of-stream |
| 267 // and error by looking if `errno` was updated. | 295 // and error by looking if `errno` was updated. |
| 268 errno = 0; | 296 errno = 0; |
| 269 // In glibc 2.24+, readdir_r is deprecated. | 297 // In glibc 2.24+, readdir_r is deprecated. |
| 270 // According to the man page for readdir: | 298 // According to the man page for readdir: |
| 271 // "readdir(3) is not required to be thread-safe. However, in modern | 299 // "readdir(3) is not required to be thread-safe. However, in modern |
| 272 // implementations (including the glibc implementation), concurrent calls to | 300 // implementations (including the glibc implementation), concurrent calls to |
| 273 // readdir(3) that specify different directory streams are thread-safe." | 301 // readdir(3) that specify different directory streams are thread-safe." |
| 274 dirent* entry = readdir(dir_pointer); | 302 dirent* entry = readdir(dir_pointer); |
| 275 if (entry == NULL) { | 303 if (entry == NULL) { |
| 276 // Failed to read next directory entry. | 304 // Failed to read next directory entry. |
| 277 if (errno != 0) { | 305 if (errno != 0) { |
| 278 break; | 306 break; |
| 279 } | 307 } |
| 280 // End of directory. | 308 // End of directory. |
| 281 return (NO_RETRY_EXPECTED(closedir(dir_pointer)) == 0) && | 309 int status = NO_RETRY_EXPECTED(closedir(dir_pointer)); |
| 282 (NO_RETRY_EXPECTED(remove(path->AsString())) == 0); | 310 FDUtils::SaveErrorAndClose(fd); |
| 311 if (status != 0) { |
| 312 return false; |
| 313 } |
| 314 status = |
| 315 NO_RETRY_EXPECTED(unlinkat(dirfd, path->AsString(), AT_REMOVEDIR)); |
| 316 return status == 0; |
| 283 } | 317 } |
| 284 bool ok = false; | 318 bool ok = false; |
| 285 switch (entry->d_type) { | 319 switch (entry->d_type) { |
| 286 case DT_DIR: | 320 case DT_DIR: |
| 287 ok = DeleteDir(entry->d_name, path); | 321 ok = DeleteDir(dirfd, entry->d_name, path); |
| 288 break; | 322 break; |
| 289 case DT_BLK: | 323 case DT_BLK: |
| 290 case DT_CHR: | 324 case DT_CHR: |
| 291 case DT_FIFO: | 325 case DT_FIFO: |
| 292 case DT_SOCK: | 326 case DT_SOCK: |
| 293 case DT_REG: | 327 case DT_REG: |
| 294 case DT_LNK: | 328 case DT_LNK: |
| 295 // Treat all links as files. This will delete the link which | 329 // 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 | 330 // is what we want no matter if the link target is a file or a |
| 297 // directory. | 331 // directory. |
| 298 ok = DeleteFile(entry->d_name, path); | 332 ok = DeleteFile(dirfd, entry->d_name, path); |
| 299 break; | 333 break; |
| 300 case DT_UNKNOWN: { | 334 case DT_UNKNOWN: { |
| 301 if (!path->Add(entry->d_name)) { | 335 if (!path->Add(entry->d_name)) { |
| 302 break; | 336 break; |
| 303 } | 337 } |
| 304 // On some file systems the entry type is not determined by | 338 // On some file systems the entry type is not determined by |
| 305 // readdir. For those we use lstat to determine the entry | 339 // readdir. For those we use lstat to determine the entry |
| 306 // type. | 340 // type. |
| 307 struct stat64 entry_info; | 341 struct stat64 entry_info; |
| 308 if (TEMP_FAILURE_RETRY(lstat64(path->AsString(), &entry_info)) == -1) { | 342 if (TEMP_FAILURE_RETRY(fstatat64(dirfd, |
| 343 path->AsString(), |
| 344 &entry_info, |
| 345 AT_SYMLINK_NOFOLLOW)) == -1) { |
| 309 break; | 346 break; |
| 310 } | 347 } |
| 311 path->Reset(path_length); | 348 path->Reset(path_length); |
| 312 if (S_ISDIR(entry_info.st_mode)) { | 349 if (S_ISDIR(entry_info.st_mode)) { |
| 313 ok = DeleteDir(entry->d_name, path); | 350 ok = DeleteDir(dirfd, entry->d_name, path); |
| 314 } else { | 351 } else { |
| 315 // Treat links as files. This will delete the link which is | 352 // 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 | 353 // what we want no matter if the link target is a file or a |
| 317 // directory. | 354 // directory. |
| 318 ok = DeleteFile(entry->d_name, path); | 355 ok = DeleteFile(dirfd, entry->d_name, path); |
| 319 } | 356 } |
| 320 break; | 357 break; |
| 321 } | 358 } |
| 322 default: | 359 default: |
| 323 // We should have covered all the bases. If not, let's get an error. | 360 // We should have covered all the bases. If not, let's get an error. |
| 324 FATAL1("Unexpected d_type: %d\n", entry->d_type); | 361 FATAL1("Unexpected d_type: %d\n", entry->d_type); |
| 325 break; | 362 break; |
| 326 } | 363 } |
| 327 if (!ok) { | 364 if (!ok) { |
| 328 break; | 365 break; |
| 329 } | 366 } |
| 330 path->Reset(path_length); | 367 path->Reset(path_length); |
| 331 } | 368 } |
| 332 // Only happens if an error. | 369 // Only happens if an error. |
| 333 ASSERT(errno != 0); | 370 ASSERT(errno != 0); |
| 334 int err = errno; | 371 int err = errno; |
| 335 VOID_NO_RETRY_EXPECTED(closedir(dir_pointer)); | 372 VOID_NO_RETRY_EXPECTED(closedir(dir_pointer)); |
| 373 FDUtils::SaveErrorAndClose(fd); |
| 336 errno = err; | 374 errno = err; |
| 337 return false; | 375 return false; |
| 338 } | 376 } |
| 339 | 377 |
| 340 Directory::ExistsResult Directory::Exists(const char* dir_name) { | 378 Directory::ExistsResult Directory::Exists(Namespace* namespc, |
| 379 const char* dir_name) { |
| 380 NamespaceScope ns(namespc, dir_name); |
| 341 struct stat64 entry_info; | 381 struct stat64 entry_info; |
| 342 int success = TEMP_FAILURE_RETRY(stat64(dir_name, &entry_info)); | 382 int success = TEMP_FAILURE_RETRY(fstatat64( |
| 383 ns.fd(), ns.path(), &entry_info, 0)); |
| 343 if (success == 0) { | 384 if (success == 0) { |
| 344 if (S_ISDIR(entry_info.st_mode)) { | 385 if (S_ISDIR(entry_info.st_mode)) { |
| 345 return EXISTS; | 386 return EXISTS; |
| 346 } else { | 387 } else { |
| 347 // An OSError may be constructed based on the return value of this | 388 // An OSError may be constructed based on the return value of this |
| 348 // function, so set errno to something that makes sense. | 389 // function, so set errno to something that makes sense. |
| 349 errno = ENOTDIR; | 390 errno = ENOTDIR; |
| 350 return DOES_NOT_EXIST; | 391 return DOES_NOT_EXIST; |
| 351 } | 392 } |
| 352 } else { | 393 } else { |
| 353 if ((errno == EACCES) || (errno == EBADF) || (errno == EFAULT) || | 394 if ((errno == EACCES) || (errno == EBADF) || (errno == EFAULT) || |
| 354 (errno == ENOMEM) || (errno == EOVERFLOW)) { | 395 (errno == ENOMEM) || (errno == EOVERFLOW)) { |
| 355 // Search permissions denied for one of the directories in the | 396 // Search permissions denied for one of the directories in the |
| 356 // path or a low level error occured. We do not know if the | 397 // path or a low level error occured. We do not know if the |
| 357 // directory exists. | 398 // directory exists. |
| 358 return UNKNOWN; | 399 return UNKNOWN; |
| 359 } | 400 } |
| 360 ASSERT((errno == ELOOP) || (errno == ENAMETOOLONG) || (errno == ENOENT) || | 401 ASSERT((errno == ELOOP) || (errno == ENAMETOOLONG) || (errno == ENOENT) || |
| 361 (errno == ENOTDIR)); | 402 (errno == ENOTDIR)); |
| 362 return DOES_NOT_EXIST; | 403 return DOES_NOT_EXIST; |
| 363 } | 404 } |
| 364 } | 405 } |
| 365 | 406 |
| 366 char* Directory::CurrentNoScope() { | 407 char* Directory::CurrentNoScope() { |
| 367 return getcwd(NULL, 0); | 408 return getcwd(NULL, 0); |
| 368 } | 409 } |
| 369 | 410 |
| 370 const char* Directory::Current() { | 411 bool Directory::Create(Namespace* namespc, const char* dir_name) { |
| 371 char buffer[PATH_MAX]; | 412 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 | 413 // Create the directory with the permissions specified by the |
| 384 // process umask. | 414 // process umask. |
| 385 int result = NO_RETRY_EXPECTED(mkdir(dir_name, 0777)); | 415 const int result = NO_RETRY_EXPECTED(mkdirat(ns.fd(), ns.path(), 0777)); |
| 386 // If the directory already exists, treat it as a success. | 416 // If the directory already exists, treat it as a success. |
| 387 if ((result == -1) && (errno == EEXIST)) { | 417 if ((result == -1) && (errno == EEXIST)) { |
| 388 return (Exists(dir_name) == EXISTS); | 418 return (Exists(namespc, dir_name) == EXISTS); |
| 389 } | 419 } |
| 390 return (result == 0); | 420 return (result == 0); |
| 391 } | 421 } |
| 392 | 422 |
| 393 const char* Directory::SystemTemp() { | 423 const char* Directory::SystemTemp(Namespace* namespc) { |
| 394 PathBuffer path; | 424 PathBuffer path; |
| 395 const char* temp_dir = getenv("TMPDIR"); | 425 const char* temp_dir = getenv("TMPDIR"); |
| 396 if (temp_dir == NULL) { | 426 if (temp_dir == NULL) { |
| 397 temp_dir = getenv("TMP"); | 427 temp_dir = getenv("TMP"); |
| 398 } | 428 } |
| 399 if (temp_dir == NULL) { | 429 if (temp_dir == NULL) { |
| 400 temp_dir = "/tmp"; | 430 temp_dir = "/tmp"; |
| 401 } | 431 } |
| 402 if (!path.Add(temp_dir)) { | 432 NamespaceScope ns(namespc, temp_dir); |
| 433 if (!path.Add(ns.path())) { |
| 403 return NULL; | 434 return NULL; |
| 404 } | 435 } |
| 405 | 436 |
| 406 // Remove any trailing slash. | 437 // Remove any trailing slash. |
| 407 char* result = path.AsString(); | 438 char* result = path.AsString(); |
| 408 int length = strlen(result); | 439 int length = strlen(result); |
| 409 if ((length > 1) && (result[length - 1] == '/')) { | 440 if ((length > 1) && (result[length - 1] == '/')) { |
| 410 result[length - 1] = '\0'; | 441 result[length - 1] = '\0'; |
| 411 } | 442 } |
| 412 return path.AsScopedString(); | 443 return path.AsScopedString(); |
| 413 } | 444 } |
| 414 | 445 |
| 415 const char* Directory::CreateTemp(const char* prefix) { | 446 // Returns a new, unused directory name, adding characters to the end |
| 416 // Returns a new, unused directory name, adding characters to the end | 447 // of prefix. Creates the directory with the permissions specified |
| 417 // of prefix. Creates the directory with the permissions specified | 448 // by the process umask. |
| 418 // by the process umask. | 449 // The return value is Dart_ScopeAllocated. |
| 419 // The return value is Dart_ScopeAllocated. | 450 const char* Directory::CreateTemp(Namespace* namespc, const char* prefix) { |
| 420 PathBuffer path; | 451 PathBuffer path; |
| 452 const int firstchar = 'A'; |
| 453 const int numchars = 'Z' - 'A' + 1; |
| 454 uint8_t random_bytes[7]; |
| 455 |
| 456 // mkdtemp doesn't have an "at" variant, so we have to simulate it. |
| 421 if (!path.Add(prefix)) { | 457 if (!path.Add(prefix)) { |
| 422 return NULL; | 458 return NULL; |
| 423 } | 459 } |
| 424 if (!path.Add("XXXXXX")) { | 460 intptr_t prefix_length = path.length(); |
| 425 // Pattern has overflowed. | 461 while (true) { |
| 426 return NULL; | 462 Crypto::GetRandomBytes(6, random_bytes); |
| 427 } | 463 for (intptr_t i = 0; i < 6; i++) { |
| 428 char* result; | 464 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 } | 465 } |
| 444 return NO_RETRY_EXPECTED(rmdir(dir_name)) == 0; | 466 random_bytes[6] = '\0'; |
| 445 } else { | 467 if (!path.Add(reinterpret_cast<char*>(random_bytes))) { |
| 446 PathBuffer path; | 468 return NULL; |
| 447 if (!path.Add(dir_name)) { | |
| 448 return false; | |
| 449 } | 469 } |
| 450 return DeleteRecursively(&path); | 470 NamespaceScope ns(namespc, path.AsString()); |
| 471 const int result = NO_RETRY_EXPECTED(mkdirat(ns.fd(), ns.path(), 0777)); |
| 472 if (result == 0) { |
| 473 return path.AsScopedString(); |
| 474 } else if (errno == EEXIST) { |
| 475 path.Reset(prefix_length); |
| 476 } else { |
| 477 return NULL; |
| 478 } |
| 451 } | 479 } |
| 452 } | 480 } |
| 453 | 481 |
| 454 bool Directory::Rename(const char* path, const char* new_path) { | 482 bool Directory::Delete( |
| 455 ExistsResult exists = Exists(path); | 483 Namespace* namespc, const char* dir_name, bool recursive) { |
| 484 NamespaceScope ns(namespc, dir_name); |
| 485 if (!recursive) { |
| 486 if ((File::GetType(namespc, dir_name, false) == File::kIsLink) && |
| 487 (File::GetType(namespc, dir_name, true) == File::kIsDirectory)) { |
| 488 return NO_RETRY_EXPECTED(unlinkat(ns.fd(), ns.path(), 0)) == 0; |
| 489 } |
| 490 return NO_RETRY_EXPECTED(unlinkat(ns.fd(), ns.path(), AT_REMOVEDIR)) == 0; |
| 491 } else { |
| 492 PathBuffer path; |
| 493 if (!path.Add(ns.path())) { |
| 494 return false; |
| 495 } |
| 496 return DeleteRecursively(ns.fd(), &path); |
| 497 } |
| 498 } |
| 499 |
| 500 bool Directory::Rename( |
| 501 Namespace* namespc, const char* old_path, const char* new_path) { |
| 502 ExistsResult exists = Exists(namespc, old_path); |
| 456 if (exists != EXISTS) { | 503 if (exists != EXISTS) { |
| 457 return false; | 504 return false; |
| 458 } | 505 } |
| 459 return (NO_RETRY_EXPECTED(rename(path, new_path)) == 0); | 506 NamespaceScope oldns(namespc, old_path); |
| 507 NamespaceScope newns(namespc, new_path); |
| 508 return (NO_RETRY_EXPECTED(renameat( |
| 509 oldns.fd(), oldns.path(), newns.fd(), newns.path())) == 0); |
| 460 } | 510 } |
| 461 | 511 |
| 462 } // namespace bin | 512 } // namespace bin |
| 463 } // namespace dart | 513 } // namespace dart |
| 464 | 514 |
| 465 #endif // defined(HOST_OS_LINUX) | 515 #endif // defined(HOST_OS_LINUX) |
| OLD | NEW |