]> git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_filedb/lib/AtomicFile.h
Security-57740.1.18.tar.gz
[apple/security.git] / OSX / libsecurity_filedb / lib / AtomicFile.h
1 /*
2 * Copyright (c) 2000-2001,2003,2011-2012,2014 Apple Inc. All Rights Reserved.
3 *
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
8 * using this file.
9 *
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.
16 */
17
18
19 //
20 // AtomicFile.h - Description t.b.d.
21 //
22 #ifndef _SECURITY_ATOMICFILE_H_
23 #define _SECURITY_ATOMICFILE_H_ 1
24
25 #include <security_utilities/refcount.h>
26 #include <Security/cssm.h>
27 #include <string>
28 #include <sys/stat.h>
29
30 namespace Security
31 {
32
33 class AtomicBufferedFile;
34 class AtomicLockedFile;
35 class AtomicTempFile;
36
37 class AtomicFile
38 {
39 public:
40 AtomicFile(const std::string &inPath);
41 ~AtomicFile();
42
43 // Aquire the write lock and remove the file.
44 void performDelete();
45
46 // Aquire the write lock and rename the file.
47 void rename(const std::string &inNewPath);
48
49 // Lock the file for writing and return a newly created AtomicTempFile.
50 RefPointer<AtomicTempFile> create(mode_t mode);
51
52 // Lock the file for writing and return a newly created AtomicTempFile.
53 RefPointer<AtomicTempFile> write();
54
55 // Return a bufferedFile containing current version of the file for reading.
56 RefPointer<AtomicBufferedFile> read();
57
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; }
62
63 mode_t mode() const;
64 bool isOnLocalFileSystem() {return mIsLocalFileSystem;}
65
66 enum OffsetType
67 {
68 FromStart,
69 FromEnd // only works with offset of 0
70 };
71
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);
76
77 private:
78 bool mIsLocalFileSystem;
79 string mPath;
80 string mDir;
81 string mFile;
82 string mLockFilePath;
83 };
84
85
86 //
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.
90 //
91 class AtomicBufferedFile : public RefCount
92 {
93 public:
94 AtomicBufferedFile(const std::string &inPath, bool isLocalFileSystem);
95 ~AtomicBufferedFile();
96
97 // Open the file and return it's size.
98 off_t open();
99
100 // Read inLength bytes starting at inOffset.
101 const uint8 *read(off_t inOffset, off_t inLength, off_t &outLength);
102
103 // Return the current mode bits of the file
104 mode_t mode();
105
106 // Close the file (this doesn't release the buffer).
107 void close();
108
109 // Return the length of the file.
110 off_t length() const { return mLength; }
111
112 private:
113 void loadBuffer();
114 void unloadBuffer();
115
116 private:
117 // Complete path to the file
118 string mPath;
119
120 // File descriptor to the file or -1 if it's not currently open.
121 int mFileRef;
122
123 // This is where the data from the file is read in to.
124 uint8 *mBuffer;
125
126 // Length of file in bytes.
127 off_t mLength;
128 };
129
130
131 //
132 // AtomicTempFile - A temporary file to write changes to.
133 //
134 class AtomicTempFile : public RefCount
135 {
136 public:
137 // Start a write for a new file.
138 AtomicTempFile(AtomicFile &inFile, const RefPointer<AtomicLockedFile> &inLockedFile, mode_t mode);
139
140 // Start a write of an existing file.
141 AtomicTempFile(AtomicFile &inFile, const RefPointer<AtomicLockedFile> &inLockedFile);
142
143 ~AtomicTempFile();
144
145 // Commit the current create or write and close the write file.
146 void commit();
147
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);
151
152 private:
153 // Called by both constructors.
154 void create(mode_t mode);
155
156 // Fsync the file
157 void fsync();
158
159 // Close the file
160 void close();
161
162 // Rollback the current create or write (happens automatically if commit() isn't called before the destructor is).
163 void rollback() throw();
164
165 private:
166 // Our AtomicFile object.
167 AtomicFile &mFile;
168
169 RefPointer<AtomicLockedFile> mLockedFile;
170
171 // Complete path to the file
172 string mPath;
173
174 // File descriptor to the file or -1 if it's not currently open.
175 int mFileRef;
176
177 // If this is true we unlink both mPath and mFile.path() when we rollback.
178 bool mCreating;
179 };
180
181
182 class FileLocker
183 {
184 public:
185 virtual ~FileLocker();
186
187 virtual void lock(mode_t mode) = 0;
188 virtual void unlock() = 0;
189 };
190
191
192
193 class LocalFileLocker : public FileLocker
194 {
195 public:
196 LocalFileLocker(AtomicFile &inFile);
197 virtual ~LocalFileLocker();
198
199 virtual void lock(mode_t mode);
200 virtual void unlock();
201
202 private:
203 int mLockFile;
204 string mPath;
205 };
206
207
208
209 class NetworkFileLocker : public FileLocker
210 {
211 public:
212 NetworkFileLocker(AtomicFile &inFile);
213 virtual ~NetworkFileLocker();
214
215 virtual void lock(mode_t mode);
216 virtual void unlock();
217
218 private:
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);
223
224 // The directory in which we create the lock
225 string mDir;
226
227 // Complete path to the file
228 string mPath;
229 };
230
231
232
233 // The current lock being held.
234 class AtomicLockedFile : public RefCount
235 {
236 public:
237 // Create a write lock for inFile.
238 AtomicLockedFile(AtomicFile &inFile);
239
240 ~AtomicLockedFile();
241
242 private:
243 void lock(mode_t mode = (S_IRUSR|S_IRGRP|S_IROTH) /* === 0444 */);
244 void unlock() throw();
245
246 private:
247 FileLocker* mFileLocker;
248 };
249
250
251 } // end namespace Security
252
253
254 #endif // _SECURITY_ATOMICFILE_H_