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

Side by Side Diff: base/file_util_posix.cc

Issue 10756020: Add the methods to change and get a posix file permission to file_util. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Review fix Created 8 years, 5 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 | « base/file_util.h ('k') | base/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/file_util.h" 5 #include "base/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 <fnmatch.h> 10 #include <fnmatch.h>
(...skipping 439 matching lines...) Expand 10 before | Expand all | Expand 10 after
450 450
451 if (count <= 0) { 451 if (count <= 0) {
452 target_path->clear(); 452 target_path->clear();
453 return false; 453 return false;
454 } 454 }
455 455
456 *target_path = FilePath(FilePath::StringType(buf, count)); 456 *target_path = FilePath(FilePath::StringType(buf, count));
457 return true; 457 return true;
458 } 458 }
459 459
460 bool GetPosixFilePermissions(const FilePath& path, int* mode) {
461 base::ThreadRestrictions::AssertIOAllowed();
462 DCHECK(mode);
463
464 stat_wrapper_t file_info;
465 // Uses stat(), because on symbolic link, lstat() does not return valid
466 // permission bits in st_mode
467 if (CallStat(path.value().c_str(), &file_info) != 0)
468 return false;
469
470 *mode = file_info.st_mode & FILE_PERMISSION_MASK;
471 return true;
472 }
473
474 bool SetPosixFilePermissions(const FilePath& path,
475 int mode) {
476 base::ThreadRestrictions::AssertIOAllowed();
477 DCHECK((mode & ~FILE_PERMISSION_MASK) == 0);
478
479 // Calls stat() so that we can preserve the higher bits like S_ISGID.
480 stat_wrapper_t stat_buf;
481 if (CallStat(path.value().c_str(), &stat_buf) != 0)
482 return false;
483
484 // Clears the existing permission bits, and adds the new ones.
485 mode_t updated_mode_bits = stat_buf.st_mode & ~FILE_PERMISSION_MASK;
486 updated_mode_bits |= mode & FILE_PERMISSION_MASK;
487
488 if (HANDLE_EINTR(chmod(path.value().c_str(), updated_mode_bits)) != 0)
489 return false;
490
491 return true;
492 }
493
460 // Creates and opens a temporary file in |directory|, returning the 494 // Creates and opens a temporary file in |directory|, returning the
461 // file descriptor. |path| is set to the temporary file path. 495 // file descriptor. |path| is set to the temporary file path.
462 // This function does NOT unlink() the file. 496 // This function does NOT unlink() the file.
463 int CreateAndOpenFdForTemporaryFile(FilePath directory, FilePath* path) { 497 int CreateAndOpenFdForTemporaryFile(FilePath directory, FilePath* path) {
464 base::ThreadRestrictions::AssertIOAllowed(); // For call to mkstemp(). 498 base::ThreadRestrictions::AssertIOAllowed(); // For call to mkstemp().
465 *path = directory.Append(TempFileName()); 499 *path = directory.Append(TempFileName());
466 const std::string& tmpdir_string = path->value(); 500 const std::string& tmpdir_string = path->value();
467 // this should be OK since mkstemp just replaces characters in place 501 // this should be OK since mkstemp just replaces characters in place
468 char* buffer = const_cast<char*>(tmpdir_string.c_str()); 502 char* buffer = const_cast<char*>(tmpdir_string.c_str());
469 503
(...skipping 645 matching lines...) Expand 10 before | Expand all | Expand 10 after
1115 1149
1116 allowed_group_ids.insert(group_record->gr_gid); 1150 allowed_group_ids.insert(group_record->gr_gid);
1117 } 1151 }
1118 1152
1119 return VerifyPathControlledByUser( 1153 return VerifyPathControlledByUser(
1120 kFileSystemRoot, path, kRootUid, allowed_group_ids); 1154 kFileSystemRoot, path, kRootUid, allowed_group_ids);
1121 } 1155 }
1122 #endif // defined(OS_MACOSX) 1156 #endif // defined(OS_MACOSX)
1123 1157
1124 } // namespace file_util 1158 } // namespace file_util
OLDNEW
« no previous file with comments | « base/file_util.h ('k') | base/file_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698