diff options
author | jsbell@chromium.org <jsbell@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-10-10 23:02:39 +0000 |
---|---|---|
committer | jsbell@chromium.org <jsbell@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-10-10 23:02:39 +0000 |
commit | 3b95e1d0219931466d4c53da0703371f4b44faae (patch) | |
tree | cb794445e83db887fd27b5c326a65d31c5865807 /base/platform_file.h | |
parent | e421391b11135c0661182f9f2ff1227902a873a0 (diff) | |
download | chromium_src-3b95e1d0219931466d4c53da0703371f4b44faae.zip chromium_src-3b95e1d0219931466d4c53da0703371f4b44faae.tar.gz chromium_src-3b95e1d0219931466d4c53da0703371f4b44faae.tar.bz2 |
Expose PlatformFileLock/Unlock, fix locking in Chromium's leveldb Env
The base::CreatePlatformFile() flags PLATFORM_FILE_EXCLUSIVE_READ/WRITE
are ineffective on existing files on POSIX, and can't be used to
implement the locking scheme required by leveldb.
Add PlatformFileLock/Unlock which have the correct semantics, and
use those in ChromiumEnv. Also, crib code from leveldb's env_posix.cc
to handle in-process exclusivity which is not guaranteed by
file locks.
BUG=245471
Review URL: https://codereview.chromium.org/26306003
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@228036 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/platform_file.h')
-rw-r--r-- | base/platform_file.h | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/base/platform_file.h b/base/platform_file.h index 3d69ae7..66e5f96 100644 --- a/base/platform_file.h +++ b/base/platform_file.h @@ -24,6 +24,8 @@ namespace base { // or creating a file. // PLATFORM_FILE_(WRITE|APPEND) are mutually exclusive. This is so that APPEND // behavior will be consistent with O_APPEND on POSIX. +// PLATFORM_FILE_EXCLUSIVE_(READ|WRITE) only grant exclusive access to the file +// on creation on POSIX; for existing files, consider using LockPlatformFile(). enum PlatformFileFlags { PLATFORM_FILE_OPEN = 1 << 0, // Opens a file, only if it exists. PLATFORM_FILE_CREATE = 1 << 1, // Creates a new file, only if it @@ -210,6 +212,31 @@ BASE_EXPORT bool TouchPlatformFile(PlatformFile file, // Returns some information for the given file. BASE_EXPORT bool GetPlatformFileInfo(PlatformFile file, PlatformFileInfo* info); +// Attempts to take an exclusive write lock on the file. Returns immediately +// (i.e. does not wait for another process to unlock the file). If the lock +// was obtained, the result will be PLATFORM_FILE_OK. A lock only guarantees +// that other processes may not also take a lock on the same file with the +// same API - it may still be opened, renamed, unlinked, etc. +// +// Common semantics: +// * Locks are held by processes, but not inherited by child processes. +// * Locks are released by the OS on file handle close or process termination. +// * Locks are reliable only on local filesystems. +// * Duplicated file handles may also write to locked files. +// Windows-specific semantics: +// * Locks are mandatory for read/write APIs, advisory for mapping APIs. +// * Within a process, locking the same file (by the same or new handle) +// will fail. +// POSIX-specific semantics: +// * Locks are advisory only. +// * Within a process, locking the same file (by the same or new handle) +// will succeed. +// * Closing any descriptor on a given file releases the lock. +BASE_EXPORT PlatformFileError LockPlatformFile(PlatformFile file); + +// Unlock a file previously locked with LockPlatformFile. +BASE_EXPORT PlatformFileError UnlockPlatformFile(PlatformFile file); + // Use this class to pass ownership of a PlatformFile to a receiver that may or // may not want to accept it. This class does not own the storage for the // PlatformFile. |