| 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 331 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 342 // or -1 on error. | 342 // or -1 on error. |
| 343 BASE_EXPORT int AppendToFile(const FilePath& filename, | 343 BASE_EXPORT int AppendToFile(const FilePath& filename, |
| 344 const char* data, int size); | 344 const char* data, int size); |
| 345 | 345 |
| 346 // Gets the current working directory for the process. | 346 // Gets the current working directory for the process. |
| 347 BASE_EXPORT bool GetCurrentDirectory(FilePath* path); | 347 BASE_EXPORT bool GetCurrentDirectory(FilePath* path); |
| 348 | 348 |
| 349 // Sets the current working directory for the process. | 349 // Sets the current working directory for the process. |
| 350 BASE_EXPORT bool SetCurrentDirectory(const FilePath& path); | 350 BASE_EXPORT bool SetCurrentDirectory(const FilePath& path); |
| 351 | 351 |
| 352 } // namespace base | |
| 353 | |
| 354 // ----------------------------------------------------------------------------- | |
| 355 | |
| 356 namespace file_util { | |
| 357 | |
| 358 // Attempts to find a number that can be appended to the |path| to make it | 352 // Attempts to find a number that can be appended to the |path| to make it |
| 359 // unique. If |path| does not exist, 0 is returned. If it fails to find such | 353 // unique. If |path| does not exist, 0 is returned. If it fails to find such |
| 360 // a number, -1 is returned. If |suffix| is not empty, also checks the | 354 // a number, -1 is returned. If |suffix| is not empty, also checks the |
| 361 // existence of it with the given suffix. | 355 // existence of it with the given suffix. |
| 362 BASE_EXPORT int GetUniquePathNumber(const base::FilePath& path, | 356 BASE_EXPORT int GetUniquePathNumber(const FilePath& path, |
| 363 const base::FilePath::StringType& suffix); | 357 const FilePath::StringType& suffix); |
| 364 | |
| 365 #if defined(OS_POSIX) | |
| 366 // Creates a directory with a guaranteed unique name based on |path|, returning | |
| 367 // the pathname if successful, or an empty path if there was an error creating | |
| 368 // the directory. Does not create parent directories. | |
| 369 BASE_EXPORT base::FilePath MakeUniqueDirectory(const base::FilePath& path); | |
| 370 #endif | |
| 371 | 358 |
| 372 #if defined(OS_POSIX) | 359 #if defined(OS_POSIX) |
| 373 // Test that |path| can only be changed by a given user and members of | 360 // Test that |path| can only be changed by a given user and members of |
| 374 // a given set of groups. | 361 // a given set of groups. |
| 375 // Specifically, test that all parts of |path| under (and including) |base|: | 362 // Specifically, test that all parts of |path| under (and including) |base|: |
| 376 // * Exist. | 363 // * Exist. |
| 377 // * Are owned by a specific user. | 364 // * Are owned by a specific user. |
| 378 // * Are not writable by all users. | 365 // * Are not writable by all users. |
| 379 // * Are owned by a member of a given set of groups, or are not writable by | 366 // * Are owned by a member of a given set of groups, or are not writable by |
| 380 // their group. | 367 // their group. |
| (...skipping 14 matching lines...) Expand all Loading... |
| 395 // the filesystem, are owned by the superuser, controlled by the group | 382 // the filesystem, are owned by the superuser, controlled by the group |
| 396 // "admin", are not writable by all users, and contain no symbolic links. | 383 // "admin", are not writable by all users, and contain no symbolic links. |
| 397 // Will return false if |path| does not exist. | 384 // Will return false if |path| does not exist. |
| 398 BASE_EXPORT bool VerifyPathControlledByAdmin(const base::FilePath& path); | 385 BASE_EXPORT bool VerifyPathControlledByAdmin(const base::FilePath& path); |
| 399 #endif // defined(OS_MACOSX) && !defined(OS_IOS) | 386 #endif // defined(OS_MACOSX) && !defined(OS_IOS) |
| 400 | 387 |
| 401 // Returns the maximum length of path component on the volume containing | 388 // Returns the maximum length of path component on the volume containing |
| 402 // the directory |path|, in the number of FilePath::CharType, or -1 on failure. | 389 // the directory |path|, in the number of FilePath::CharType, or -1 on failure. |
| 403 BASE_EXPORT int GetMaximumPathComponentLength(const base::FilePath& path); | 390 BASE_EXPORT int GetMaximumPathComponentLength(const base::FilePath& path); |
| 404 | 391 |
| 392 #if defined(OS_LINUX) |
| 393 // Broad categories of file systems as returned by statfs() on Linux. |
| 394 enum FileSystemType { |
| 395 FILE_SYSTEM_UNKNOWN, // statfs failed. |
| 396 FILE_SYSTEM_0, // statfs.f_type == 0 means unknown, may indicate AFS. |
| 397 FILE_SYSTEM_ORDINARY, // on-disk filesystem like ext2 |
| 398 FILE_SYSTEM_NFS, |
| 399 FILE_SYSTEM_SMB, |
| 400 FILE_SYSTEM_CODA, |
| 401 FILE_SYSTEM_MEMORY, // in-memory file system |
| 402 FILE_SYSTEM_CGROUP, // cgroup control. |
| 403 FILE_SYSTEM_OTHER, // any other value. |
| 404 FILE_SYSTEM_TYPE_COUNT |
| 405 }; |
| 406 |
| 407 // Attempts determine the FileSystemType for |path|. |
| 408 // Returns false if |path| doesn't exist. |
| 409 BASE_EXPORT bool GetFileSystemType(const FilePath& path, FileSystemType* type); |
| 410 #endif |
| 411 |
| 412 } // namespace base |
| 413 |
| 414 // ----------------------------------------------------------------------------- |
| 415 |
| 416 namespace file_util { |
| 417 |
| 405 // Functor for |ScopedFILE| (below). | 418 // Functor for |ScopedFILE| (below). |
| 406 struct ScopedFILEClose { | 419 struct ScopedFILEClose { |
| 407 inline void operator()(FILE* x) const { | 420 inline void operator()(FILE* x) const { |
| 408 if (x) | 421 if (x) |
| 409 fclose(x); | 422 fclose(x); |
| 410 } | 423 } |
| 411 }; | 424 }; |
| 412 | 425 |
| 413 // Automatically closes |FILE*|s. | 426 // Automatically closes |FILE*|s. |
| 414 typedef scoped_ptr<FILE, ScopedFILEClose> ScopedFILE; | 427 typedef scoped_ptr<FILE, ScopedFILEClose> ScopedFILE; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 432 | 445 |
| 433 // Automatically closes FDs (note: doesn't store the FD). | 446 // Automatically closes FDs (note: doesn't store the FD). |
| 434 // TODO(viettrungluu): This is a very odd API, since (unlike |FILE*|s, you'll | 447 // 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 | 448 // need to store the FD separately and keep its memory alive). This should |
| 436 // probably be called |ScopedFDCloser| or something like that. | 449 // probably be called |ScopedFDCloser| or something like that. |
| 437 typedef scoped_ptr<int, ScopedFDClose> ScopedFD; | 450 typedef scoped_ptr<int, ScopedFDClose> ScopedFD; |
| 438 // Let new users use ScopedFDCloser already, while ScopedFD is replaced. | 451 // Let new users use ScopedFDCloser already, while ScopedFD is replaced. |
| 439 typedef ScopedFD ScopedFDCloser; | 452 typedef ScopedFD ScopedFDCloser; |
| 440 #endif // OS_POSIX | 453 #endif // OS_POSIX |
| 441 | 454 |
| 442 #if defined(OS_LINUX) | |
| 443 // Broad categories of file systems as returned by statfs() on Linux. | |
| 444 enum FileSystemType { | |
| 445 FILE_SYSTEM_UNKNOWN, // statfs failed. | |
| 446 FILE_SYSTEM_0, // statfs.f_type == 0 means unknown, may indicate AFS. | |
| 447 FILE_SYSTEM_ORDINARY, // on-disk filesystem like ext2 | |
| 448 FILE_SYSTEM_NFS, | |
| 449 FILE_SYSTEM_SMB, | |
| 450 FILE_SYSTEM_CODA, | |
| 451 FILE_SYSTEM_MEMORY, // in-memory file system | |
| 452 FILE_SYSTEM_CGROUP, // cgroup control. | |
| 453 FILE_SYSTEM_OTHER, // any other value. | |
| 454 FILE_SYSTEM_TYPE_COUNT | |
| 455 }; | |
| 456 | |
| 457 // Attempts determine the FileSystemType for |path|. | |
| 458 // Returns false if |path| doesn't exist. | |
| 459 BASE_EXPORT bool GetFileSystemType(const base::FilePath& path, | |
| 460 FileSystemType* type); | |
| 461 #endif | |
| 462 | |
| 463 } // namespace file_util | 455 } // namespace file_util |
| 464 | 456 |
| 465 // Internal -------------------------------------------------------------------- | 457 // Internal -------------------------------------------------------------------- |
| 466 | 458 |
| 467 namespace base { | 459 namespace base { |
| 468 namespace internal { | 460 namespace internal { |
| 469 | 461 |
| 470 // Same as Move but allows paths with traversal components. | 462 // Same as Move but allows paths with traversal components. |
| 471 // Use only with extreme care. | 463 // Use only with extreme care. |
| 472 BASE_EXPORT bool MoveUnsafe(const FilePath& from_path, | 464 BASE_EXPORT bool MoveUnsafe(const FilePath& from_path, |
| (...skipping 10 matching lines...) Expand all Loading... |
| 483 // This function simulates Move(), but unlike Move() it works across volumes. | 475 // This function simulates Move(), but unlike Move() it works across volumes. |
| 484 // This function is not transactional. | 476 // This function is not transactional. |
| 485 BASE_EXPORT bool CopyAndDeleteDirectory(const FilePath& from_path, | 477 BASE_EXPORT bool CopyAndDeleteDirectory(const FilePath& from_path, |
| 486 const FilePath& to_path); | 478 const FilePath& to_path); |
| 487 #endif // defined(OS_WIN) | 479 #endif // defined(OS_WIN) |
| 488 | 480 |
| 489 } // namespace internal | 481 } // namespace internal |
| 490 } // namespace base | 482 } // namespace base |
| 491 | 483 |
| 492 #endif // BASE_FILE_UTIL_H_ | 484 #endif // BASE_FILE_UTIL_H_ |
| OLD | NEW |