]>
git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_filedb/lib/AtomicFile.h
86be8bea9ef8371519e4090ea95384751aa5ac25
   2  * Copyright (c) 2000-2001,2003,2011-2012,2014 Apple Inc. All Rights Reserved. 
   4  * The contents of this file constitute Original Code as defined in and are 
   5  * subject to the Apple Public Source License Version 1.2 (the 'License'). 
   6  * You may not use this file except in compliance with the License. Please obtain 
   7  * a copy of the License at http://www.apple.com/publicsource and read it before 
  10  * This Original Code and all software distributed under the License are 
  11  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS 
  12  * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT 
  13  * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
  14  * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the 
  15  * specific language governing rights and limitations under the License. 
  20 //  AtomicFile.h - Description t.b.d. 
  22 #ifndef _SECURITY_ATOMICFILE_H_ 
  23 #define _SECURITY_ATOMICFILE_H_  1 
  25 #include <security_utilities/refcount.h> 
  26 #include <Security/cssm.h> 
  33 class AtomicBufferedFile
; 
  34 class AtomicLockedFile
; 
  40         AtomicFile(const std::string 
&inPath
); 
  43     // Acquire the write lock and remove the file. 
  46     // Acquire the write lock and rename the file. 
  47     void rename(const std::string 
&inNewPath
); 
  49         // Lock the file for writing and return a newly created AtomicTempFile. 
  50     RefPointer
<AtomicTempFile
> create(mode_t mode
); 
  52     // Lock the file for writing and return a newly created AtomicTempFile. 
  53         RefPointer
<AtomicTempFile
> write(); 
  55         // Return a bufferedFile containing current version of the file for reading. 
  56         RefPointer
<AtomicBufferedFile
> read(); 
  58         const string
& path() const { return mPath
; } 
  59         const string
& dir() const { return mDir
; } 
  60         const string
& file() const { return mFile
; } 
  61         const string
& lockFileName() { return mLockFilePath
; } 
  64         bool isOnLocalFileSystem() {return mIsLocalFileSystem
;} 
  69                 FromEnd                 
// only works with offset of 0 
  72         static void pathSplit(const std::string 
&inFull
, std::string 
&outDir
, std::string 
&outFile
); 
  73         static void mkpath(const std::string 
&inDir
, mode_t mode 
= 0777); 
  74         static int ropen(const char *const name
, int flags
, mode_t mode
); 
  75         static int rclose(int fd
); 
  78         bool mIsLocalFileSystem
; 
  87 // AtomicBufferedFile - This represents an instance of a file opened for reading. 
  88 // The file is read into memory and closed after this is done. 
  89 // The memory is released when this object is destroyed. 
  91 class AtomicBufferedFile 
: public RefCount
 
  94         AtomicBufferedFile(const std::string 
&inPath
, bool isLocalFileSystem
); 
  95         ~AtomicBufferedFile(); 
  97         // Open the file and return it's size. 
 100         // Read inLength bytes starting at inOffset. 
 101         const uint8 
*read(off_t inOffset
, off_t inLength
, off_t 
&outLength
); 
 103         // Return the current mode bits of the file 
 106         // Close the file (this doesn't release the buffer). 
 109         // Return the length of the file. 
 110         off_t 
length() const { return mLength
; } 
 117         // Complete path to the file 
 120         // File descriptor to the file or -1 if it's not currently open. 
 123         // This is where the data from the file is read in to. 
 126         // Length of file in bytes. 
 132 // AtomicTempFile - A temporary file to write changes to. 
 134 class AtomicTempFile 
: public RefCount
 
 137         // Start a write for a new file. 
 138         AtomicTempFile(AtomicFile 
&inFile
, const RefPointer
<AtomicLockedFile
> &inLockedFile
, mode_t mode
); 
 140         // Start a write of an existing file. 
 141         AtomicTempFile(AtomicFile 
&inFile
, const RefPointer
<AtomicLockedFile
> &inLockedFile
); 
 145     // Commit the current create or write and close the write file. 
 148     void write(AtomicFile::OffsetType inOffsetType
, off_t inOffset
, const uint32 
*inData
, uint32 inCount
); 
 149     void write(AtomicFile::OffsetType inOffsetType
, off_t inOffset
, const uint8 
*inData
, size_t inLength
); 
 150     void write(AtomicFile::OffsetType inOffsetType
, off_t inOffset
, const uint32 inData
); 
 153         // Called by both constructors. 
 154         void create(mode_t mode
); 
 162     // Rollback the current create or write (happens automatically if commit() isn't called before the destructor is). 
 163     void rollback() throw(); 
 166         // Our AtomicFile object. 
 169         RefPointer
<AtomicLockedFile
> mLockedFile
; 
 171         // Complete path to the file 
 174         // File descriptor to the file or -1 if it's not currently open. 
 177         // If this is true we unlink both mPath and mFile.path() when we rollback. 
 185         virtual ~FileLocker(); 
 187         virtual void lock(mode_t mode
) = 0; 
 188         virtual void unlock() = 0; 
 193 class LocalFileLocker 
: public FileLocker
 
 196         LocalFileLocker(AtomicFile 
&inFile
); 
 197         virtual ~LocalFileLocker(); 
 199         virtual void lock(mode_t mode
); 
 200         virtual void unlock(); 
 209 class NetworkFileLocker 
: public FileLocker
 
 212         NetworkFileLocker(AtomicFile 
&inFile
); 
 213         virtual ~NetworkFileLocker(); 
 215         virtual void lock(mode_t mode
); 
 216         virtual void unlock(); 
 219         std::string 
unique(mode_t mode
); 
 220         int rlink(const char *const old
, const char *const newn
, struct stat 
&sto
); 
 221         int myrename(const char *const old
, const char *const newn
); 
 222         int xcreat(const char *const name
, mode_t mode
, time_t &tim
); 
 224         // The directory in which we create the lock 
 227         // Complete path to the file 
 233 // The current lock being held. 
 234 class AtomicLockedFile 
: public RefCount
 
 237         // Create a write lock for inFile. 
 238         AtomicLockedFile(AtomicFile 
&inFile
); 
 243         void lock(mode_t mode 
= (S_IRUSR
|S_IRGRP
|S_IROTH
) /* === 0444 */); 
 244         void unlock() throw(); 
 247         FileLocker
* mFileLocker
; 
 251 } // end namespace Security 
 254 #endif // _SECURITY_ATOMICFILE_H_