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