/*
- * Copyright 2015 Facebook, Inc.
+ * Copyright 2013-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
#include <folly/File.h>
-#include <fcntl.h>
-#include <unistd.h>
#include <folly/Exception.h>
#include <folly/FileUtil.h>
#include <folly/Format.h>
#include <folly/ScopeGuard.h>
+#include <folly/portability/Fcntl.h>
+#include <folly/portability/SysFile.h>
+#include <folly/portability/Unistd.h>
#include <system_error>
namespace folly {
-File::File()
- : fd_(-1)
- , ownsFd_(false)
-{}
+File::File() noexcept : fd_(-1), ownsFd_(false) {}
-File::File(int fd, bool ownsFd)
- : fd_(fd)
- , ownsFd_(ownsFd) {
+File::File(int fd, bool ownsFd) noexcept : fd_(fd), ownsFd_(ownsFd) {
CHECK_GE(fd, -1) << "fd must be -1 or non-negative";
CHECK(fd != -1 || !ownsFd) << "cannot own -1";
}
ownsFd_ = true;
}
+File::File(const std::string& name, int flags, mode_t mode)
+ : File(name.c_str(), flags, mode) {}
+
+File::File(StringPiece name, int flags, mode_t mode)
+ : File(name.str(), flags, mode) {}
+
File::File(File&& other) noexcept
: fd_(other.fd_)
, ownsFd_(other.ownsFd_) {
bool File::doTryLock(int op) {
int r = flockNoInt(fd_, op | LOCK_NB);
// flock returns EWOULDBLOCK if already locked
- if (r == -1 && errno == EWOULDBLOCK) return false;
+ if (r == -1 && errno == EWOULDBLOCK) {
+ return false;
+ }
checkUnixError(r, "flock() failed (try_lock)");
return true;
}
}
void File::unlock_shared() { unlock(); }
-} // namespace folly
+} // namespace folly