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 395 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
406 struct ScopedFILEClose { | 406 struct ScopedFILEClose { |
407 inline void operator()(FILE* x) const { | 407 inline void operator()(FILE* x) const { |
408 if (x) | 408 if (x) |
409 fclose(x); | 409 fclose(x); |
410 } | 410 } |
411 }; | 411 }; |
412 | 412 |
413 // Automatically closes |FILE*|s. | 413 // Automatically closes |FILE*|s. |
414 typedef scoped_ptr<FILE, ScopedFILEClose> ScopedFILE; | 414 typedef scoped_ptr<FILE, ScopedFILEClose> ScopedFILE; |
415 | 415 |
416 #if defined(OS_POSIX) | |
417 // Functor for |ScopedFD| (below). | |
418 struct ScopedFDClose { | |
419 inline void operator()(int* x) const { | |
420 if (x && *x >= 0) { | |
421 // It's important to crash here. | |
422 // There are security implications to not closing a file descriptor | |
423 // properly. As file descriptors are "capabilities", keeping them open | |
424 // would make the current process keep access to a resource. Much of | |
425 // Chrome relies on being able to "drop" such access. | |
426 // It's especially problematic on Linux with the setuid sandbox, where | |
427 // a single open directory would bypass the entire security model. | |
428 PCHECK(0 == IGNORE_EINTR(close(*x))); | |
429 } | |
430 } | |
431 }; | |
432 | |
433 // Automatically closes FDs (note: doesn't store the FD). | |
434 // TODO(viettrungluu): This is a very odd API, since (unlike |FILE*|s, you'll | |
435 // need to store the FD separately and keep its memory alive). This should | |
436 // probably be called |ScopedFDCloser| or something like that. | |
437 typedef scoped_ptr<int, ScopedFDClose> ScopedFD; | |
438 // Let new users use ScopedFDCloser already, while ScopedFD is replaced. | |
439 typedef ScopedFD ScopedFDCloser; | |
440 #endif // OS_POSIX | |
441 | |
442 #if defined(OS_LINUX) | 416 #if defined(OS_LINUX) |
443 // Broad categories of file systems as returned by statfs() on Linux. | 417 // Broad categories of file systems as returned by statfs() on Linux. |
444 enum FileSystemType { | 418 enum FileSystemType { |
445 FILE_SYSTEM_UNKNOWN, // statfs failed. | 419 FILE_SYSTEM_UNKNOWN, // statfs failed. |
446 FILE_SYSTEM_0, // statfs.f_type == 0 means unknown, may indicate AFS. | 420 FILE_SYSTEM_0, // statfs.f_type == 0 means unknown, may indicate AFS. |
447 FILE_SYSTEM_ORDINARY, // on-disk filesystem like ext2 | 421 FILE_SYSTEM_ORDINARY, // on-disk filesystem like ext2 |
448 FILE_SYSTEM_NFS, | 422 FILE_SYSTEM_NFS, |
449 FILE_SYSTEM_SMB, | 423 FILE_SYSTEM_SMB, |
450 FILE_SYSTEM_CODA, | 424 FILE_SYSTEM_CODA, |
451 FILE_SYSTEM_MEMORY, // in-memory file system | 425 FILE_SYSTEM_MEMORY, // in-memory file system |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
483 // This function simulates Move(), but unlike Move() it works across volumes. | 457 // This function simulates Move(), but unlike Move() it works across volumes. |
484 // This function is not transactional. | 458 // This function is not transactional. |
485 BASE_EXPORT bool CopyAndDeleteDirectory(const FilePath& from_path, | 459 BASE_EXPORT bool CopyAndDeleteDirectory(const FilePath& from_path, |
486 const FilePath& to_path); | 460 const FilePath& to_path); |
487 #endif // defined(OS_WIN) | 461 #endif // defined(OS_WIN) |
488 | 462 |
489 } // namespace internal | 463 } // namespace internal |
490 } // namespace base | 464 } // namespace base |
491 | 465 |
492 #endif // BASE_FILE_UTIL_H_ | 466 #endif // BASE_FILE_UTIL_H_ |
OLD | NEW |