Page Speed Optimization Libraries  1.5.27.2
net/instaweb/util/public/file_system.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2010 Google Inc.
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *      http:///www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00018 
00019 #ifndef NET_INSTAWEB_UTIL_PUBLIC_FILE_SYSTEM_H_
00020 #define NET_INSTAWEB_UTIL_PUBLIC_FILE_SYSTEM_H_
00021 
00022 #include <vector>
00023 
00024 #include "net/instaweb/util/public/basictypes.h"
00025 #include "net/instaweb/util/public/string.h"
00026 #include "net/instaweb/util/public/string_util.h"
00027 
00028 namespace net_instaweb {
00029 
00035 class BoolOrError {
00036   enum Choice {
00037     kIsFalse,
00038     kIsTrue,
00039     kIsError
00040   };
00041 
00042  public:
00043   BoolOrError() : choice_(kIsError) { }
00044   explicit BoolOrError(bool t_or_f) : choice_(t_or_f ? kIsTrue : kIsFalse) { }
00045 
00047   BoolOrError(const BoolOrError& src) : choice_(src.choice_) { }
00048   BoolOrError& operator=(const BoolOrError& src) {
00049     if (&src != this) {
00050       choice_ = src.choice_;
00051     }
00052     return *this;
00053   }
00054 
00055   bool is_false() const { return choice_ == kIsFalse; }
00056   bool is_true() const { return choice_ == kIsTrue; }
00057   bool is_error() const { return choice_ == kIsError; }
00058   void set_error() { choice_ = kIsError; }
00059   void set(bool t_or_f) { choice_ = t_or_f ? kIsTrue : kIsFalse; }
00060 
00061  private:
00062   Choice choice_;
00063 };
00064 
00065 class MessageHandler;
00066 class Timer;
00067 class Writer;
00068 
00076 class FileSystem {
00077  public:
00078   virtual ~FileSystem();
00079 
00080   class File {
00081    public:
00082     virtual ~File();
00083 
00085     virtual const char* filename() = 0;
00086 
00087    protected:
00089     friend class FileSystem;
00090     virtual bool Close(MessageHandler* handler) = 0;
00091   };
00092 
00093   class InputFile : public File {
00094    public:
00098     virtual int Read(char* buf, int size, MessageHandler* handler) = 0;
00099 
00100    protected:
00101     friend class FileSystem;
00102     virtual ~InputFile();
00103   };
00104 
00105   class OutputFile : public File {
00106    public:
00112     virtual bool Write(const StringPiece& buf, MessageHandler* handler) = 0;
00113     virtual bool Flush(MessageHandler* handler) = 0;
00114     virtual bool SetWorldReadable(MessageHandler* handler) = 0;
00115 
00116    protected:
00117     friend class FileSystem;
00118     virtual ~OutputFile();
00119   };
00120 
00121   struct FileInfo {
00122     FileInfo(int64 size_bytes, int64 atime_sec, const GoogleString& name)
00123         : size_bytes(size_bytes), atime_sec(atime_sec), name(name) {}
00124 
00125     int64 size_bytes;
00126     int64 atime_sec;
00127     GoogleString name;
00128   };
00129 
00130   struct DirInfo {
00131     DirInfo() : size_bytes(0), inode_count(0) { }
00132 
00133     std::vector<FileInfo> files;
00134     StringVector empty_dirs;
00135     int64 size_bytes;
00136     int64 inode_count;
00137   };
00138 
00144   virtual int MaxPathLength(const StringPiece& base) const;
00145 
00149   virtual bool ReadFile(const char* filename,
00150                         GoogleString* buffer,
00151                         MessageHandler* handler);
00152   virtual bool ReadFile(const char* filename,
00153                         Writer* writer,
00154                         MessageHandler* handler);
00155   virtual bool ReadFile(InputFile* input_file,
00156                         GoogleString* buffer,
00157                         MessageHandler* handler);
00158   virtual bool ReadFile(InputFile* input_file,
00159                         Writer* writer,
00160                         MessageHandler* handler);
00161   virtual bool WriteFile(const char* filename,
00162                          const StringPiece& buffer,
00163                          MessageHandler* handler);
00166   virtual bool WriteTempFile(const StringPiece& prefix_name,
00167                              const StringPiece& buffer,
00168                              GoogleString* filename,
00169                              MessageHandler* handler);
00170 
00175   bool WriteFileAtomic(const StringPiece& filename,
00176                        const StringPiece& buffer,
00177                        MessageHandler* handler);
00178 
00179   virtual InputFile* OpenInputFile(const char* filename,
00180                                    MessageHandler* handler) = 0;
00182   OutputFile* OpenOutputFile(const char* filename,
00183                              MessageHandler* handler) {
00184     SetupFileDir(filename, handler);
00185     return OpenOutputFileHelper(filename, false, handler);
00186   }
00189   OutputFile* OpenOutputFileForAppend(const char* filename,
00190                                       MessageHandler* handler) {
00191     SetupFileDir(filename, handler);
00192     return OpenOutputFileHelper(filename, true, handler);
00193   }
00199   OutputFile* OpenTempFile(const StringPiece& prefix_name,
00200                            MessageHandler* handler) {
00201     SetupFileDir(prefix_name, handler);
00202     return OpenTempFileHelper(prefix_name, handler);
00203   }
00204 
00206   virtual bool Close(File* file, MessageHandler* handler);
00207 
00208 
00210   virtual bool RemoveFile(const char* filename, MessageHandler* handler) = 0;
00211 
00214   bool RenameFile(const char* old_filename, const char* new_filename,
00215                   MessageHandler* handler) {
00216     SetupFileDir(new_filename, handler);
00217     return RenameFileHelper(old_filename, new_filename, handler);
00218   }
00219 
00222   virtual bool MakeDir(const char* directory_path, MessageHandler* handler) = 0;
00223 
00225   virtual bool RemoveDir(const char* directory_path,
00226                          MessageHandler* handler) = 0;
00227 
00229   virtual BoolOrError Exists(const char* path, MessageHandler* handler) = 0;
00230 
00232   virtual BoolOrError IsDir(const char* path, MessageHandler* handler) = 0;
00233 
00236   virtual bool RecursivelyMakeDir(const StringPiece& directory_path,
00237                                   MessageHandler* handler);
00238 
00245   virtual bool ListContents(const StringPiece& dir, StringVector* files,
00246                             MessageHandler* handler) = 0;
00247 
00253   virtual bool Atime(const StringPiece& path, int64* timestamp_sec,
00254                      MessageHandler* handler) = 0;
00255 
00257   virtual bool Mtime(const StringPiece& path, int64* timestamp_sec,
00258                      MessageHandler* handler) = 0;
00259 
00268   virtual void GetDirInfo(const StringPiece& path, DirInfo* dirinfo,
00269                           MessageHandler* handler);
00270 
00279   virtual bool Size(const StringPiece& path, int64* size_bytes,
00280                     MessageHandler* handler) = 0;
00281 
00287   virtual BoolOrError TryLock(const StringPiece& lock_name,
00288                               MessageHandler* handler) = 0;
00289 
00295   virtual BoolOrError TryLockWithTimeout(const StringPiece& lock_name,
00296                                          int64 timeout_millis,
00297                                          const Timer* timer,
00298                                          MessageHandler* handler) {
00299     return TryLock(lock_name, handler);
00300   }
00301 
00307   virtual bool Unlock(const StringPiece& lock_name,
00308                       MessageHandler* handler) = 0;
00309 
00310  protected:
00313   virtual OutputFile* OpenOutputFileHelper(const char* filename,
00314                                            bool append,
00315                                            MessageHandler* handler) = 0;
00316   virtual OutputFile* OpenTempFileHelper(const StringPiece& filename,
00317                                          MessageHandler* handler) = 0;
00318   virtual bool RenameFileHelper(const char* old_filename,
00319                                 const char* new_filename,
00320                                 MessageHandler* handler) = 0;
00321 
00322  private:
00324   void SetupFileDir(const StringPiece& filename, MessageHandler* handler);
00325 };
00326 
00327 }  
00328 
00329 #endif  ///< NET_INSTAWEB_UTIL_PUBLIC_FILE_SYSTEM_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines