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

Side by Side Diff: base/files/file_util_posix.cc

Issue 2086103002: Add base::ExecutableExistsInPath (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Don't leak base::Environment Created 4 years, 6 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
« no previous file with comments | « base/files/file_util.h ('k') | base/files/file_util_unittest.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 Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/files/file_util.h" 5 #include "base/files/file_util.h"
6 6
7 #include <dirent.h> 7 #include <dirent.h>
8 #include <errno.h> 8 #include <errno.h>
9 #include <fcntl.h> 9 #include <fcntl.h>
10 #include <libgen.h> 10 #include <libgen.h>
11 #include <limits.h> 11 #include <limits.h>
12 #include <stddef.h> 12 #include <stddef.h>
13 #include <stdio.h> 13 #include <stdio.h>
14 #include <stdlib.h> 14 #include <stdlib.h>
15 #include <string.h> 15 #include <string.h>
16 #include <sys/errno.h> 16 #include <sys/errno.h>
17 #include <sys/mman.h> 17 #include <sys/mman.h>
18 #include <sys/param.h> 18 #include <sys/param.h>
19 #include <sys/stat.h> 19 #include <sys/stat.h>
20 #include <sys/time.h> 20 #include <sys/time.h>
21 #include <sys/types.h> 21 #include <sys/types.h>
22 #include <time.h> 22 #include <time.h>
23 #include <unistd.h> 23 #include <unistd.h>
24 24
25 #include "base/environment.h"
25 #include "base/files/file_enumerator.h" 26 #include "base/files/file_enumerator.h"
26 #include "base/files/file_path.h" 27 #include "base/files/file_path.h"
27 #include "base/files/scoped_file.h" 28 #include "base/files/scoped_file.h"
28 #include "base/logging.h" 29 #include "base/logging.h"
29 #include "base/macros.h" 30 #include "base/macros.h"
30 #include "base/memory/singleton.h" 31 #include "base/memory/singleton.h"
31 #include "base/path_service.h" 32 #include "base/path_service.h"
32 #include "base/posix/eintr_wrapper.h" 33 #include "base/posix/eintr_wrapper.h"
33 #include "base/stl_util.h" 34 #include "base/stl_util.h"
35 #include "base/strings/string_split.h"
34 #include "base/strings/string_util.h" 36 #include "base/strings/string_util.h"
35 #include "base/strings/stringprintf.h" 37 #include "base/strings/stringprintf.h"
36 #include "base/strings/sys_string_conversions.h" 38 #include "base/strings/sys_string_conversions.h"
37 #include "base/strings/utf_string_conversions.h" 39 #include "base/strings/utf_string_conversions.h"
38 #include "base/sys_info.h" 40 #include "base/sys_info.h"
39 #include "base/threading/thread_restrictions.h" 41 #include "base/threading/thread_restrictions.h"
40 #include "base/time/time.h" 42 #include "base/time/time.h"
41 #include "build/build_config.h" 43 #include "build/build_config.h"
42 44
43 #if defined(OS_MACOSX) 45 #if defined(OS_MACOSX)
(...skipping 404 matching lines...) Expand 10 before | Expand all | Expand 10 after
448 // Clears the existing permission bits, and adds the new ones. 450 // Clears the existing permission bits, and adds the new ones.
449 mode_t updated_mode_bits = stat_buf.st_mode & ~FILE_PERMISSION_MASK; 451 mode_t updated_mode_bits = stat_buf.st_mode & ~FILE_PERMISSION_MASK;
450 updated_mode_bits |= mode & FILE_PERMISSION_MASK; 452 updated_mode_bits |= mode & FILE_PERMISSION_MASK;
451 453
452 if (HANDLE_EINTR(chmod(path.value().c_str(), updated_mode_bits)) != 0) 454 if (HANDLE_EINTR(chmod(path.value().c_str(), updated_mode_bits)) != 0)
453 return false; 455 return false;
454 456
455 return true; 457 return true;
456 } 458 }
457 459
460 bool ExecutableExistsInPath(Environment* env,
461 const FilePath::StringType& executable) {
462 std::string path;
463 if (!env->GetVar("PATH", &path)) {
464 LOG(ERROR) << "No $PATH variable. Assuming no " << executable << ".";
465 return false;
466 }
467
468 for (const StringPiece& cur_path :
469 SplitStringPiece(path, ":", KEEP_WHITESPACE, SPLIT_WANT_NONEMPTY)) {
470 FilePath file(cur_path);
471 int permissions;
472 if (GetPosixFilePermissions(file.Append(executable), &permissions) &&
473 (permissions & FILE_PERMISSION_EXECUTE_BY_USER))
474 return true;
475 }
476 return false;
477 }
478
458 #if !defined(OS_MACOSX) 479 #if !defined(OS_MACOSX)
459 // This is implemented in file_util_mac.mm for Mac. 480 // This is implemented in file_util_mac.mm for Mac.
460 bool GetTempDir(FilePath* path) { 481 bool GetTempDir(FilePath* path) {
461 const char* tmp = getenv("TMPDIR"); 482 const char* tmp = getenv("TMPDIR");
462 if (tmp) { 483 if (tmp) {
463 *path = FilePath(tmp); 484 *path = FilePath(tmp);
464 } else { 485 } else {
465 #if defined(OS_ANDROID) 486 #if defined(OS_ANDROID)
466 return PathService::Get(base::DIR_CACHE, path); 487 return PathService::Get(base::DIR_CACHE, path);
467 #else 488 #else
(...skipping 458 matching lines...) Expand 10 before | Expand all | Expand 10 after
926 return false; 947 return false;
927 948
928 DeleteFile(from_path, true); 949 DeleteFile(from_path, true);
929 return true; 950 return true;
930 } 951 }
931 952
932 } // namespace internal 953 } // namespace internal
933 954
934 #endif // !defined(OS_NACL_NONSFI) 955 #endif // !defined(OS_NACL_NONSFI)
935 } // namespace base 956 } // namespace base
OLDNEW
« no previous file with comments | « base/files/file_util.h ('k') | base/files/file_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698