Page Speed Optimization Libraries
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Public Types | Public Member Functions | List of all members
net_instaweb::MemFileSystem Class Reference

#include "mem_file_system.h"

Inheritance diagram for net_instaweb::MemFileSystem:

Public Types

typedef Callback1< const
GoogleString & > 

Public Member Functions

 MemFileSystem (ThreadSystem *threads, Timer *timer)
virtual InputFileOpenInputFile (const char *filename, MessageHandler *message_handler)
virtual OutputFileOpenOutputFileHelper (const char *filename, bool append, MessageHandler *message_handler)
virtual OutputFileOpenTempFileHelper (const StringPiece &prefix_name, MessageHandler *message_handle)
virtual bool ListContents (const StringPiece &dir, StringVector *files, MessageHandler *handler)
virtual bool MakeDir (const char *directory_path, MessageHandler *handler)
virtual bool RecursivelyMakeDir (const StringPiece &directory_path, MessageHandler *handler)
virtual bool RemoveDir (const char *path, MessageHandler *handler)
 Like POSIX 'rmdir', remove a directory only if it is empty.
virtual bool RemoveFile (const char *filename, MessageHandler *handler)
 Like POSIX 'rm'.
virtual bool RenameFileHelper (const char *old_file, const char *new_file, MessageHandler *handler)
virtual bool Atime (const StringPiece &path, int64 *timestamp_sec, MessageHandler *handler)
virtual bool Mtime (const StringPiece &path, int64 *timestamp_sec, MessageHandler *handler)
 Modified time. Time the file contents were modified.
virtual bool Size (const StringPiece &path, int64 *size, MessageHandler *handler) const
virtual BoolOrError Exists (const char *path, MessageHandler *handler)
 Like POSIX 'test -e', checks if path exists (is a file, directory, etc.).
virtual BoolOrError IsDir (const char *path, MessageHandler *handler)
 Like POSIX 'test -d', checks if path exists and refers to a directory.
virtual BoolOrError TryLock (const StringPiece &lock_name, MessageHandler *handler)
virtual BoolOrError TryLockWithTimeout (const StringPiece &lock_name, int64 timeout_ms, const Timer *timer, MessageHandler *handler)
virtual bool BumpLockTimeout (const StringPiece &lock_name, MessageHandler *handler)
virtual bool Unlock (const StringPiece &lock_name, MessageHandler *handler)
void set_atime_enabled (bool enabled)
 When atime is disabled, reading a file will not update its atime.
bool advance_time_on_update ()
void set_advance_time_on_update (bool x, MockTimer *mock_timer)
void Clear ()
void Disable ()
 Test-specific functionality to disable and re-enable the filesystem.
void Enable ()
void ClearStats ()
 Access statistics.
int num_input_file_opens () const
int num_input_file_stats () const
 returns number of times MTime was called.
int num_output_file_opens () const
int num_temp_file_opens () const
void set_write_callback (FileCallback *x)
virtual bool WriteFile (const char *filename, const StringPiece &buffer, MessageHandler *handler)
 Non-atomic. Use WriteFileAtomic() for atomic version.
virtual bool WriteTempFile (const StringPiece &prefix_name, const StringPiece &buffer, GoogleString *filename, MessageHandler *handler)
- Public Member Functions inherited from net_instaweb::FileSystem
virtual int MaxPathLength (const StringPiece &base) const
virtual bool ReadFile (const char *filename, int64 max_file_size, Writer *writer, MessageHandler *handler)
virtual bool ReadFile (InputFile *input_file, int64 max_file_size, Writer *writer, MessageHandler *handler)
virtual bool ReadFile (const char *filename, int64 max_file_size, GoogleString *buffer, MessageHandler *handler)
virtual bool ReadFile (InputFile *input_file, int64 max_file_size, GoogleString *buffer, MessageHandler *handler)
virtual bool ReadFile (const char *filename, GoogleString *buffer, MessageHandler *handler)
virtual bool ReadFile (InputFile *input_file, GoogleString *buffer, MessageHandler *handler)
virtual bool ReadFile (const char *filename, Writer *writer, MessageHandler *handler)
virtual bool ReadFile (InputFile *input_file, Writer *writer, MessageHandler *handler)
bool WriteFileAtomic (const StringPiece &filename, const StringPiece &buffer, MessageHandler *handler)
OutputFileOpenOutputFile (const char *filename, MessageHandler *handler)
 Automatically creates sub-directories to filename.
OutputFileOpenOutputFileForAppend (const char *filename, MessageHandler *handler)
OutputFileOpenTempFile (const StringPiece &prefix_name, MessageHandler *handler)
virtual bool Close (File *file, MessageHandler *handler)
 Closes the File and cleans up memory.
bool RenameFile (const char *old_filename, const char *new_filename, MessageHandler *handler)
void GetDirInfo (const StringPiece &path, DirInfo *dirinfo, MessageHandler *handler)
virtual void GetDirInfoWithProgress (const StringPiece &path, DirInfo *dirinfo, ProgressNotifier *notifier, MessageHandler *handler)

Additional Inherited Members

- Static Public Attributes inherited from net_instaweb::FileSystem
static const int64 kUnlimitedSize = -1
 This is documented as -1 in user-facing documentation, so don't change it.

Detailed Description

An in-memory implementation of the FileSystem interface. This was originally for use in unit tests; but can also host the lock manager if needed. Does not fully support directories. Not particularly efficient. Not threadsafe except for lock methods.

TODO(abliss): add an ability to block writes for arbitrarily long, to enable testing resilience to concurrency problems with real filesystems.
TODO(jmarantz): make threadsafe.

Member Function Documentation

bool net_instaweb::MemFileSystem::advance_time_on_update ( )

In order to test file-system 'atime' code, we need to move mock time forward during tests by an entire second (aka 1000 ms). However, that's disruptive to other tests that try to use mock-time to examine millisecond-level timing, so we leave this behavior off by default.

virtual bool net_instaweb::MemFileSystem::Atime ( const StringPiece &  path,
int64 *  timestamp_sec,
MessageHandler handler 

We offer a "simulated atime" in which the clock ticks forward one second every time you read or write a file.

Implements net_instaweb::FileSystem.

virtual bool net_instaweb::MemFileSystem::BumpLockTimeout ( const StringPiece &  lock_name,
MessageHandler handler 

If you're holding a lock for a long running task you want to avoid someone else receiving the lock if they request it with TryLockWithTimeout because you've been working for longer than the timeout, you should bump it often enough that it doesn't expire.

Default implementation does nothing, since the default implementation of TryLockWithTimeout doesn't do anything either.

Reimplemented from net_instaweb::FileSystem.

void net_instaweb::MemFileSystem::Clear ( )

Empties out the entire filesystem. Should not be called while files are open.

virtual bool net_instaweb::MemFileSystem::ListContents ( const StringPiece &  dir,
StringVector *  files,
MessageHandler handler 

Like POSIX 'ls -a', lists all files and directories under the given directory (but omits "." and ".."). Full paths (not just filenames) will be pushed onto the back of the supplied vector (without clearing it). Returns true on success (even if the dir was empty), false on error (even if some files were pushed onto the vector). This is generally not threadsafe! Use a mutex.

Implements net_instaweb::FileSystem.

virtual bool net_instaweb::MemFileSystem::MakeDir ( const char *  directory_path,
MessageHandler handler 

Like POSIX 'mkdir', makes a directory only if parent directory exists. Fails if directory_name already exists or parent directory doesn't exist.

Implements net_instaweb::FileSystem.

virtual OutputFile* net_instaweb::MemFileSystem::OpenOutputFileHelper ( const char *  filename,
bool  append,
MessageHandler handler 

These interfaces must be defined by implementers of FileSystem. They may assume the directory already exists.

Implements net_instaweb::FileSystem.

virtual bool net_instaweb::MemFileSystem::RecursivelyMakeDir ( const StringPiece &  directory_path,
MessageHandler handler 

Like POSIX 'mkdir -p', makes all directories up to this one recursively. Fails if we do not have permission to make any directory in chain.

Reimplemented from net_instaweb::FileSystem.

void net_instaweb::MemFileSystem::set_write_callback ( FileCallback x)

Adds a callback to be called once after a file-write and then deleted.

This is intended primarily for testing, and thus is not on the base class.

virtual bool net_instaweb::MemFileSystem::Size ( const StringPiece &  path,
int64 *  size,
MessageHandler handler 
) const

Given a file, computes its size in bytes and store it in *size. Returns true on success, false on failure. Behavior is undefined if path refers to a directory. This function has different behavior depending on the underlying implementation. Memory-based implementations will report the size of the file, while disk-based implementations should return the actual allocated size on disk.

TODO(abliss): replace this with a single Stat() function.

Implements net_instaweb::FileSystem.

virtual BoolOrError net_instaweb::MemFileSystem::TryLock ( const StringPiece &  lock_name,
MessageHandler handler 

Attempts to obtain a global (cross-process, cross-thread) lock of the given name (which should be a valid filename, not otherwise used, in an extant directory). If someone else has this lock, returns False immediately. If anything goes wrong, returns Error. On success, returns True: then you must call Unlock when you are done.

Implements net_instaweb::FileSystem.

virtual BoolOrError net_instaweb::MemFileSystem::TryLockWithTimeout ( const StringPiece &  lock_name,
int64  timeout_millis,
const Timer timer,
MessageHandler handler 

Like TryLock, but may attempt to break stale locks, though the default implementation never actually breaks any. A lock is stale if it was taken (or last bumped) more than timeout_millis ms ago.

If you obtain a lock through this method, there are no hard guarantees that nobody else has it too. <blink> If you use this function, your lock becomes "best-effort". </blink>

If you override this function, you need to override BumpLockTimeout as well.

Reimplemented from net_instaweb::FileSystem.

virtual bool net_instaweb::MemFileSystem::Unlock ( const StringPiece &  lock_name,
MessageHandler handler 

Attempts to release a lock previously obtained through TryLock. If your thread did not previously obtain the lock, the behavior is undefined. Returns true if we successfully release the lock. Returns false if we were unable to release the lock (e.g. somebody came along and write-protected the lockfile). You might try again, or start using a different lock name.

Implements net_instaweb::FileSystem.

virtual bool net_instaweb::MemFileSystem::WriteTempFile ( const StringPiece &  prefix_name,
const StringPiece &  buffer,
GoogleString filename,
MessageHandler handler 

Writes given data to a temp file in one shot, storing the filename in filename on success. Returns false and clears filename on failure.

Reimplemented from net_instaweb::FileSystem.

The documentation for this class was generated from the following file: