Page Speed Optimization Libraries  1.13.35.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
file_system.h
Go to the documentation of this file.
1 /*
2  * Copyright 2010 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http:///www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
18 
19 #ifndef PAGESPEED_KERNEL_BASE_FILE_SYSTEM_H_
20 #define PAGESPEED_KERNEL_BASE_FILE_SYSTEM_H_
21 
22 #include <vector>
23 
27 
28 namespace net_instaweb {
29 
35 class BoolOrError {
36  enum Choice {
37  kIsFalse,
38  kIsTrue,
39  kIsError
40  };
41 
42  public:
43  BoolOrError() : choice_(kIsError) { }
44  explicit BoolOrError(bool t_or_f) : choice_(t_or_f ? kIsTrue : kIsFalse) { }
45 
47  BoolOrError(const BoolOrError& src) : choice_(src.choice_) { }
48  BoolOrError& operator=(const BoolOrError& src) {
49  if (&src != this) {
50  choice_ = src.choice_;
51  }
52  return *this;
53  }
54 
55  bool is_false() const { return choice_ == kIsFalse; }
56  bool is_true() const { return choice_ == kIsTrue; }
57  bool is_error() const { return choice_ == kIsError; }
58  void set_error() { choice_ = kIsError; }
59  void set(bool t_or_f) { choice_ = t_or_f ? kIsTrue : kIsFalse; }
60 
61  private:
62  Choice choice_;
63 };
64 
65 class MessageHandler;
66 class Timer;
67 class Writer;
68 
76 class FileSystem {
77  public:
79  static const int64 kUnlimitedSize = -1;
80 
81  virtual ~FileSystem();
82 
83  class File {
84  public:
85  virtual ~File();
86 
88  virtual const char* filename() = 0;
89 
90  protected:
92  friend class FileSystem;
93  virtual bool Close(MessageHandler* handler) = 0;
94  };
95 
96  class InputFile : public File {
97  public:
99  virtual int Read(char* buf, int size, MessageHandler* handler) = 0;
100 
105  virtual bool ReadFile(GoogleString* buf, int64 max_file_size,
106  MessageHandler* handler) = 0;
107 
108  protected:
109  friend class FileSystem;
110  virtual ~InputFile();
111  };
112 
113  class OutputFile : public File {
114  public:
117  virtual bool Write(const StringPiece& buf, MessageHandler* handler) = 0;
118  virtual bool Flush(MessageHandler* handler) = 0;
119  virtual bool SetWorldReadable(MessageHandler* handler) = 0;
120 
121  protected:
122  friend class FileSystem;
123  virtual ~OutputFile();
124  };
125 
127  public:
128  virtual void Notify() = 0;
129  virtual ~ProgressNotifier() {}
130  };
131 
133  public:
134  void Notify() override {}
135  };
136 
137  struct FileInfo {
138  FileInfo(int64 size_bytes, int64 atime_sec, const GoogleString& name)
139  : size_bytes(size_bytes), atime_sec(atime_sec), name(name) {}
140 
141  int64 size_bytes;
142  int64 atime_sec;
143  GoogleString name;
144  };
145 
146  struct DirInfo {
147  DirInfo() : size_bytes(0), inode_count(0) { }
148 
149  std::vector<FileInfo> files;
150  StringVector empty_dirs;
151  int64 size_bytes;
152  int64 inode_count;
153  };
154 
160  virtual int MaxPathLength(const StringPiece& base) const;
161 
166  virtual bool ReadFile(const char* filename,
167  int64 max_file_size,
168  Writer* writer,
169  MessageHandler* handler);
170  virtual bool ReadFile(InputFile* input_file,
171  int64 max_file_size,
172  Writer* writer,
173  MessageHandler* handler);
174  virtual bool ReadFile(const char* filename,
175  int64 max_file_size,
176  GoogleString* buffer,
177  MessageHandler* handler);
178  virtual bool ReadFile(InputFile* input_file,
179  int64 max_file_size,
180  GoogleString* buffer,
181  MessageHandler* handler);
187  virtual bool ReadFile(const char* filename,
188  GoogleString* buffer,
189  MessageHandler* handler);
190  virtual bool ReadFile(InputFile* input_file,
191  GoogleString* buffer,
192  MessageHandler* handler);
193  virtual bool ReadFile(const char* filename,
194  Writer* writer,
195  MessageHandler* handler);
196  virtual bool ReadFile(InputFile* input_file,
197  Writer* writer,
198  MessageHandler* handler);
200  virtual bool WriteFile(const char* filename,
201  const StringPiece& buffer,
202  MessageHandler* handler);
205  virtual bool WriteTempFile(const StringPiece& prefix_name,
206  const StringPiece& buffer,
207  GoogleString* filename,
208  MessageHandler* handler);
209 
213  bool WriteFileAtomic(const StringPiece& filename,
214  const StringPiece& buffer,
215  MessageHandler* handler);
216 
217  virtual InputFile* OpenInputFile(const char* filename,
218  MessageHandler* handler) = 0;
220  OutputFile* OpenOutputFile(const char* filename,
221  MessageHandler* handler) {
222  SetupFileDir(filename, handler);
223  return OpenOutputFileHelper(filename, false, handler);
224  }
227  OutputFile* OpenOutputFileForAppend(const char* filename,
228  MessageHandler* handler) {
229  SetupFileDir(filename, handler);
230  return OpenOutputFileHelper(filename, true, handler);
231  }
237  OutputFile* OpenTempFile(const StringPiece& prefix_name,
238  MessageHandler* handler) {
239  SetupFileDir(prefix_name, handler);
240  return OpenTempFileHelper(prefix_name, handler);
241  }
242 
244  virtual bool Close(File* file, MessageHandler* handler);
245 
246 
248  virtual bool RemoveFile(const char* filename, MessageHandler* handler) = 0;
249 
252  bool RenameFile(const char* old_filename, const char* new_filename,
253  MessageHandler* handler) {
254  SetupFileDir(new_filename, handler);
255  return RenameFileHelper(old_filename, new_filename, handler);
256  }
257 
260  virtual bool MakeDir(const char* directory_path, MessageHandler* handler) = 0;
261 
263  virtual bool RemoveDir(const char* directory_path,
264  MessageHandler* handler) = 0;
265 
267  virtual BoolOrError Exists(const char* path, MessageHandler* handler) = 0;
268 
270  virtual BoolOrError IsDir(const char* path, MessageHandler* handler) = 0;
271 
274  virtual bool RecursivelyMakeDir(const StringPiece& directory_path,
275  MessageHandler* handler);
276 
283  virtual bool ListContents(const StringPiece& dir, StringVector* files,
284  MessageHandler* handler) = 0;
285 
291  virtual bool Atime(const StringPiece& path, int64* timestamp_sec,
292  MessageHandler* handler) = 0;
293 
295  virtual bool Mtime(const StringPiece& path, int64* timestamp_sec,
296  MessageHandler* handler) = 0;
297 
306  void GetDirInfo(const StringPiece& path, DirInfo* dirinfo,
307  MessageHandler* handler);
308 
311  virtual void GetDirInfoWithProgress(
312  const StringPiece& path, DirInfo* dirinfo,
313  ProgressNotifier* notifier, MessageHandler* handler);
314 
323  virtual bool Size(const StringPiece& path, int64* size,
324  MessageHandler* handler) const = 0;
325 
331  virtual BoolOrError TryLock(const StringPiece& lock_name,
332  MessageHandler* handler) = 0;
333 
344  virtual BoolOrError TryLockWithTimeout(const StringPiece& lock_name,
345  int64 timeout_millis,
346  const Timer* timer,
347  MessageHandler* handler) {
348  return TryLock(lock_name, handler);
349  }
350 
355  virtual bool BumpLockTimeout(const StringPiece& lock_name,
356  MessageHandler* handler) {
359  return true;
360  }
361 
367  virtual bool Unlock(const StringPiece& lock_name,
368  MessageHandler* handler) = 0;
369 
370  protected:
373  virtual OutputFile* OpenOutputFileHelper(const char* filename,
374  bool append,
375  MessageHandler* handler) = 0;
376  virtual OutputFile* OpenTempFileHelper(const StringPiece& filename,
377  MessageHandler* handler) = 0;
378  virtual bool RenameFileHelper(const char* old_filename,
379  const char* new_filename,
380  MessageHandler* handler) = 0;
381 
382  private:
384  void SetupFileDir(const StringPiece& filename, MessageHandler* handler);
385 };
386 
387 }
388 
389 #endif
virtual BoolOrError IsDir(const char *path, MessageHandler *handler)=0
Like POSIX 'test -d', checks if path exists and refers to a directory.
void GetDirInfo(const StringPiece &path, DirInfo *dirinfo, MessageHandler *handler)
Definition: file_system.h:126
virtual bool Atime(const StringPiece &path, int64 *timestamp_sec, MessageHandler *handler)=0
virtual bool RecursivelyMakeDir(const StringPiece &directory_path, MessageHandler *handler)
virtual bool MakeDir(const char *directory_path, MessageHandler *handler)=0
virtual bool BumpLockTimeout(const StringPiece &lock_name, MessageHandler *handler)
Definition: file_system.h:355
virtual int MaxPathLength(const StringPiece &base) const
Definition: file_system.h:35
virtual bool RemoveDir(const char *directory_path, MessageHandler *handler)=0
Like POSIX 'rmdir', remove a directory only if it is empty.
virtual bool Write(const StringPiece &buf, MessageHandler *handler)=0
OutputFile * OpenTempFile(const StringPiece &prefix_name, MessageHandler *handler)
Definition: file_system.h:237
virtual bool Mtime(const StringPiece &path, int64 *timestamp_sec, MessageHandler *handler)=0
Modified time. Time the file contents were modified.
virtual bool WriteFile(const char *filename, const StringPiece &buffer, MessageHandler *handler)
Non-atomic. Use WriteFileAtomic() for atomic version.
virtual BoolOrError TryLock(const StringPiece &lock_name, MessageHandler *handler)=0
virtual bool Close(File *file, MessageHandler *handler)
Closes the File and cleans up memory.
virtual void GetDirInfoWithProgress(const StringPiece &path, DirInfo *dirinfo, ProgressNotifier *notifier, MessageHandler *handler)
OutputFile * OpenOutputFile(const char *filename, MessageHandler *handler)
Automatically creates sub-directories to filename.
Definition: file_system.h:220
static const int64 kUnlimitedSize
This is documented as -1 in user-facing documentation, so don't change it.
Definition: file_system.h:79
virtual const char * filename()=0
Gets the name of the file.
virtual BoolOrError TryLockWithTimeout(const StringPiece &lock_name, int64 timeout_millis, const Timer *timer, MessageHandler *handler)
Definition: file_system.h:344
bool RenameFile(const char *old_filename, const char *new_filename, MessageHandler *handler)
Definition: file_system.h:252
virtual bool Unlock(const StringPiece &lock_name, MessageHandler *handler)=0
std::string GoogleString
PAGESPEED_KERNEL_BASE_STRING_H_.
Definition: string.h:24
OutputFile * OpenOutputFileForAppend(const char *filename, MessageHandler *handler)
Definition: file_system.h:227
virtual OutputFile * OpenOutputFileHelper(const char *filename, bool append, MessageHandler *handler)=0
Definition: file_system.h:146
BoolOrError(const BoolOrError &src)
Intended to be passed by value; explicitly support copy & assign.
Definition: file_system.h:47
Definition: file_system.h:76
Interface for writing bytes to an output stream.
Definition: writer.h:29
bool WriteFileAtomic(const StringPiece &filename, const StringPiece &buffer, MessageHandler *handler)
virtual int Read(char *buf, int size, MessageHandler *handler)=0
Note: This returns num bytes read, NOT a success bool.
Definition: file_system.h:96
Definition: file_system.h:137
virtual bool ReadFile(const char *filename, int64 max_file_size, Writer *writer, MessageHandler *handler)
virtual bool WriteTempFile(const StringPiece &prefix_name, const StringPiece &buffer, GoogleString *filename, MessageHandler *handler)
virtual bool Size(const StringPiece &path, int64 *size, MessageHandler *handler) const =0
Definition: message_handler.h:39
virtual bool RemoveFile(const char *filename, MessageHandler *handler)=0
Like POSIX 'rm'.
virtual bool ReadFile(GoogleString *buf, int64 max_file_size, MessageHandler *handler)=0
virtual BoolOrError Exists(const char *path, MessageHandler *handler)=0
Like POSIX 'test -e', checks if path exists (is a file, directory, etc.).
Definition: file_system.h:83
Timer interface, made virtual so it can be mocked for tests.
Definition: timer.h:27
virtual bool ListContents(const StringPiece &dir, StringVector *files, MessageHandler *handler)=0
Definition: file_system.h:113