| 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(TARGET_OS_MACOS) | 6 #if defined(TARGET_OS_MACOS) |
| 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 <string.h> // NOLINT | 12 #include <string.h> // NOLINT |
| 13 #include <sys/param.h> // NOLINT | 13 #include <sys/param.h> // NOLINT |
| 14 #include <sys/stat.h> // NOLINT | 14 #include <sys/stat.h> // NOLINT |
| 15 #include <unistd.h> // NOLINT | 15 #include <unistd.h> // NOLINT |
| 16 | 16 |
| 17 #include "bin/dartutils.h" |
| 17 #include "bin/file.h" | 18 #include "bin/file.h" |
| 18 #include "bin/platform.h" | 19 #include "bin/platform.h" |
| 19 | |
| 20 #include "platform/signal_blocker.h" | 20 #include "platform/signal_blocker.h" |
| 21 | 21 |
| 22 | |
| 23 namespace dart { | 22 namespace dart { |
| 24 namespace bin { | 23 namespace bin { |
| 25 | 24 |
| 25 PathBuffer::PathBuffer() : length_(0) { |
| 26 data_ = calloc(PATH_MAX + 1, sizeof(char)); // NOLINT |
| 27 } |
| 26 | 28 |
| 27 PathBuffer::PathBuffer() : length_(0) { | |
| 28 data_ = calloc(PATH_MAX + 1, sizeof(char)); // NOLINT | |
| 29 } | |
| 30 | 29 |
| 31 bool PathBuffer::AddW(const wchar_t* name) { | 30 bool PathBuffer::AddW(const wchar_t* name) { |
| 32 UNREACHABLE(); | 31 UNREACHABLE(); |
| 33 return false; | 32 return false; |
| 34 } | 33 } |
| 35 | 34 |
| 35 |
| 36 char* PathBuffer::AsString() const { | 36 char* PathBuffer::AsString() const { |
| 37 return reinterpret_cast<char*>(data_); | 37 return reinterpret_cast<char*>(data_); |
| 38 } | 38 } |
| 39 | 39 |
| 40 |
| 40 wchar_t* PathBuffer::AsStringW() const { | 41 wchar_t* PathBuffer::AsStringW() const { |
| 41 UNREACHABLE(); | 42 UNREACHABLE(); |
| 42 return NULL; | 43 return NULL; |
| 43 } | 44 } |
| 44 | 45 |
| 46 |
| 47 const char* PathBuffer::AsScopedString() const { |
| 48 return DartUtils::ScopedCopyCString(AsString()); |
| 49 } |
| 50 |
| 51 |
| 45 bool PathBuffer::Add(const char* name) { | 52 bool PathBuffer::Add(const char* name) { |
| 46 char* data = AsString(); | 53 char* data = AsString(); |
| 47 int written = snprintf(data + length_, | 54 int written = snprintf(data + length_, |
| 48 PATH_MAX - length_, | 55 PATH_MAX - length_, |
| 49 "%s", | 56 "%s", |
| 50 name); | 57 name); |
| 51 data[PATH_MAX] = '\0'; | 58 data[PATH_MAX] = '\0'; |
| 52 if (written <= PATH_MAX - length_ && | 59 if ((written <= PATH_MAX - length_) && |
| 53 written >= 0 && | 60 (written >= 0) && |
| 54 static_cast<size_t>(written) == strlen(name)) { | 61 (static_cast<size_t>(written) == strlen(name))) { |
| 55 length_ += written; | 62 length_ += written; |
| 56 return true; | 63 return true; |
| 57 } else { | 64 } else { |
| 58 errno = ENAMETOOLONG; | 65 errno = ENAMETOOLONG; |
| 59 return false; | 66 return false; |
| 60 } | 67 } |
| 61 } | 68 } |
| 62 | 69 |
| 63 void PathBuffer::Reset(int new_length) { | 70 |
| 71 void PathBuffer::Reset(intptr_t new_length) { |
| 64 length_ = new_length; | 72 length_ = new_length; |
| 65 AsString()[length_] = '\0'; | 73 AsString()[length_] = '\0'; |
| 66 } | 74 } |
| 67 | 75 |
| 68 | 76 |
| 69 // A linked list of symbolic links, with their unique file system identifiers. | 77 // A linked list of symbolic links, with their unique file system identifiers. |
| 70 // These are scanned to detect loops while doing a recursive directory listing. | 78 // These are scanned to detect loops while doing a recursive directory listing. |
| 71 struct LinkList { | 79 struct LinkList { |
| 72 dev_t dev; | 80 dev_t dev; |
| 73 ino_t ino; | 81 ino_t ino; |
| 74 LinkList* next; | 82 LinkList* next; |
| 75 }; | 83 }; |
| 76 | 84 |
| 77 | 85 |
| 78 ListType DirectoryListingEntry::Next(DirectoryListing* listing) { | 86 ListType DirectoryListingEntry::Next(DirectoryListing* listing) { |
| 79 if (done_) { | 87 if (done_) { |
| 80 return kListDone; | 88 return kListDone; |
| 81 } | 89 } |
| 82 | 90 |
| 83 if (lister_ == 0) { | 91 if (lister_ == 0) { |
| 84 do { | 92 do { |
| 85 lister_ = reinterpret_cast<intptr_t>( | 93 lister_ = reinterpret_cast<intptr_t>( |
| 86 opendir(listing->path_buffer().AsString())); | 94 opendir(listing->path_buffer().AsString())); |
| 87 } while (lister_ == 0 && errno == EINTR); | 95 } while ((lister_ == 0) && (errno == EINTR)); |
| 88 | 96 |
| 89 if (lister_ == 0) { | 97 if (lister_ == 0) { |
| 90 done_ = true; | 98 done_ = true; |
| 91 return kListError; | 99 return kListError; |
| 92 } | 100 } |
| 93 if (parent_ != NULL) { | 101 if (parent_ != NULL) { |
| 94 if (!listing->path_buffer().Add(File::PathSeparator())) { | 102 if (!listing->path_buffer().Add(File::PathSeparator())) { |
| 95 return kListError; | 103 return kListError; |
| 96 } | 104 } |
| 97 } | 105 } |
| 98 path_length_ = listing->path_buffer().length(); | 106 path_length_ = listing->path_buffer().length(); |
| 99 } | 107 } |
| 100 // Reset. | 108 // Reset. |
| 101 listing->path_buffer().Reset(path_length_); | 109 listing->path_buffer().Reset(path_length_); |
| 102 ResetLink(); | 110 ResetLink(); |
| 103 | 111 |
| 104 // Iterate the directory and post the directories and files to the | 112 // Iterate the directory and post the directories and files to the |
| 105 // ports. | 113 // ports. |
| 106 int status = 0; | 114 int status = 0; |
| 107 dirent entry; | 115 dirent entry; |
| 108 dirent* result; | 116 dirent* result; |
| 109 if ((status = NO_RETRY_EXPECTED(readdir_r(reinterpret_cast<DIR*>(lister_), | 117 status = NO_RETRY_EXPECTED(readdir_r( |
| 110 &entry, | 118 reinterpret_cast<DIR*>(lister_), &entry, &result)); |
| 111 &result))) == 0 && | 119 if ((status == 0) && (result != NULL)) { |
| 112 result != NULL) { | |
| 113 if (!listing->path_buffer().Add(entry.d_name)) { | 120 if (!listing->path_buffer().Add(entry.d_name)) { |
| 114 done_ = true; | 121 done_ = true; |
| 115 return kListError; | 122 return kListError; |
| 116 } | 123 } |
| 117 switch (entry.d_type) { | 124 switch (entry.d_type) { |
| 118 case DT_DIR: | 125 case DT_DIR: |
| 119 if (strcmp(entry.d_name, ".") == 0) return Next(listing); | 126 if ((strcmp(entry.d_name, ".") == 0) || |
| 120 if (strcmp(entry.d_name, "..") == 0) return Next(listing); | 127 (strcmp(entry.d_name, "..") == 0)) { |
| 128 return Next(listing); |
| 129 } |
| 121 return kListDirectory; | 130 return kListDirectory; |
| 122 case DT_REG: | 131 case DT_REG: |
| 123 return kListFile; | 132 return kListFile; |
| 124 case DT_LNK: | 133 case DT_LNK: |
| 125 if (!listing->follow_links()) { | 134 if (!listing->follow_links()) { |
| 126 return kListLink; | 135 return kListLink; |
| 127 } | 136 } |
| 128 // Else fall through to next case. | 137 // Else fall through to next case. |
| 129 // Fall through. | 138 // Fall through. |
| 130 case DT_UNKNOWN: { | 139 case DT_UNKNOWN: { |
| 131 // On some file systems the entry type is not determined by | 140 // On some file systems the entry type is not determined by |
| 132 // readdir_r. For those and for links we use stat to determine | 141 // readdir_r. For those and for links we use stat to determine |
| 133 // the actual entry type. Notice that stat returns the type of | 142 // the actual entry type. Notice that stat returns the type of |
| 134 // the file pointed to. | 143 // the file pointed to. |
| 135 struct stat entry_info; | 144 struct stat entry_info; |
| 136 int stat_success; | 145 int stat_success; |
| 137 stat_success = NO_RETRY_EXPECTED( | 146 stat_success = NO_RETRY_EXPECTED( |
| 138 lstat(listing->path_buffer().AsString(), &entry_info)); | 147 lstat(listing->path_buffer().AsString(), &entry_info)); |
| 139 if (stat_success == -1) { | 148 if (stat_success == -1) { |
| 140 return kListError; | 149 return kListError; |
| 141 } | 150 } |
| 142 if (listing->follow_links() && S_ISLNK(entry_info.st_mode)) { | 151 if (listing->follow_links() && S_ISLNK(entry_info.st_mode)) { |
| 143 // Check to see if we are in a loop created by a symbolic link. | 152 // Check to see if we are in a loop created by a symbolic link. |
| 144 LinkList current_link = { entry_info.st_dev, | 153 LinkList current_link = { entry_info.st_dev, |
| 145 entry_info.st_ino, | 154 entry_info.st_ino, |
| 146 link_ }; | 155 link_ }; |
| 147 LinkList* previous = link_; | 156 LinkList* previous = link_; |
| 148 while (previous != NULL) { | 157 while (previous != NULL) { |
| 149 if (previous->dev == current_link.dev && | 158 if ((previous->dev == current_link.dev) && |
| 150 previous->ino == current_link.ino) { | 159 (previous->ino == current_link.ino)) { |
| 151 // Report the looping link as a link, rather than following it. | 160 // Report the looping link as a link, rather than following it. |
| 152 return kListLink; | 161 return kListLink; |
| 153 } | 162 } |
| 154 previous = previous->next; | 163 previous = previous->next; |
| 155 } | 164 } |
| 156 stat_success = NO_RETRY_EXPECTED( | 165 stat_success = NO_RETRY_EXPECTED( |
| 157 stat(listing->path_buffer().AsString(), &entry_info)); | 166 stat(listing->path_buffer().AsString(), &entry_info)); |
| 158 if (stat_success == -1) { | 167 if (stat_success == -1) { |
| 159 // Report a broken link as a link, even if follow_links is true. | 168 // Report a broken link as a link, even if follow_links is true. |
| 160 return kListLink; | 169 return kListLink; |
| 161 } | 170 } |
| 162 if (S_ISDIR(entry_info.st_mode)) { | 171 if (S_ISDIR(entry_info.st_mode)) { |
| 163 // Recurse into the subdirectory with current_link added to the | 172 // Recurse into the subdirectory with current_link added to the |
| 164 // linked list of seen file system links. | 173 // linked list of seen file system links. |
| 165 link_ = new LinkList(current_link); | 174 link_ = new LinkList(current_link); |
| 166 if (strcmp(entry.d_name, ".") == 0) return Next(listing); | 175 if ((strcmp(entry.d_name, ".") == 0) || |
| 167 if (strcmp(entry.d_name, "..") == 0) return Next(listing); | 176 (strcmp(entry.d_name, "..") == 0)) { |
| 177 return Next(listing); |
| 178 } |
| 168 return kListDirectory; | 179 return kListDirectory; |
| 169 } | 180 } |
| 170 } | 181 } |
| 171 if (S_ISDIR(entry_info.st_mode)) { | 182 if (S_ISDIR(entry_info.st_mode)) { |
| 172 if (strcmp(entry.d_name, ".") == 0) return Next(listing); | 183 if ((strcmp(entry.d_name, ".") == 0) || |
| 173 if (strcmp(entry.d_name, "..") == 0) return Next(listing); | 184 (strcmp(entry.d_name, "..") == 0)) { |
| 185 return Next(listing); |
| 186 } |
| 174 return kListDirectory; | 187 return kListDirectory; |
| 175 } else if (S_ISREG(entry_info.st_mode)) { | 188 } else if (S_ISREG(entry_info.st_mode)) { |
| 176 return kListFile; | 189 return kListFile; |
| 177 } else if (S_ISLNK(entry_info.st_mode)) { | 190 } else if (S_ISLNK(entry_info.st_mode)) { |
| 178 return kListLink; | 191 return kListLink; |
| 179 } | 192 } |
| 180 } | 193 } |
| 181 | 194 |
| 182 default: | 195 default: |
| 183 break; | 196 break; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 196 | 209 |
| 197 DirectoryListingEntry::~DirectoryListingEntry() { | 210 DirectoryListingEntry::~DirectoryListingEntry() { |
| 198 ResetLink(); | 211 ResetLink(); |
| 199 if (lister_ != 0) { | 212 if (lister_ != 0) { |
| 200 closedir(reinterpret_cast<DIR*>(lister_)); | 213 closedir(reinterpret_cast<DIR*>(lister_)); |
| 201 } | 214 } |
| 202 } | 215 } |
| 203 | 216 |
| 204 | 217 |
| 205 void DirectoryListingEntry::ResetLink() { | 218 void DirectoryListingEntry::ResetLink() { |
| 206 if (link_ != NULL && (parent_ == NULL || parent_->link_ != link_)) { | 219 if ((link_ != NULL) && ((parent_ == NULL) || (parent_->link_ != link_))) { |
| 207 delete link_; | 220 delete link_; |
| 208 link_ = NULL; | 221 link_ = NULL; |
| 209 } | 222 } |
| 210 if (parent_ != NULL) { | 223 if (parent_ != NULL) { |
| 211 link_ = parent_->link_; | 224 link_ = parent_->link_; |
| 212 } | 225 } |
| 213 } | 226 } |
| 214 | 227 |
| 215 | 228 |
| 216 static bool DeleteRecursively(PathBuffer* path); | 229 static bool DeleteRecursively(PathBuffer* path); |
| 217 | 230 |
| 218 | 231 |
| 219 static bool DeleteFile(char* file_name, | 232 static bool DeleteFile(char* file_name, |
| 220 PathBuffer* path) { | 233 PathBuffer* path) { |
| 221 return path->Add(file_name) && unlink(path->AsString()) == 0; | 234 return path->Add(file_name) && (unlink(path->AsString()) == 0); |
| 222 } | 235 } |
| 223 | 236 |
| 224 | 237 |
| 225 static bool DeleteDir(char* dir_name, | 238 static bool DeleteDir(char* dir_name, |
| 226 PathBuffer* path) { | 239 PathBuffer* path) { |
| 227 if (strcmp(dir_name, ".") == 0) return true; | 240 if ((strcmp(dir_name, ".") == 0) || (strcmp(dir_name, "..") == 0)) { |
| 228 if (strcmp(dir_name, "..") == 0) return true; | 241 return true; |
| 242 } |
| 229 return path->Add(dir_name) && DeleteRecursively(path); | 243 return path->Add(dir_name) && DeleteRecursively(path); |
| 230 } | 244 } |
| 231 | 245 |
| 232 | 246 |
| 233 static bool DeleteRecursively(PathBuffer* path) { | 247 static bool DeleteRecursively(PathBuffer* path) { |
| 234 // Do not recurse into links for deletion. Instead delete the link. | 248 // Do not recurse into links for deletion. Instead delete the link. |
| 235 // If it's a file, delete it. | 249 // If it's a file, delete it. |
| 236 struct stat st; | 250 struct stat st; |
| 237 if (NO_RETRY_EXPECTED(lstat(path->AsString(), &st)) == -1) { | 251 if (NO_RETRY_EXPECTED(lstat(path->AsString(), &st)) == -1) { |
| 238 return false; | 252 return false; |
| 239 } else if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) { | 253 } else if (S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)) { |
| 240 return (unlink(path->AsString()) == 0); | 254 return (unlink(path->AsString()) == 0); |
| 241 } | 255 } |
| 242 | 256 |
| 243 if (!path->Add(File::PathSeparator())) return false; | 257 if (!path->Add(File::PathSeparator())) { |
| 258 return false; |
| 259 } |
| 244 | 260 |
| 245 // Not a link. Attempt to open as a directory and recurse into the | 261 // Not a link. Attempt to open as a directory and recurse into the |
| 246 // directory. | 262 // directory. |
| 247 DIR* dir_pointer; | 263 DIR* dir_pointer; |
| 248 do { | 264 do { |
| 249 dir_pointer = opendir(path->AsString()); | 265 dir_pointer = opendir(path->AsString()); |
| 250 } while (dir_pointer == NULL && errno == EINTR); | 266 } while ((dir_pointer == NULL) && (errno == EINTR)); |
| 251 if (dir_pointer == NULL) { | 267 if (dir_pointer == NULL) { |
| 252 return false; | 268 return false; |
| 253 } | 269 } |
| 254 | 270 |
| 255 // Iterate the directory and delete all files and directories. | 271 // Iterate the directory and delete all files and directories. |
| 256 int path_length = path->length(); | 272 int path_length = path->length(); |
| 257 dirent entry; | 273 dirent entry; |
| 258 dirent* result; | 274 dirent* result; |
| 259 while (NO_RETRY_EXPECTED(readdir_r(dir_pointer, &entry, &result)) == 0) { | 275 while (NO_RETRY_EXPECTED(readdir_r(dir_pointer, &entry, &result)) == 0) { |
| 260 if (result == NULL) { | 276 if (result == NULL) { |
| 261 // End of directory. | 277 // End of directory. |
| 262 return NO_RETRY_EXPECTED(closedir(dir_pointer)) == 0 && | 278 return (NO_RETRY_EXPECTED(closedir(dir_pointer)) == 0) && |
| 263 NO_RETRY_EXPECTED(remove(path->AsString())) == 0; | 279 (NO_RETRY_EXPECTED(remove(path->AsString())) == 0); |
| 264 } | 280 } |
| 265 bool ok = false; | 281 bool ok = false; |
| 266 switch (entry.d_type) { | 282 switch (entry.d_type) { |
| 267 case DT_DIR: | 283 case DT_DIR: |
| 268 ok = DeleteDir(entry.d_name, path); | 284 ok = DeleteDir(entry.d_name, path); |
| 269 break; | 285 break; |
| 270 case DT_REG: | 286 case DT_REG: |
| 271 case DT_LNK: | 287 case DT_LNK: |
| 272 // Treat all links as files. This will delete the link which | 288 // Treat all links as files. This will delete the link which |
| 273 // is what we want no matter if the link target is a file or a | 289 // is what we want no matter if the link target is a file or a |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 316 Directory::ExistsResult Directory::Exists(const char* dir_name) { | 332 Directory::ExistsResult Directory::Exists(const char* dir_name) { |
| 317 struct stat entry_info; | 333 struct stat entry_info; |
| 318 int success = NO_RETRY_EXPECTED(stat(dir_name, &entry_info)); | 334 int success = NO_RETRY_EXPECTED(stat(dir_name, &entry_info)); |
| 319 if (success == 0) { | 335 if (success == 0) { |
| 320 if (S_ISDIR(entry_info.st_mode)) { | 336 if (S_ISDIR(entry_info.st_mode)) { |
| 321 return EXISTS; | 337 return EXISTS; |
| 322 } else { | 338 } else { |
| 323 return DOES_NOT_EXIST; | 339 return DOES_NOT_EXIST; |
| 324 } | 340 } |
| 325 } else { | 341 } else { |
| 326 if (errno == EACCES || | 342 if ((errno == EACCES) || |
| 327 errno == EBADF || | 343 (errno == EBADF) || |
| 328 errno == EFAULT || | 344 (errno == EFAULT) || |
| 329 errno == ENOMEM || | 345 (errno == ENOMEM) || |
| 330 errno == EOVERFLOW) { | 346 (errno == EOVERFLOW)) { |
| 331 // Search permissions denied for one of the directories in the | 347 // Search permissions denied for one of the directories in the |
| 332 // path or a low level error occured. We do not know if the | 348 // path or a low level error occured. We do not know if the |
| 333 // directory exists. | 349 // directory exists. |
| 334 return UNKNOWN; | 350 return UNKNOWN; |
| 335 } | 351 } |
| 336 ASSERT(errno == ELOOP || | 352 ASSERT((errno == ELOOP) || |
| 337 errno == ENAMETOOLONG || | 353 (errno == ENAMETOOLONG) || |
| 338 errno == ENOENT || | 354 (errno == ENOENT) || |
| 339 errno == ENOTDIR); | 355 (errno == ENOTDIR)); |
| 340 return DOES_NOT_EXIST; | 356 return DOES_NOT_EXIST; |
| 341 } | 357 } |
| 342 } | 358 } |
| 343 | 359 |
| 344 | 360 |
| 345 char* Directory::Current() { | 361 char* Directory::CurrentNoScope() { |
| 346 return getcwd(NULL, 0); | 362 return getcwd(NULL, 0); |
| 347 } | 363 } |
| 348 | 364 |
| 349 | 365 |
| 366 const char* Directory::Current() { |
| 367 char* result = DartUtils::ScopedCString(PATH_MAX); |
| 368 ASSERT(result != NULL); |
| 369 return getcwd(result, PATH_MAX); |
| 370 } |
| 371 |
| 372 |
| 350 bool Directory::SetCurrent(const char* path) { | 373 bool Directory::SetCurrent(const char* path) { |
| 351 int result = NO_RETRY_EXPECTED(chdir(path)); | 374 int result = NO_RETRY_EXPECTED(chdir(path)); |
| 352 return result == 0; | 375 return result == 0; |
| 353 } | 376 } |
| 354 | 377 |
| 355 | 378 |
| 356 bool Directory::Create(const char* dir_name) { | 379 bool Directory::Create(const char* dir_name) { |
| 357 // Create the directory with the permissions specified by the | 380 // Create the directory with the permissions specified by the |
| 358 // process umask. | 381 // process umask. |
| 359 int result = NO_RETRY_EXPECTED(mkdir(dir_name, 0777)); | 382 int result = NO_RETRY_EXPECTED(mkdir(dir_name, 0777)); |
| 360 // If the directory already exists, treat it as a success. | 383 // If the directory already exists, treat it as a success. |
| 361 if (result == -1 && errno == EEXIST) { | 384 if ((result == -1) && (errno == EEXIST)) { |
| 362 return (Exists(dir_name) == EXISTS); | 385 return (Exists(dir_name) == EXISTS); |
| 363 } | 386 } |
| 364 return (result == 0); | 387 return (result == 0); |
| 365 } | 388 } |
| 366 | 389 |
| 367 | 390 |
| 368 char* Directory::SystemTemp() { | 391 const char* Directory::SystemTemp() { |
| 392 PathBuffer path; |
| 369 const char* temp_dir = getenv("TMPDIR"); | 393 const char* temp_dir = getenv("TMPDIR"); |
| 370 if (temp_dir == NULL) { | 394 if (temp_dir == NULL) { |
| 371 temp_dir = getenv("TMP"); | 395 temp_dir = getenv("TMP"); |
| 372 } | 396 } |
| 373 if (temp_dir == NULL) { | 397 if (temp_dir == NULL) { |
| 374 temp_dir = "/tmp"; | 398 temp_dir = "/tmp"; |
| 375 } | 399 } |
| 376 char* result = strdup(temp_dir); | 400 if (!path.Add(temp_dir)) { |
| 401 return NULL; |
| 402 } |
| 377 // Remove any trailing slash. | 403 // Remove any trailing slash. |
| 404 char* result = path.AsString(); |
| 378 int length = strlen(result); | 405 int length = strlen(result); |
| 379 if (length > 1 && result[length - 1] == '/') { | 406 if ((length > 1) && (result[length - 1] == '/')) { |
| 380 result[length - 1] = '\0'; | 407 result[length - 1] = '\0'; |
| 381 } | 408 } |
| 382 return result; | 409 return path.AsScopedString(); |
| 383 } | 410 } |
| 384 | 411 |
| 385 | 412 |
| 386 char* Directory::CreateTemp(const char* prefix) { | 413 const char* Directory::CreateTemp(const char* prefix) { |
| 387 // Returns a new, unused directory name, adding characters to the end | 414 // Returns a new, unused directory name, adding characters to the end |
| 388 // of prefix. Creates the directory with the permissions specified | 415 // of prefix. Creates the directory with the permissions specified |
| 389 // by the process umask. | 416 // by the process umask. |
| 390 // The return value must be freed by the caller. | 417 // The return value is Dart_ScopeAllocated. |
| 391 PathBuffer path; | 418 PathBuffer path; |
| 392 path.Add(prefix); | 419 if (!path.Add(prefix)) { |
| 420 return NULL; |
| 421 } |
| 393 if (!path.Add("XXXXXX")) { | 422 if (!path.Add("XXXXXX")) { |
| 394 // Pattern has overflowed. | 423 // Pattern has overflowed. |
| 395 return NULL; | 424 return NULL; |
| 396 } | 425 } |
| 397 char* result; | 426 char* result; |
| 398 do { | 427 do { |
| 399 result = mkdtemp(path.AsString()); | 428 result = mkdtemp(path.AsString()); |
| 400 } while (result == NULL && errno == EINTR); | 429 } while ((result == NULL) && (errno == EINTR)); |
| 401 if (result == NULL) { | 430 if (result == NULL) { |
| 402 return NULL; | 431 return NULL; |
| 403 } | 432 } |
| 404 return strdup(result); | 433 return path.AsScopedString(); |
| 405 } | 434 } |
| 406 | 435 |
| 407 | 436 |
| 408 bool Directory::Delete(const char* dir_name, bool recursive) { | 437 bool Directory::Delete(const char* dir_name, bool recursive) { |
| 409 if (!recursive) { | 438 if (!recursive) { |
| 410 if (File::GetType(dir_name, false) == File::kIsLink && | 439 if ((File::GetType(dir_name, false) == File::kIsLink) && |
| 411 File::GetType(dir_name, true) == File::kIsDirectory) { | 440 (File::GetType(dir_name, true) == File::kIsDirectory)) { |
| 412 return (NO_RETRY_EXPECTED(unlink(dir_name)) == 0); | 441 return (NO_RETRY_EXPECTED(unlink(dir_name)) == 0); |
| 413 } | 442 } |
| 414 return (NO_RETRY_EXPECTED(rmdir(dir_name)) == 0); | 443 return (NO_RETRY_EXPECTED(rmdir(dir_name)) == 0); |
| 415 } else { | 444 } else { |
| 416 PathBuffer path; | 445 PathBuffer path; |
| 417 if (!path.Add(dir_name)) { | 446 if (!path.Add(dir_name)) { |
| 418 return false; | 447 return false; |
| 419 } | 448 } |
| 420 return DeleteRecursively(&path); | 449 return DeleteRecursively(&path); |
| 421 } | 450 } |
| 422 } | 451 } |
| 423 | 452 |
| 424 | 453 |
| 425 bool Directory::Rename(const char* path, const char* new_path) { | 454 bool Directory::Rename(const char* path, const char* new_path) { |
| 426 ExistsResult exists = Exists(path); | 455 ExistsResult exists = Exists(path); |
| 427 if (exists != EXISTS) return false; | 456 if (exists != EXISTS) { |
| 457 return false; |
| 458 } |
| 428 return (NO_RETRY_EXPECTED(rename(path, new_path)) == 0); | 459 return (NO_RETRY_EXPECTED(rename(path, new_path)) == 0); |
| 429 } | 460 } |
| 430 | 461 |
| 431 } // namespace bin | 462 } // namespace bin |
| 432 } // namespace dart | 463 } // namespace dart |
| 433 | 464 |
| 434 #endif // defined(TARGET_OS_MACOS) | 465 #endif // defined(TARGET_OS_MACOS) |
| OLD | NEW |