OLD | NEW |
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 <libgen.h> | 10 #include <libgen.h> |
(...skipping 318 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
329 } | 329 } |
330 | 330 |
331 bool DirectoryExists(const FilePath& path) { | 331 bool DirectoryExists(const FilePath& path) { |
332 ThreadRestrictions::AssertIOAllowed(); | 332 ThreadRestrictions::AssertIOAllowed(); |
333 stat_wrapper_t file_info; | 333 stat_wrapper_t file_info; |
334 if (CallStat(path.value().c_str(), &file_info) == 0) | 334 if (CallStat(path.value().c_str(), &file_info) == 0) |
335 return S_ISDIR(file_info.st_mode); | 335 return S_ISDIR(file_info.st_mode); |
336 return false; | 336 return false; |
337 } | 337 } |
338 | 338 |
339 } // namespace base | |
340 | |
341 // ----------------------------------------------------------------------------- | |
342 | |
343 namespace file_util { | |
344 | |
345 using base::stat_wrapper_t; | |
346 using base::CallStat; | |
347 using base::CallLstat; | |
348 using base::DirectoryExists; | |
349 using base::FileEnumerator; | |
350 using base::FilePath; | |
351 using base::MakeAbsoluteFilePath; | |
352 using base::RealPath; | |
353 using base::VerifySpecificPathControlledByUser; | |
354 | |
355 bool ReadFromFD(int fd, char* buffer, size_t bytes) { | 339 bool ReadFromFD(int fd, char* buffer, size_t bytes) { |
356 size_t total_read = 0; | 340 size_t total_read = 0; |
357 while (total_read < bytes) { | 341 while (total_read < bytes) { |
358 ssize_t bytes_read = | 342 ssize_t bytes_read = |
359 HANDLE_EINTR(read(fd, buffer + total_read, bytes - total_read)); | 343 HANDLE_EINTR(read(fd, buffer + total_read, bytes - total_read)); |
360 if (bytes_read <= 0) | 344 if (bytes_read <= 0) |
361 break; | 345 break; |
362 total_read += bytes_read; | 346 total_read += bytes_read; |
363 } | 347 } |
364 return total_read == bytes; | 348 return total_read == bytes; |
365 } | 349 } |
366 | 350 |
367 bool CreateSymbolicLink(const FilePath& target_path, | 351 bool CreateSymbolicLink(const FilePath& target_path, |
368 const FilePath& symlink_path) { | 352 const FilePath& symlink_path) { |
369 DCHECK(!symlink_path.empty()); | 353 DCHECK(!symlink_path.empty()); |
370 DCHECK(!target_path.empty()); | 354 DCHECK(!target_path.empty()); |
371 return ::symlink(target_path.value().c_str(), | 355 return ::symlink(target_path.value().c_str(), |
372 symlink_path.value().c_str()) != -1; | 356 symlink_path.value().c_str()) != -1; |
373 } | 357 } |
374 | 358 |
375 bool ReadSymbolicLink(const FilePath& symlink_path, | 359 bool ReadSymbolicLink(const FilePath& symlink_path, FilePath* target_path) { |
376 FilePath* target_path) { | |
377 DCHECK(!symlink_path.empty()); | 360 DCHECK(!symlink_path.empty()); |
378 DCHECK(target_path); | 361 DCHECK(target_path); |
379 char buf[PATH_MAX]; | 362 char buf[PATH_MAX]; |
380 ssize_t count = ::readlink(symlink_path.value().c_str(), buf, arraysize(buf)); | 363 ssize_t count = ::readlink(symlink_path.value().c_str(), buf, arraysize(buf)); |
381 | 364 |
382 if (count <= 0) { | 365 if (count <= 0) { |
383 target_path->clear(); | 366 target_path->clear(); |
384 return false; | 367 return false; |
385 } | 368 } |
386 | 369 |
387 *target_path = FilePath(FilePath::StringType(buf, count)); | 370 *target_path = FilePath(FilePath::StringType(buf, count)); |
388 return true; | 371 return true; |
389 } | 372 } |
390 | 373 |
391 bool GetPosixFilePermissions(const FilePath& path, int* mode) { | 374 bool GetPosixFilePermissions(const FilePath& path, int* mode) { |
392 base::ThreadRestrictions::AssertIOAllowed(); | 375 ThreadRestrictions::AssertIOAllowed(); |
393 DCHECK(mode); | 376 DCHECK(mode); |
394 | 377 |
395 stat_wrapper_t file_info; | 378 stat_wrapper_t file_info; |
396 // Uses stat(), because on symbolic link, lstat() does not return valid | 379 // Uses stat(), because on symbolic link, lstat() does not return valid |
397 // permission bits in st_mode | 380 // permission bits in st_mode |
398 if (CallStat(path.value().c_str(), &file_info) != 0) | 381 if (CallStat(path.value().c_str(), &file_info) != 0) |
399 return false; | 382 return false; |
400 | 383 |
401 *mode = file_info.st_mode & FILE_PERMISSION_MASK; | 384 *mode = file_info.st_mode & FILE_PERMISSION_MASK; |
402 return true; | 385 return true; |
403 } | 386 } |
404 | 387 |
405 bool SetPosixFilePermissions(const FilePath& path, | 388 bool SetPosixFilePermissions(const FilePath& path, |
406 int mode) { | 389 int mode) { |
407 base::ThreadRestrictions::AssertIOAllowed(); | 390 ThreadRestrictions::AssertIOAllowed(); |
408 DCHECK((mode & ~FILE_PERMISSION_MASK) == 0); | 391 DCHECK((mode & ~FILE_PERMISSION_MASK) == 0); |
409 | 392 |
410 // Calls stat() so that we can preserve the higher bits like S_ISGID. | 393 // Calls stat() so that we can preserve the higher bits like S_ISGID. |
411 stat_wrapper_t stat_buf; | 394 stat_wrapper_t stat_buf; |
412 if (CallStat(path.value().c_str(), &stat_buf) != 0) | 395 if (CallStat(path.value().c_str(), &stat_buf) != 0) |
413 return false; | 396 return false; |
414 | 397 |
415 // Clears the existing permission bits, and adds the new ones. | 398 // Clears the existing permission bits, and adds the new ones. |
416 mode_t updated_mode_bits = stat_buf.st_mode & ~FILE_PERMISSION_MASK; | 399 mode_t updated_mode_bits = stat_buf.st_mode & ~FILE_PERMISSION_MASK; |
417 updated_mode_bits |= mode & FILE_PERMISSION_MASK; | 400 updated_mode_bits |= mode & FILE_PERMISSION_MASK; |
418 | 401 |
419 if (HANDLE_EINTR(chmod(path.value().c_str(), updated_mode_bits)) != 0) | 402 if (HANDLE_EINTR(chmod(path.value().c_str(), updated_mode_bits)) != 0) |
420 return false; | 403 return false; |
421 | 404 |
422 return true; | 405 return true; |
423 } | 406 } |
424 | 407 |
| 408 } // namespace base |
| 409 |
| 410 // ----------------------------------------------------------------------------- |
| 411 |
| 412 namespace file_util { |
| 413 |
| 414 using base::stat_wrapper_t; |
| 415 using base::CallStat; |
| 416 using base::CallLstat; |
| 417 using base::DirectoryExists; |
| 418 using base::FileEnumerator; |
| 419 using base::FilePath; |
| 420 using base::MakeAbsoluteFilePath; |
| 421 using base::RealPath; |
| 422 using base::VerifySpecificPathControlledByUser; |
| 423 |
425 // Creates and opens a temporary file in |directory|, returning the | 424 // Creates and opens a temporary file in |directory|, returning the |
426 // file descriptor. |path| is set to the temporary file path. | 425 // file descriptor. |path| is set to the temporary file path. |
427 // This function does NOT unlink() the file. | 426 // This function does NOT unlink() the file. |
428 int CreateAndOpenFdForTemporaryFile(FilePath directory, FilePath* path) { | 427 int CreateAndOpenFdForTemporaryFile(FilePath directory, FilePath* path) { |
429 base::ThreadRestrictions::AssertIOAllowed(); // For call to mkstemp(). | 428 base::ThreadRestrictions::AssertIOAllowed(); // For call to mkstemp(). |
430 *path = directory.Append(base::TempFileName()); | 429 *path = directory.Append(base::TempFileName()); |
431 const std::string& tmpdir_string = path->value(); | 430 const std::string& tmpdir_string = path->value(); |
432 // this should be OK since mkstemp just replaces characters in place | 431 // this should be OK since mkstemp just replaces characters in place |
433 char* buffer = const_cast<char*>(tmpdir_string.c_str()); | 432 char* buffer = const_cast<char*>(tmpdir_string.c_str()); |
434 | 433 |
(...skipping 536 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
971 result = false; | 970 result = false; |
972 if (HANDLE_EINTR(close(outfile)) < 0) | 971 if (HANDLE_EINTR(close(outfile)) < 0) |
973 result = false; | 972 result = false; |
974 | 973 |
975 return result; | 974 return result; |
976 } | 975 } |
977 #endif // !defined(OS_MACOSX) | 976 #endif // !defined(OS_MACOSX) |
978 | 977 |
979 } // namespace internal | 978 } // namespace internal |
980 } // namespace base | 979 } // namespace base |
OLD | NEW |