00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00018
00019 #ifndef NET_INSTAWEB_UTIL_PUBLIC_FILE_SYSTEM_H_
00020 #define NET_INSTAWEB_UTIL_PUBLIC_FILE_SYSTEM_H_
00021
00022 #include "net/instaweb/util/public/basictypes.h"
00023 #include "net/instaweb/util/public/string.h"
00024 #include "net/instaweb/util/public/string_util.h"
00025
00026 namespace net_instaweb {
00027
00033 class BoolOrError {
00034 enum Choice {
00035 kIsFalse,
00036 kIsTrue,
00037 kIsError
00038 };
00039
00040 public:
00041 BoolOrError() : choice_(kIsError) { }
00042 explicit BoolOrError(bool t_or_f) : choice_(t_or_f ? kIsTrue : kIsFalse) { }
00043
00045 BoolOrError(const BoolOrError& src) : choice_(src.choice_) { }
00046 BoolOrError& operator=(const BoolOrError& src) {
00047 if (&src != this) {
00048 choice_ = src.choice_;
00049 }
00050 return *this;
00051 }
00052
00053 bool is_false() const { return choice_ == kIsFalse; }
00054 bool is_true() const { return choice_ == kIsTrue; }
00055 bool is_error() const { return choice_ == kIsError; }
00056 void set_error() { choice_ = kIsError; }
00057 void set(bool t_or_f) { choice_ = t_or_f ? kIsTrue : kIsFalse; }
00058
00059 private:
00060 Choice choice_;
00061 };
00062
00063 class MessageHandler;
00064 class Writer;
00065
00073 class FileSystem {
00074 public:
00075 virtual ~FileSystem();
00076
00077 class File {
00078 public:
00079 virtual ~File();
00080
00082 virtual const char* filename() = 0;
00083
00084 protected:
00086 friend class FileSystem;
00087 virtual bool Close(MessageHandler* handler) = 0;
00088 };
00089
00090 class InputFile : public File {
00091 public:
00095 virtual int Read(char* buf, int size, MessageHandler* handler) = 0;
00096
00097 protected:
00098 friend class FileSystem;
00099 virtual ~InputFile();
00100 };
00101
00102 class OutputFile : public File {
00103 public:
00109 virtual bool Write(const StringPiece& buf, MessageHandler* handler) = 0;
00110 virtual bool Flush(MessageHandler* handler) = 0;
00111 virtual bool SetWorldReadable(MessageHandler* handler) = 0;
00112
00113 protected:
00114 friend class FileSystem;
00115 virtual ~OutputFile();
00116 };
00117
00123 virtual int MaxPathLength(const StringPiece& base) const;
00124
00126 virtual bool ReadFile(const char* filename,
00127 GoogleString* buffer,
00128 MessageHandler* handler);
00129 virtual bool ReadFile(const char* filename,
00130 Writer* writer,
00131 MessageHandler* handler);
00132 virtual bool WriteFile(const char* filename,
00133 const StringPiece& buffer,
00134 MessageHandler* handler);
00137 virtual bool WriteTempFile(const StringPiece& prefix_name,
00138 const StringPiece& buffer,
00139 GoogleString* filename,
00140 MessageHandler* handler);
00141
00146 bool WriteFileAtomic(const StringPiece& filename,
00147 const StringPiece& buffer,
00148 MessageHandler* handler);
00149
00150 virtual InputFile* OpenInputFile(const char* filename,
00151 MessageHandler* handler) = 0;
00153 OutputFile* OpenOutputFile(const char* filename,
00154 MessageHandler* handler) {
00155 SetupFileDir(filename, handler);
00156 return OpenOutputFileHelper(filename, handler);
00157 }
00163 OutputFile* OpenTempFile(const StringPiece& prefix_name,
00164 MessageHandler* handler) {
00165 SetupFileDir(prefix_name, handler);
00166 return OpenTempFileHelper(prefix_name, handler);
00167 }
00168
00170 virtual bool Close(File* file, MessageHandler* handler);
00171
00172
00174 virtual bool RemoveFile(const char* filename, MessageHandler* handler) = 0;
00175
00178 bool RenameFile(const char* old_filename, const char* new_filename,
00179 MessageHandler* handler) {
00180 SetupFileDir(new_filename, handler);
00181 return RenameFileHelper(old_filename, new_filename, handler);
00182 }
00183
00186 virtual bool MakeDir(const char* directory_path, MessageHandler* handler) = 0;
00187
00189 virtual BoolOrError Exists(const char* path, MessageHandler* handler) = 0;
00190
00192 virtual BoolOrError IsDir(const char* path, MessageHandler* handler) = 0;
00193
00196 virtual bool RecursivelyMakeDir(const StringPiece& directory_path,
00197 MessageHandler* handler);
00198
00205 virtual bool ListContents(const StringPiece& dir, StringVector* files,
00206 MessageHandler* handler) = 0;
00207
00213 virtual bool Atime(const StringPiece& path, int64* timestamp_sec,
00214 MessageHandler* handler) = 0;
00215
00217 virtual bool Mtime(const StringPiece& path, int64* timestamp_sec,
00218 MessageHandler* handler) = 0;
00219
00227 virtual bool RecursiveDirSize(const StringPiece& path, int64* size,
00228 MessageHandler* handler);
00229
00234 virtual bool Size(const StringPiece& path, int64* size,
00235 MessageHandler* handler) = 0;
00236
00242 virtual BoolOrError TryLock(const StringPiece& lock_name,
00243 MessageHandler* handler) = 0;
00244
00250 virtual BoolOrError TryLockWithTimeout(const StringPiece& lock_name,
00251 int64 timeout_millis,
00252 MessageHandler* handler) {
00253 return TryLock(lock_name, handler);
00254 }
00255
00261 virtual bool Unlock(const StringPiece& lock_name,
00262 MessageHandler* handler) = 0;
00263
00264 protected:
00267 virtual OutputFile* OpenOutputFileHelper(const char* filename,
00268 MessageHandler* handler) = 0;
00269 virtual OutputFile* OpenTempFileHelper(const StringPiece& filename,
00270 MessageHandler* handler) = 0;
00271 virtual bool RenameFileHelper(const char* old_filename,
00272 const char* new_filename,
00273 MessageHandler* handler) = 0;
00274
00275 private:
00277 void SetupFileDir(const StringPiece& filename, MessageHandler* handler);
00278 };
00279
00280 }
00281
00282 #endif ///< NET_INSTAWEB_UTIL_PUBLIC_FILE_SYSTEM_H_