X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2FFile.h;h=958347d2b40d96e38164ccb7d2e4bafbc8a80121;hb=2fef5f704ab8cb3fb8f5643a23ae36d32969020d;hp=edcad92025bcf996ffd4ed66dd77766c9f414520;hpb=9d6c66ddc8f6f38eef4c6f1c09abb3b30f888057;p=folly.git diff --git a/folly/File.h b/folly/File.h index edcad920..958347d2 100644 --- a/folly/File.h +++ b/folly/File.h @@ -1,5 +1,5 @@ /* - * Copyright 2013 Facebook, Inc. + * Copyright 2017 Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,12 +14,20 @@ * limitations under the License. */ -#ifndef FOLLY_FILE_H_ -#define FOLLY_FILE_H_ +#pragma once +#include #include #include -#include + +#include +#include + +#include +#include +#include +#include +#include namespace folly { @@ -31,21 +39,36 @@ class File { /** * Creates an empty File object, for late initialization. */ - File(); + File() noexcept; /** * 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) noexcept; /** * Open and create a file object. Throws on error. + * Owns the file descriptor implicitly. */ - /* 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); + + /** + * All the constructors that are not noexcept can throw std::system_error. + * This is a helper method to use folly::Expected to chain a file open event + * to something else you want to do with the open fd. + */ + template + static Expected makeFile(Args&&... args) noexcept { + try { + return File(std::forward(args)...); + } catch (const std::system_error& se) { + return makeUnexpected(exception_wrapper(std::current_exception(), se)); + } + } ~File(); @@ -54,14 +77,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 +86,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 +107,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 +118,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 +151,4 @@ class File { void swap(File& a, File& b); -} // namespace folly - -#endif /* FOLLY_FILE_H_ */ +} // namespace folly