#include "mem_file_system.h"
Public Member Functions | |
MemFileSystem (ThreadSystem *threads, Timer *timer) | |
virtual InputFile * | OpenInputFile (const char *filename, MessageHandler *message_handler) |
virtual OutputFile * | OpenOutputFileHelper (const char *filename, MessageHandler *message_handler) |
virtual OutputFile * | OpenTempFileHelper (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 | 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) |
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, 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 |
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.
bool net_instaweb::MemFileSystem::advance_time_on_update | ( | ) | [inline] |
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 | |||
) | [virtual] |
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.
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 | |||
) | [virtual] |
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 | |||
) | [virtual] |
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, | |
MessageHandler * | handler | |||
) | [virtual] |
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 | |||
) | [virtual] |
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.
virtual bool net_instaweb::MemFileSystem::Size | ( | const StringPiece & | path, | |
int64 * | size, | |||
MessageHandler * | handler | |||
) | [virtual] |
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.
Implements net_instaweb::FileSystem.
virtual BoolOrError net_instaweb::MemFileSystem::TryLock | ( | const StringPiece & | lock_name, | |
MessageHandler * | handler | |||
) | [virtual] |
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, | |||
MessageHandler * | handler | |||
) | [virtual] |
Like TryLock, but may attempt to break the lock if it appears to be staler than the given number of milliseconds. (The default implementation never actually breaks locks.) 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>
Reimplemented from net_instaweb::FileSystem.
virtual bool net_instaweb::MemFileSystem::Unlock | ( | const StringPiece & | lock_name, | |
MessageHandler * | handler | |||
) | [virtual] |
Attempts to release a lock previously obtained through TryLock. If your thread did not prevously 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.