sipxportlib  Version 3.3
OsFileBase.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2005, 2007 SIPez LLC.
3 // Licensed to SIPfoundry under a Contributor Agreement.
4 //
5 // Copyright (C) 2004-2006 SIPfoundry Inc.
6 // Licensed by SIPfoundry under the LGPL license.
7 //
8 // Copyright (C) 2004-2006 Pingtel Corp. All rights reserved.
9 // Licensed to SIPfoundry under a Contributor Agreement.
10 //
11 // $$
13 
14 #ifndef _OsFile_h_
15 #define _OsFile_h_
16 
17 // SYSTEM INCLUDES
18 #include "os/OsDefs.h"
19 #include "os/OsStatus.h"
20 #include "os/OsPathBase.h"
21 #include "os/OsLock.h"
22 #include "os/OsBSem.h"
23 #include "os/OsMutex.h"
24 #include "os/OsConfigDb.h"
25 #include <utl/UtlString.h>
26 
27 // APPLICATION INCLUDES
28 
29 // DEFINES
30 
31 // MACROS
32 // EXTERNAL FUNCTIONS
33 // EXTERNAL VARIABLES
34 // CONSTANTS
35 // STRUCTS
36 // TYPEDEFS
37 // FORWARD DECLARATIONS
38 class OsFileInfoBase;
39 class OsPathBase;
40 
41 //:OS class for creating,reading, writing, manipulating files.
43 {
44 /* //////////////////////////// PUBLIC //////////////////////////////////// */
45 public:
46 
47  enum Mode {
48  READ_ONLY = 1,
51  CREATE = 8,
52  TRUNCATE = 16,
53  APPEND = 32,
54  FSLOCK = 64,
56  };
59 
60  //: Options for the origin in setting the file position.
62  START = 0,
63  CURRENT = 1,
64  END = 2
65  };
69 
70 /* ============================ CREATORS ================================== */
71 
72  OsFileBase(const OsPathBase& filename);
73  //:Default constructor
74 
75  virtual
76  ~OsFileBase();
77  //:Destructor
78 
79 /* ============================ MANIPULATORS ============================== */
80 
81 
83  static long openAndRead(const char* filename, UtlString& fileContentsRead);
84 
86  static long openAndWrite(const char* filename, const UtlString& fileContentsToWrite);
87 
89  static long openAndWrite(const char* filename,
90  const char* fileContentsToWrite,
91  unsigned int contentLength);
92 
93  virtual OsStatus open(const int mode = READ_WRITE);
94  //: Opens the specified file using the specified mode
95  //: Returns:
96  //: OS_SUCCESS if successful
97  //: OS_FILE_ACCESS_DENIED if file is readonly or currently in use
98  //: OS_FILE_NOT_FOUND if the file specified was not found.
99  //: For file locking, use FSLOCK or FSLOCK_WAIT in mode. Note that you
100  //: must open the file read-write to lock it, and that file locking is
101  //: advisory: other callers must also use FSLOCK or FSLOCK_WAIT.
102 
103  virtual OsStatus fileunlock();
104  //: Cross-process unlocks this file.
105  //: Notes: This method should only be called by OsFileBase::close()!
106 
107  virtual OsStatus filelock(const bool wait);
108  //: Cross-process locks this file, optionally waiting for the lock.
109  //: Returns:
110  //: OS_SUCCESS if successful
111  //: OS_FAILED if unsuccessful
112  //: Notes: This method should only be called by OsFileBase::open()!
113 
114  virtual OsStatus flush();
115  //: Flushes any pending output
116 
117  virtual OsStatus write(const void* pBuf, unsigned long bufLen, unsigned long& rBytesWritten);
118  //: Write X bytes to file
119  //: Returns:
120  //: OS_SUCCESS if successful
121  //: OS_FILE_DISKFULL if (you guessed it) disk full. :)
122  //: OS_localFileLocks
123  //: FILE_INVALID_HANDLE if something has gone wrong an handle is invalid.
124 
125  virtual OsStatus setLength(unsigned long newLength);
126  //: Sets the length of the file specified by the object to the new size
127  //: Sets the length of the file specified by the object to the new size
128  //: Shrinking or Growing the file as needed.
129 
130  virtual OsStatus setPosition(long pos, FilePositionOrigin origin = START);
131  //: Set the current active position in the file for the next read or write
132  //: operation. The pos variable is a signed number which is
133  //: added to the specified origin. For origin == OsFile::Start
134  //: only positive values for pos are meaningful. For
135  //: origin == OsFile::End only negative values for
136  //: pos are meaningful
137 
138 
139  virtual OsStatus remove(UtlBoolean bForce = FALSE);
140  //: Removes the file specified by this object
141  //: Set bForce to TRUE to remove read-only files
142  //: Returns:
143  //: OS_SUCCESS if successful
144  //: OS_INVALID if failed
145 
146  virtual OsStatus rename(const OsPathBase& rNewFilename);
147  //: Rename this file to the new file name
148  //: Returns:
149  //: OS_SUCCESS if successful
150  //: OS_INVALID if failed
151 
152  virtual OsStatus copy(const OsPathBase& rNewFilename);
153  //: Copy this file to the new specified location
154  //: Returns:
155  //: OS_SUCCESS if successful
156  //: OS_FILE_WRITE_FAILED if it fails to create the file.
157 
158  virtual OsStatus setReadOnly(UtlBoolean isReadOnly);
159  //: Sets the file to the new state
160  //: Returns:
161  //: OS_SUCCESS if successful
162  //: OS_INVALID if failed
163 
164  virtual OsStatus touch();
165  //: Updates the date and time on the file. Creates if needed.
166 
167 /* ============================ ACCESSORS ================================= */
168 
169  virtual OsStatus getPosition(unsigned long &pos);
170  //: Get the current active position in the file for the next read or write operation.
171 
172  virtual void getFileName(OsPathBase& rOsPath) const;
173  //: Returns the fully qualified filename for this File object
174 
175  virtual OsStatus read(void *pBuf, unsigned long bufLen, unsigned long &rBytesRead);
176  //: Read X bytes from file
177 
178  virtual OsStatus readLine(UtlString &str);
179  //: Read bytes up to \n or eof, whichever comes first
180  //: Return
181  virtual UtlBoolean close();
182  //: Closes the file.
183 
184  OsStatus getLength(unsigned long &length);
185  //: Returns the length of the file specified by the object
186 
187  FILE* getFileDescriptor() { return mOsFileHandle; };
188 
189  OsConfigDb* getFileLocks() { return mpFileLocks; };
190 
191 /* ============================ INQUIRY =================================== */
192 
193  UtlBoolean isReadonly() const;
194  //: Returns TRUE if file is readonly
195 
196 
197  UtlBoolean exists() ;
198  //: Returns TRUE if file object filename exists
199 
200 
201  virtual OsStatus getFileInfo(OsFileInfoBase& rFileinfo) const = 0;
202  //: Returns all the relevant info on this file
203 
204  UtlBoolean isEOF();
205  //: Returns TRUE if stream is past end of file
206 
207 /* //////////////////////////// PROTECTED ///////////////////////////////// */
208 protected:
209 
210  OsMutex fileMutex;
211  //: Block other methods like close
212  // while we are busy reading,writing and close.
213 
214  OsFileBase(const OsFileBase& rOsFile);
215  //:Copy constructor
216 
217  OsFileBase& operator=(const OsFileBase& rhs);
218  //:Assignment operator
219 
221  //: Handle to file
222 
224  //:Fully qualified name where file is (or will be) located
225 
226  int mMode;
227  //: The open file's mode
228 
229 /* //////////////////////////// PRIVATE /////////////////////////////////// */
230 private:
231  static OsConfigDb *mpFileLocks;
232 };
233 
234 /* ============================ INLINE METHODS ============================ */
235 
236 #endif // _OsFile_h_
237 
238 
OsConfigDb * getFileLocks()
Definition: OsFileBase.h:189
OsMutex fileMutex
Definition: OsFileBase.h:210
UtlBoolean exists()
Definition: OsFileBase.cpp:865
virtual OsStatus setLength(unsigned long newLength)
Definition: OsFileBase.cpp:397
virtual UtlBoolean close()
Definition: OsFileBase.cpp:742
Definition: OsFileBase.h:49
virtual OsStatus filelock(const bool wait)
Definition: OsFileBase.cpp:209
virtual OsStatus flush()
Definition: OsFileBase.cpp:347
Definition: OsFileBase.h:53
UtlBoolean isEOF()
Definition: OsFileBase.cpp:789
OsStatus
Definition: OsStatus.h:27
FILE * getFileDescriptor()
Definition: OsFileBase.h:187
virtual OsStatus getPosition(unsigned long &pos)
Definition: OsFileBase.cpp:448
Definition: OsFileBase.h:63
Definition: OsFileBase.h:62
Definition: OsFileBase.h:48
virtual OsStatus fileunlock()
Definition: OsFileBase.cpp:193
OsFileBase & operator=(const OsFileBase &rhs)
Definition: OsFileBase.cpp:890
virtual OsStatus setPosition(long pos, FilePositionOrigin origin=START)
Definition: OsFileBase.cpp:413
UtlBoolean isReadonly() const
Definition: OsFileBase.cpp:814
Definition: OsFileInfoBase.h:34
FilePositionOrigin
enumcode: FSLOCK - Opens the file exclusively (advisory locking). enumcode: FSLOCK_WAIT - Waits to op...
Definition: OsFileBase.h:61
virtual OsStatus readLine(UtlString &str)
Definition: OsFileBase.cpp:709
Definition: OsFileBase.h:50
Definition: UtlString.h:48
virtual OsStatus setReadOnly(UtlBoolean isReadOnly)
Definition: OsFileBase.cpp:176
OsFileBase(const OsPathBase &filename)
enumcode: Start - Set position relative to start of file. enumcode: Current - Set position relative t...
Definition: OsFileBase.cpp:65
virtual OsStatus open(const int mode=READ_WRITE)
Definition: OsFileBase.cpp:228
Definition: OsConfigDb.h:67
int mMode
Definition: OsFileBase.h:226
virtual void getFileName(OsPathBase &rOsPath) const
Definition: OsFileBase.cpp:658
Definition: OsFileBase.h:51
virtual OsStatus read(void *pBuf, unsigned long bufLen, unsigned long &rBytesRead)
Definition: OsFileBase.cpp:674
OsStatus getLength(unsigned long &length)
Definition: OsFileBase.cpp:833
OsPathBase mFilename
Definition: OsFileBase.h:223
virtual ~OsFileBase()
Definition: OsFileBase.cpp:100
FILE * mOsFileHandle
Definition: OsFileBase.h:220
int UtlBoolean
Definition: UtlDefs.h:41
virtual OsStatus touch()
Definition: OsFileBase.cpp:617
virtual OsStatus rename(const OsPathBase &rNewFilename)
Definition: OsFileBase.cpp:499
static long openAndWrite(const char *filename, const UtlString &fileContentsToWrite)
Opens and write the given UtlString to the named file.
Definition: OsFileBase.cpp:146
Definition: OsFileBase.h:42
Mode
Definition: OsFileBase.h:47
Definition: OsFileBase.h:64
Definition: OsFileBase.h:52
virtual OsStatus getFileInfo(OsFileInfoBase &rFileinfo) const =0
Definition: OsFileBase.h:55
Definition: OsPathBase.h:34
#define FALSE
Definition: UtlDefs.h:21
Definition: OsFileBase.h:54
static long openAndRead(const char *filename, UtlString &fileContentsRead)
Opens and reads the contents of the named file into the given UtlString.
Definition: OsFileBase.cpp:117
virtual OsStatus write(const void *pBuf, unsigned long bufLen, unsigned long &rBytesWritten)
Definition: OsFileBase.cpp:372
virtual OsStatus copy(const OsPathBase &rNewFilename)
Definition: OsFileBase.cpp:543