Page Speed Optimization Libraries  1.2.24.1
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 Writer;
00067 
00075 class FileSystem {
00076  public:
00077   virtual ~FileSystem();
00078 
00079   class File {
00080    public:
00081     virtual ~File();
00082 
00084     virtual const char* filename() = 0;
00085 
00086    protected:
00088     friend class FileSystem;
00089     virtual bool Close(MessageHandler* handler) = 0;
00090   };
00091 
00092   class InputFile : public File {
00093    public:
00097     virtual int Read(char* buf, int size, MessageHandler* handler) = 0;
00098 
00099    protected:
00100     friend class FileSystem;
00101     virtual ~InputFile();
00102   };
00103 
00104   class OutputFile : public File {
00105    public:
00111     virtual bool Write(const StringPiece& buf, MessageHandler* handler) = 0;
00112     virtual bool Flush(MessageHandler* handler) = 0;
00113     virtual bool SetWorldReadable(MessageHandler* handler) = 0;
00114 
00115    protected:
00116     friend class FileSystem;
00117     virtual ~OutputFile();
00118   };
00119 
00120   struct FileInfo {
00121     FileInfo(int64 size_bytes, int64 atime_sec, const GoogleString& name)
00122         : size_bytes(size_bytes), atime_sec(atime_sec), name(name) {}
00123 
00124     int64 size_bytes;
00125     int64 atime_sec;
00126     GoogleString name;
00127   };
00128 
00129   struct DirInfo {
00130     DirInfo() : size_bytes(0), inode_count(0) { }
00131 
00132     std::vector<FileInfo> files;
00133     StringVector empty_dirs;
00134     int64 size_bytes;
00135     int64 inode_count;
00136   };
00137 
00143   virtual int MaxPathLength(const StringPiece& base) const;
00144 
00148   virtual bool ReadFile(const char* filename,
00149                         GoogleString* buffer,
00150                         MessageHandler* handler);
00151   virtual bool ReadFile(const char* filename,
00152                         Writer* writer,
00153                         MessageHandler* handler);
00154   virtual bool ReadFile(InputFile* input_file,
00155                         GoogleString* buffer,
00156                         MessageHandler* handler);
00157   virtual bool ReadFile(InputFile* input_file,
00158                         Writer* writer,
00159                         MessageHandler* handler);
00160   virtual bool WriteFile(const char* filename,
00161                          const StringPiece& buffer,
00162                          MessageHandler* handler);
00165   virtual bool WriteTempFile(const StringPiece& prefix_name,
00166                              const StringPiece& buffer,
00167                              GoogleString* filename,
00168                              MessageHandler* handler);
00169 
00174   bool WriteFileAtomic(const StringPiece& filename,
00175                        const StringPiece& buffer,
00176                        MessageHandler* handler);
00177 
00178   virtual InputFile* OpenInputFile(const char* filename,
00179                                    MessageHandler* handler) = 0;
00181   OutputFile* OpenOutputFile(const char* filename,
00182                              MessageHandler* handler) {
00183     SetupFileDir(filename, handler);
00184     return OpenOutputFileHelper(filename, false, handler);
00185   }
00188   OutputFile* OpenOutputFileForAppend(const char* filename,
00189                                       MessageHandler* handler) {
00190     SetupFileDir(filename, handler);
00191     return OpenOutputFileHelper(filename, true, handler);
00192   }
00198   OutputFile* OpenTempFile(const StringPiece& prefix_name,
00199                            MessageHandler* handler) {
00200     SetupFileDir(prefix_name, handler);
00201     return OpenTempFileHelper(prefix_name, handler);
00202   }
00203 
00205   virtual bool Close(File* file, MessageHandler* handler);
00206 
00207 
00209   virtual bool RemoveFile(const char* filename, MessageHandler* handler) = 0;
00210 
00213   bool RenameFile(const char* old_filename, const char* new_filename,
00214                   MessageHandler* handler) {
00215     SetupFileDir(new_filename, handler);
00216     return RenameFileHelper(old_filename, new_filename, handler);
00217   }
00218 
00221   virtual bool MakeDir(const char* directory_path, MessageHandler* handler) = 0;
00222 
00224   virtual bool RemoveDir(const char* directory_path,
00225                          MessageHandler* handler) = 0;
00226 
00228   virtual BoolOrError Exists(const char* path, MessageHandler* handler) = 0;
00229 
00231   virtual BoolOrError IsDir(const char* path, MessageHandler* handler) = 0;
00232 
00235   virtual bool RecursivelyMakeDir(const StringPiece& directory_path,
00236                                   MessageHandler* handler);
00237 
00244   virtual bool ListContents(const StringPiece& dir, StringVector* files,
00245                             MessageHandler* handler) = 0;
00246 
00252   virtual bool Atime(const StringPiece& path, int64* timestamp_sec,
00253                      MessageHandler* handler) = 0;
00254 
00256   virtual bool Mtime(const StringPiece& path, int64* timestamp_sec,
00257                      MessageHandler* handler) = 0;
00258 
00267   virtual void GetDirInfo(const StringPiece& path, DirInfo* dirinfo,
00268                           MessageHandler* handler);
00269 
00278   virtual bool Size(const StringPiece& path, int64* size,
00279                     MessageHandler* handler) = 0;
00280 
00286   virtual BoolOrError TryLock(const StringPiece& lock_name,
00287                               MessageHandler* handler) = 0;
00288 
00294   virtual BoolOrError TryLockWithTimeout(const StringPiece& lock_name,
00295                                          int64 timeout_millis,
00296                                          MessageHandler* handler) {
00297     return TryLock(lock_name, handler);
00298   }
00299 
00305   virtual bool Unlock(const StringPiece& lock_name,
00306                       MessageHandler* handler) = 0;
00307 
00308  protected:
00311   virtual OutputFile* OpenOutputFileHelper(const char* filename,
00312                                            bool append,
00313                                            MessageHandler* handler) = 0;
00314   virtual OutputFile* OpenTempFileHelper(const StringPiece& filename,
00315                                          MessageHandler* handler) = 0;
00316   virtual bool RenameFileHelper(const char* old_filename,
00317                                 const char* new_filename,
00318                                 MessageHandler* handler) = 0;
00319 
00320  private:
00322   void SetupFileDir(const StringPiece& filename, MessageHandler* handler);
00323 };
00324 
00325 }  
00326 
00327 #endif  ///< NET_INSTAWEB_UTIL_PUBLIC_FILE_SYSTEM_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines