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

Side by Side Diff: base/file_util_posix.cc

Issue 10696069: 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
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() may not return valid
466 // information in the permission bits 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 stat_wrapper_t stat_buf;
480 if (CallStat(path.value().c_str(), &stat_buf) != 0)
481 return false;
482
483 mode_t updated_mode_bits = stat_buf.st_mode & ~FILE_PERMISSION_MASK;
484 updated_mode_bits |= mode & FILE_PERMISSION_MASK;
485
486 if (HANDLE_EINTR(chmod(path.value().c_str(), updated_mode_bits)) != 0)
487 return false;
488
489 return true;
490 }
491
492 bool ChangePosixFilePermissions(const FilePath& path,
493 int mode_bits_to_set,
494 int mode_bits_to_clear) {
495 base::ThreadRestrictions::AssertIOAllowed();
496 DCHECK((mode_bits_to_set & ~FILE_PERMISSION_MASK) == 0);
497 DCHECK((mode_bits_to_clear & ~FILE_PERMISSION_MASK) == 0);
498 DCHECK(!(mode_bits_to_set & mode_bits_to_clear))
499 << "Can't set and clear the same bits.";
500
501 int mode;
502 if (!GetPosixFilePermissions(path, &mode))
503 return false;
504
505 mode |= mode_bits_to_set;
506 mode &= ~mode_bits_to_clear;
507
508 if (SetPosixFilePermissions(path, mode))
509 return false;
510
511 return true;
512 }
513
460 // Creates and opens a temporary file in |directory|, returning the 514 // Creates and opens a temporary file in |directory|, returning the
461 // file descriptor. |path| is set to the temporary file path. 515 // file descriptor. |path| is set to the temporary file path.
462 // This function does NOT unlink() the file. 516 // This function does NOT unlink() the file.
463 int CreateAndOpenFdForTemporaryFile(FilePath directory, FilePath* path) { 517 int CreateAndOpenFdForTemporaryFile(FilePath directory, FilePath* path) {
464 base::ThreadRestrictions::AssertIOAllowed(); // For call to mkstemp(). 518 base::ThreadRestrictions::AssertIOAllowed(); // For call to mkstemp().
465 *path = directory.Append(TempFileName()); 519 *path = directory.Append(TempFileName());
466 const std::string& tmpdir_string = path->value(); 520 const std::string& tmpdir_string = path->value();
467 // this should be OK since mkstemp just replaces characters in place 521 // this should be OK since mkstemp just replaces characters in place
468 char* buffer = const_cast<char*>(tmpdir_string.c_str()); 522 char* buffer = const_cast<char*>(tmpdir_string.c_str());
469 523
(...skipping 645 matching lines...) Expand 10 before | Expand all | Expand 10 after
1115 1169
1116 allowed_group_ids.insert(group_record->gr_gid); 1170 allowed_group_ids.insert(group_record->gr_gid);
1117 } 1171 }
1118 1172
1119 return VerifyPathControlledByUser( 1173 return VerifyPathControlledByUser(
1120 kFileSystemRoot, path, kRootUid, allowed_group_ids); 1174 kFileSystemRoot, path, kRootUid, allowed_group_ids);
1121 } 1175 }
1122 #endif // defined(OS_MACOSX) 1176 #endif // defined(OS_MACOSX)
1123 1177
1124 } // namespace file_util 1178 } // namespace file_util
OLDNEW
« base/file_util.h ('K') | « 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