2 * Copyright 1995, 1996 Perforce Software. All rights reserved.
4 * This file is part of Perforce - the FAST SCM System.
8 * FileSys.h - OS specific file manipulation
12 * FileSys - a file handle, with all the trimmings
14 * Static Public methods:
16 * FileSys::Create() - create a FileSys, given its file type
17 * FileSys::CreateTemp() - create, destructor deletes the file
18 * FileSys::CreateGloablTemp() - Temp, constructor makes a global name
22 * FileSys::Set() - set file name
23 * FileSys::Name() - get file name
24 * FileSys::GetType() - get type previously set
25 * FileSys::IsTextual() - return if type is one of text types
26 * FileSys::IsExec() - return if type indicates executable bit set
27 * FileSys::DoIndirectWrites() - updates should write temp/rename
29 * FileSys::MakeGlobalTemp() - make a temp name in a global directory
30 * FileSys::MakeLocalTemp() - make a temp name in same dir as file
32 * FileSys::IsDeleteOnClose() - will file be removed on close?
33 * FileSys::SetDeleteOnClose() - file will be removed
34 * FileSys::ClearDeleteOnClose() - file won't be removed
36 * FileSys::Perms() - set file permission for close after write
37 * FileSys::ModTime() - set mod time for close after write
38 * FileSys::ChmodTime() - use modTime value to change mod time directly
40 * FileSys::Open() - open named file according to mode
41 * FileSys::Write() - write a block into file
42 * FileSys::Read() - read a block from file
43 * FileSys::ReadLine() - read a line into string
44 * FileSys::ReadWhole() - read whole file into string
45 * FileSys::Close() - close file description
47 * FileSys::Stat() - return flags if file exists, writable
48 * FileSys::Truncate() - set file to zero length
49 * FileSys::Unlink() - remove single file
51 * FileSys::GetFd() - return underlying int fd, FST_BINARY only
52 * FileSys::GetSize() - return file size, FST_BINARY only
53 * FileSys::Seek() - seek to offset, FST_BINARY only
55 * FileSys::ScanDir() - return a list of directory contents
56 * FileSys::MkDir() - make a directory for the current file
57 * FileSys::RmDir() - remove the directory of the current file
58 * FileSys::Rename() - rename file to target
59 * FileSys::ReadFile() - open, read whole file into string, close
60 * FileSys::WriteFile() - open, write whole file from string, close
61 * FileSys::Chmod() - change permissions
62 * FileSys::Compare() - compare file against target
63 * FileSys::Copy - copy one file to another
64 * FileSys::Digest() - return a fingerprint of the file contents
66 * FileSys::CheckType() - look at the file and see if it is binary, etc
73 FST_TEXT
= 0x0001, // file is text
74 FST_BINARY
= 0x0002, // file is binary
75 FST_GZIP
= 0x0003, // file is gzip
76 FST_DIRECTORY
= 0x0005, // it's a directory
77 FST_SYMLINK
= 0x0006, // it's a symlink
78 FST_RESOURCE
= 0x0007, // Macintosh resource file
79 FST_SPECIAL
= 0x0008, // not a regular file
80 FST_MISSING
= 0x0009, // no file at all
81 FST_CANTTELL
= 0x000A, // can read file to find out
82 FST_EMPTY
= 0x000B, // file is empty
83 FST_UNICODE
= 0x000C, // file is unicode
84 FST_GUNZIP
= 0x000D, // stream is gzip
86 FST_MASK
= 0x000F, // mask for types
90 FST_M_APPEND
= 0x0010, // open always append
91 FST_M_EXCL
= 0x0020, // open exclusive create
92 FST_M_SYNC
= 0x0040, // fsync on close
94 FST_M_EXEC
= 0x0100, // file is executable
95 FST_M_APPLE
= 0x0200, // apple single/double encoding
96 FST_M_COMP
= 0x0400, // file is somehow compressed
98 FST_M_MASK
= 0x0ff0, // mask for modifiers
100 // Line ending types, loosely mapped to LineType
102 FST_L_LOCAL
= 0x0000, // LineTypeLocal
103 FST_L_LF
= 0x1000, // LineTypeRaw
104 FST_L_CR
= 0x2000, // LineTypeCr
105 FST_L_CRLF
= 0x3000, // LineTypeCrLf
106 FST_L_LFCRLF
= 0x4000, // LineTypeLfcrlf
108 FST_L_MASK
= 0xf000, // mask for LineTypes
110 // Composite types, for filesys.cc
112 FST_ATEXT
= 0x0011, // append-only text
113 FST_XTEXT
= 0x0101, // executable text
114 FST_RTEXT
= 0x1001, // raw text
115 FST_RXTEXT
= 0x1101, // executable raw text
116 FST_CBINARY
= 0x0402, // pre-compressed binary
117 FST_XBINARY
= 0x0102, // executable binary
118 FST_APPLETEXT
= 0x0201, // apple format text
119 FST_APPLEFILE
= 0x0202, // apple format binary
120 FST_XAPPLEFILE
= 0x0302, // executable apple format binary
121 FST_XUNICODE
= 0x010C, // executable unicode text
122 FST_RCS
= 0x1041 // RCS temporary file: raw text, sync on close
127 FSF_EXISTS
= 0x01, // file exists
128 FSF_WRITEABLE
= 0x02, // file is user-writable
129 FSF_DIRECTORY
= 0x04, // file is a directory
130 FSF_SYMLINK
= 0x08, // file is symlink
131 FSF_SPECIAL
= 0x10, // file is not regular
132 FSF_EXECUTABLE
= 0x20, // file is executable
133 FSF_EMPTY
= 0x40, // file is empty
134 FSF_HIDDEN
= 0x80 // file is invisible (hidden)
138 FOM_READ
, // open for reading
139 FOM_WRITE
// open for writing
143 FPM_RO
, // leave file read-only
144 FPM_RW
, // leave file read-write
145 FPM_ROO
// leave file read-only (owner)
156 static FileSys
*Create( FileSysType type
);
158 static FileSys
*CreateTemp( FileSysType type
) {
159 FileSys
*f
= Create( type
);
160 f
->SetDeleteOnClose();
164 static FileSys
*CreateGlobalTemp( FileSysType type
) {
165 FileSys
*f
= Create( type
);
166 f
->SetDeleteOnClose();
171 // Get/set name, perms, modtime
173 char * Name() { return path
.Text(); }
174 StrBuf
& Path() { return path
; }
176 void Perms( FilePerm p
) { perms
= p
; }
177 void ModTime( StrPtr
*u
) { modTime
= u
->Atoi(); }
178 void ModTime( time_t t
) { modTime
= static_cast<int>(t
); }
182 FileSysType
GetType() { return type
; }
183 int IsExec() { return ( type
& FST_M_EXEC
); }
185 return ( type
& FST_MASK
) == FST_TEXT
||
186 ( type
& FST_MASK
) == FST_UNICODE
;
189 return ( type
& FST_MASK
) == FST_UNICODE
;
192 // Read/write file access, provided by derived class
197 virtual void Set( const StrPtr
&name
);
198 virtual int DoIndirectWrites();
199 virtual void Translator( CharSetCvt
* );
201 virtual void Open( FileOpenMode mode
, Error
*e
) = 0;
202 virtual void Write( const char *buf
, int len
, Error
*e
) = 0;
203 virtual int Read( char *buf
, int len
, Error
*e
) = 0;
204 virtual void Close( Error
*e
) = 0;
206 virtual int Stat() = 0;
207 virtual int StatModTime() = 0;
208 virtual void Truncate( Error
*e
) = 0;
209 virtual void Unlink( Error
*e
= 0 ) = 0;
210 virtual void Rename( FileSys
*target
, Error
*e
) = 0;
211 virtual void Chmod( FilePerm perms
, Error
*e
) = 0;
212 virtual void ChmodTime( Error
*e
) = 0;
214 // NB: these for ReadFile only; interface will likely change
217 virtual size_t GetSize();
218 virtual void Seek( off_t offset
);
220 // Convenience wrappers for above
222 void Chmod( const char *perms
, Error
*e
);
223 void Chmod( Error
*e
) { Chmod( perms
, e
); }
225 void Set( const char *name
) { Set( StrRef( name
) ); }
227 void Write( const StrPtr
&b
, Error
*e
)
228 { Write( b
.Text(), b
.Length(), e
); }
230 void Write( const StrPtr
*b
, Error
*e
)
231 { Write( b
->Text(), b
->Length(), e
); }
235 void MakeGlobalTemp();
236 void MakeLocalTemp( char *file
);
237 int IsDeleteOnClose() { return isTemp
; }
238 void SetDeleteOnClose() { isTemp
= 1; }
239 void ClearDeleteOnClose() { isTemp
= 0; }
243 StrArray
*ScanDir( Error
*e
);
245 static void MkDir( const StrPtr
&p
, Error
*e
);
246 void MkDir( Error
*e
) { MkDir( path
, e
); }
247 static void RmDir( const StrPtr
&p
, Error
*e
);
248 void RmDir( Error
*e
= 0 ) { RmDir( path
, e
); }
250 FileSysType
CheckType();
252 # if defined ( OS_MACOSX )
253 FileSysType
CheckTypeMac();
256 // Type generic operations
258 int ReadLine( StrBuf
*buf
, Error
*e
);
259 void ReadWhole( StrBuf
*buf
, Error
*e
);
261 // Type generic, whole file operations
263 void ReadFile( StrBuf
*buf
, Error
*e
);
264 void WriteFile( const StrPtr
*buf
, Error
*e
);
265 int Compare( FileSys
*other
, Error
*e
);
266 void Copy( FileSys
*targetFile
, FilePerm perms
, Error
*e
);
267 void Digest( StrBuf
*digest
, Error
*e
);
271 // Character Set operations
272 static void SetCharSet( int x
= 0 ) { charSet
= x
; }
273 static int GetCharSet() { return charSet
; }
277 FileOpenMode mode
; // read or write
278 FilePerm perms
; // leave read-only or read-write
279 int modTime
; // stamp file mod date on close
283 void TempName( char *buf
);
291 // These constants are (internal) convenience only.
293 const int FileBufferSize
= 4096; // biffer size for read, compare
294 const int FileBinaryScan
= 1024; // how far to look for binary chars