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 // This file contains utility functions for dealing with the local | 5 // This file contains utility functions for dealing with the local |
6 // filesystem. | 6 // filesystem. |
7 | 7 |
8 #ifndef BASE_FILE_UTIL_H_ | 8 #ifndef BASE_FILE_UTIL_H_ |
9 #define BASE_FILE_UTIL_H_ | 9 #define BASE_FILE_UTIL_H_ |
10 | 10 |
(...skipping 408 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
419 struct ScopedFILEClose { | 419 struct ScopedFILEClose { |
420 inline void operator()(FILE* x) const { | 420 inline void operator()(FILE* x) const { |
421 if (x) | 421 if (x) |
422 fclose(x); | 422 fclose(x); |
423 } | 423 } |
424 }; | 424 }; |
425 | 425 |
426 // Automatically closes |FILE*|s. | 426 // Automatically closes |FILE*|s. |
427 typedef scoped_ptr<FILE, ScopedFILEClose> ScopedFILE; | 427 typedef scoped_ptr<FILE, ScopedFILEClose> ScopedFILE; |
428 | 428 |
| 429 #if defined(OS_POSIX) |
| 430 // Functor for |ScopedFD| (below). |
| 431 struct ScopedFDClose { |
| 432 inline void operator()(int* x) const { |
| 433 if (x && *x >= 0) { |
| 434 // It's important to crash here. |
| 435 // There are security implications to not closing a file descriptor |
| 436 // properly. As file descriptors are "capabilities", keeping them open |
| 437 // would make the current process keep access to a resource. Much of |
| 438 // Chrome relies on being able to "drop" such access. |
| 439 // It's especially problematic on Linux with the setuid sandbox, where |
| 440 // a single open directory would bypass the entire security model. |
| 441 PCHECK(0 == IGNORE_EINTR(close(*x))); |
| 442 } |
| 443 } |
| 444 }; |
| 445 |
| 446 // Automatically closes FDs (note: doesn't store the FD). |
| 447 // TODO(viettrungluu): This is a very odd API, since (unlike |FILE*|s, you'll |
| 448 // need to store the FD separately and keep its memory alive). This should |
| 449 // probably be called |ScopedFDCloser| or something like that. |
| 450 typedef scoped_ptr<int, ScopedFDClose> ScopedFD; |
| 451 // Let new users use ScopedFDCloser already, while ScopedFD is replaced. |
| 452 typedef ScopedFD ScopedFDCloser; |
| 453 #endif // OS_POSIX |
| 454 |
429 } // namespace file_util | 455 } // namespace file_util |
430 | 456 |
431 // Internal -------------------------------------------------------------------- | 457 // Internal -------------------------------------------------------------------- |
432 | 458 |
433 namespace base { | 459 namespace base { |
434 namespace internal { | 460 namespace internal { |
435 | 461 |
436 // Same as Move but allows paths with traversal components. | 462 // Same as Move but allows paths with traversal components. |
437 // Use only with extreme care. | 463 // Use only with extreme care. |
438 BASE_EXPORT bool MoveUnsafe(const FilePath& from_path, | 464 BASE_EXPORT bool MoveUnsafe(const FilePath& from_path, |
(...skipping 10 matching lines...) Expand all Loading... |
449 // This function simulates Move(), but unlike Move() it works across volumes. | 475 // This function simulates Move(), but unlike Move() it works across volumes. |
450 // This function is not transactional. | 476 // This function is not transactional. |
451 BASE_EXPORT bool CopyAndDeleteDirectory(const FilePath& from_path, | 477 BASE_EXPORT bool CopyAndDeleteDirectory(const FilePath& from_path, |
452 const FilePath& to_path); | 478 const FilePath& to_path); |
453 #endif // defined(OS_WIN) | 479 #endif // defined(OS_WIN) |
454 | 480 |
455 } // namespace internal | 481 } // namespace internal |
456 } // namespace base | 482 } // namespace base |
457 | 483 |
458 #endif // BASE_FILE_UTIL_H_ | 484 #endif // BASE_FILE_UTIL_H_ |
OLD | NEW |