X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2FFile.h;h=459251428d370b37431e191493b55f57f72000d5;hb=140c62d25d930cdbdacaa337d254a2471875a4be;hp=edcad92025bcf996ffd4ed66dd77766c9f414520;hpb=9d6c66ddc8f6f38eef4c6f1c09abb3b30f888057;p=folly.git diff --git a/folly/File.h b/folly/File.h index edcad920..45925142 100644 --- a/folly/File.h +++ b/folly/File.h @@ -1,5 +1,5 @@ /* - * Copyright 2013 Facebook, Inc. + * Copyright 2015 Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,9 +17,15 @@ #ifndef FOLLY_FILE_H_ #define FOLLY_FILE_H_ +#include #include #include -#include +#include + +#include + +#include +#include namespace folly { @@ -37,15 +43,15 @@ class File { * Create a File object from an existing file descriptor. * Takes ownership of the file descriptor if ownsFd is true. */ - /* implicit */ File(int fd, - bool ownsFd = false); + explicit File(int fd, bool ownsFd = false); /** * Open and create a file object. Throws on error. */ - /* implicit */ File(const char* name, - int flags = O_RDONLY, - mode_t mode = 0644); + explicit File(const char* name, int flags = O_RDONLY, mode_t mode = 0666); + explicit File( + const std::string& name, int flags = O_RDONLY, mode_t mode = 0666); + explicit File(StringPiece name, int flags = O_RDONLY, mode_t mode = 0666); ~File(); @@ -54,14 +60,6 @@ class File { */ static File temporary(); - /** - * Attempts to open the file at the given path. Returns an 'closed` File - * instance on failure, which will evaluate to false. - */ - static File tryOpen(const char* name, - int flags = O_RDONLY, - mode_t mode = 0644); - /** * Return the file descriptor, or -1 if the file was closed. */ @@ -71,9 +69,14 @@ class File { * Returns 'true' iff the file was successfully opened. */ explicit operator bool() const { - return fd_ >= 0; + return fd_ != -1; } + /** + * Duplicate file descriptor and return File that owns it. + */ + File dup() const; + /** * If we own the file descriptor, close the file and throw on error. * Otherwise, do nothing. @@ -87,9 +90,10 @@ class File { bool closeNoThrow(); /** - * Releases the file descriptor; no longer owned by this File. + * Returns and releases the file descriptor; no longer owned by this File. + * Returns -1 if the File object didn't wrap a file. */ - void release(); + int release() noexcept; /** * Swap this File with another. @@ -97,10 +101,29 @@ class File { void swap(File& other); // movable - File(File&&); + File(File&&) noexcept; File& operator=(File&&); + // FLOCK (INTERPROCESS) LOCKS + // + // NOTE THAT THESE LOCKS ARE flock() LOCKS. That is, they may only be used + // for inter-process synchronization -- an attempt to acquire a second lock + // on the same file descriptor from the same process may succeed. Attempting + // to acquire a second lock on a different file descriptor for the same file + // should fail, but some systems might implement flock() using fcntl() locks, + // in which case it will succeed. + void lock(); + bool try_lock(); + void unlock(); + + void lock_shared(); + bool try_lock_shared(); + void unlock_shared(); + private: + void doLock(int op); + bool doTryLock(int op); + // unique File(const File&) = delete; File& operator=(const File&) = delete; @@ -111,6 +134,7 @@ class File { void swap(File& a, File& b); + } // namespace folly #endif /* FOLLY_FILE_H_ */