X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2FFileUtil.h;h=0c23cd96a3377d300bb3698185375971b0e4bfa7;hb=fe5831207fbe073b22c2b6bd9ed66f62d568eb2c;hp=71d8e2551274089e9cf1cec503557790ccaf3168;hpb=c182b25f363bd80a8043d8616dec9dff8cf8736b;p=folly.git diff --git a/folly/FileUtil.h b/folly/FileUtil.h index 71d8e255..0c23cd96 100644 --- a/folly/FileUtil.h +++ b/folly/FileUtil.h @@ -1,5 +1,5 @@ /* - * Copyright 2014 Facebook, Inc. + * Copyright 2016 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,20 +14,19 @@ * limitations under the License. */ -#ifndef FOLLY_FILEUTIL_H_ -#define FOLLY_FILEUTIL_H_ +#pragma once #include #include #include +#include +#include +#include #include #include #include #include -#include -#include -#include namespace folly { @@ -81,9 +80,7 @@ ssize_t writevNoInt(int fd, const iovec* iov, int count); ssize_t readFull(int fd, void* buf, size_t n); ssize_t preadFull(int fd, void* buf, size_t n, off_t offset); ssize_t readvFull(int fd, iovec* iov, int count); -#if FOLLY_HAVE_PREADV ssize_t preadvFull(int fd, iovec* iov, int count, off_t offset); -#endif /** * Similar to readFull and preadFull above, wrappers around write() and @@ -102,9 +99,7 @@ ssize_t preadvFull(int fd, iovec* iov, int count, off_t offset); ssize_t writeFull(int fd, const void* buf, size_t n); ssize_t pwriteFull(int fd, const void* buf, size_t n, off_t offset); ssize_t writevFull(int fd, iovec* iov, int count); -#if FOLLY_HAVE_PWRITEV ssize_t pwritevFull(int fd, iovec* iov, int count, off_t offset); -#endif /** * Read entire file (if num_bytes is defaulted) or no more than @@ -117,21 +112,17 @@ ssize_t pwritevFull(int fd, iovec* iov, int count, off_t offset); * errno will be set appropriately by the failing system primitive. */ template -bool readFile(const char* file_name, Container& out, - size_t num_bytes = std::numeric_limits::max()) { +bool readFile( + int fd, + Container& out, + size_t num_bytes = std::numeric_limits::max()) { static_assert(sizeof(out[0]) == 1, "readFile: only containers with byte-sized elements accepted"); - assert(file_name); - - const auto fd = openNoInt(file_name, O_RDONLY); - if (fd == -1) return false; size_t soFar = 0; // amount of bytes successfully read SCOPE_EXIT { - assert(out.size() >= soFar); // resize better doesn't throw + DCHECK(out.size() >= soFar); // resize better doesn't throw out.resize(soFar); - // Ignore errors when closing the file - closeNoInt(fd); }; // Obtain file size: @@ -166,6 +157,29 @@ bool readFile(const char* file_name, Container& out, return true; } +/** + * Same as above, but takes in a file name instead of fd + */ +template +bool readFile( + const char* file_name, + Container& out, + size_t num_bytes = std::numeric_limits::max()) { + DCHECK(file_name); + + const auto fd = openNoInt(file_name, O_RDONLY); + if (fd == -1) { + return false; + } + + SCOPE_EXIT { + // Ignore errors when closing the file + closeNoInt(fd); + }; + + return readFile(fd, out, num_bytes); +} + /** * Writes container to file. The container is assumed to be * contiguous, with element size equal to 1, and offering STL-like @@ -188,10 +202,8 @@ bool writeFile(const Container& data, const char* filename, return false; } bool ok = data.empty() || - writeFull(fd, &data[0], data.size()) == data.size(); + writeFull(fd, &data[0], data.size()) == static_cast(data.size()); return closeNoInt(fd) == 0 && ok; } } // namespaces - -#endif /* FOLLY_FILEUTIL_H_ */