Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(147)

Side by Side Diff: runtime/bin/directory_linux.cc

Issue 25720002: Add Directory.systemTemp getter to replace createSystemTemp(). (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Don't add an extra / to a directory ending in // Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/bin/directory_android.cc ('k') | runtime/bin/directory_macos.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
(...skipping 355 matching lines...) Expand 10 before | Expand all | Expand 10 after
366 // process umask. 366 // process umask.
367 int result = TEMP_FAILURE_RETRY(mkdir(dir_name, 0777)); 367 int result = TEMP_FAILURE_RETRY(mkdir(dir_name, 0777));
368 // If the directory already exists, treat it as a success. 368 // If the directory already exists, treat it as a success.
369 if (result == -1 && errno == EEXIST) { 369 if (result == -1 && errno == EEXIST) {
370 return (Exists(dir_name) == EXISTS); 370 return (Exists(dir_name) == EXISTS);
371 } 371 }
372 return (result == 0); 372 return (result == 0);
373 } 373 }
374 374
375 375
376 char* Directory::CreateTemp(const char* const_template, bool system) { 376 char* Directory::SystemTemp() {
377 // Returns a new, unused directory name, modifying the contents of 377 const char* temp_dir = getenv("TMPDIR");
378 // dir_template. Creates the directory with the permissions specified 378 if (temp_dir == NULL) {
379 temp_dir = getenv("TMP");
380 }
381 if (temp_dir == NULL) {
382 temp_dir = "/tmp";
383 }
384 return strdup(temp_dir);
385 }
386
387
388 char* Directory::CreateTemp(const char* prefix) {
389 // Returns a new, unused directory name, adding characters to the end
390 // of prefix. Creates the directory with the permissions specified
379 // by the process umask. 391 // by the process umask.
380 // The return value must be freed by the caller. 392 // The return value must be freed by the caller.
381 PathBuffer path; 393 PathBuffer path;
382 if (system) { 394 path.Add(prefix);
383 const char* temp_dir = getenv("TMPDIR");
384 if (temp_dir == NULL) {
385 temp_dir = getenv("TMP");
386 }
387 if (temp_dir != NULL) {
388 path.Add(temp_dir);
389 if (temp_dir[strlen(temp_dir) - 1] != '/') {
390 path.Add("/");
391 }
392 } else {
393 path.Add("/tmp/");
394 }
395 }
396 path.Add(const_template);
397 if (!path.Add("XXXXXX")) { 395 if (!path.Add("XXXXXX")) {
398 // Pattern has overflowed. 396 // Pattern has overflowed.
399 return NULL; 397 return NULL;
400 } 398 }
401 char* result; 399 char* result;
402 do { 400 do {
403 result = mkdtemp(path.AsString()); 401 result = mkdtemp(path.AsString());
404 } while (result == NULL && errno == EINTR); 402 } while (result == NULL && errno == EINTR);
405 if (result == NULL) { 403 if (result == NULL) {
406 return NULL; 404 return NULL;
407 } 405 }
408 int length = strnlen(path.AsString(), PATH_MAX); 406 return strdup(result);
409 result = static_cast<char*>(malloc(length + 1));
410 strncpy(result, path.AsString(), length);
411 result[length] = '\0';
412 return result;
413 } 407 }
414 408
415 409
416 bool Directory::Delete(const char* dir_name, bool recursive) { 410 bool Directory::Delete(const char* dir_name, bool recursive) {
417 if (!recursive) { 411 if (!recursive) {
418 if (File::GetType(dir_name, false) == File::kIsLink && 412 if (File::GetType(dir_name, false) == File::kIsLink &&
419 File::GetType(dir_name, true) == File::kIsDirectory) { 413 File::GetType(dir_name, true) == File::kIsDirectory) {
420 return (TEMP_FAILURE_RETRY(unlink(dir_name)) == 0); 414 return (TEMP_FAILURE_RETRY(unlink(dir_name)) == 0);
421 } 415 }
422 return (TEMP_FAILURE_RETRY(rmdir(dir_name)) == 0); 416 return (TEMP_FAILURE_RETRY(rmdir(dir_name)) == 0);
(...skipping 10 matching lines...) Expand all
433 bool Directory::Rename(const char* path, const char* new_path) { 427 bool Directory::Rename(const char* path, const char* new_path) {
434 ExistsResult exists = Exists(path); 428 ExistsResult exists = Exists(path);
435 if (exists != EXISTS) return false; 429 if (exists != EXISTS) return false;
436 return (TEMP_FAILURE_RETRY(rename(path, new_path)) == 0); 430 return (TEMP_FAILURE_RETRY(rename(path, new_path)) == 0);
437 } 431 }
438 432
439 } // namespace bin 433 } // namespace bin
440 } // namespace dart 434 } // namespace dart
441 435
442 #endif // defined(TARGET_OS_LINUX) 436 #endif // defined(TARGET_OS_LINUX)
OLDNEW
« no previous file with comments | « runtime/bin/directory_android.cc ('k') | runtime/bin/directory_macos.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698