0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
FileUtils.h
Go to the documentation of this file.
1 /* -*- c++ -*-
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 3
9  * of the License, or any later version.
10  *
11  * Hypertable is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  * 02110-1301, USA.
20  */
21 
27 #ifndef Common_FileUtils_h
28 #define Common_FileUtils_h
29 
30 #include <Common/String.h>
31 
32 #include <mutex>
33 #include <vector>
34 
35 extern "C" {
36 #include <dirent.h>
37 #include <sys/socket.h>
38 #include <sys/types.h>
39 }
40 
41 namespace Hypertable {
42 
51  class FileUtils {
52  public:
59  static bool read(const String &fname, String &contents);
60 
68  static ssize_t read(int fd, void *vptr, size_t n);
69 
78  static ssize_t pread(int fd, void *vptr, size_t n, off_t offset);
79 
87  static ssize_t write(const String &fname, const std::string &contents);
88 
96  static ssize_t write(int fd, const void *vptr, size_t n);
97 
104  static ssize_t write(int fd, const std::string &str) {
105  return write(fd, str.c_str(), str.length());
106  }
107 
120  static ssize_t writev(int fd, const struct iovec *vector, int count);
121 
133  static ssize_t sendto(int fd, const void *vptr, size_t n,
134  const sockaddr *to, socklen_t tolen);
135 
143  static ssize_t send(int fd, const void *vptr, size_t n);
144 
155  static ssize_t recvfrom(int fd, void *vptr, size_t n,
156  struct sockaddr *from, socklen_t *fromlen);
157 
165  static ssize_t recv(int fd, void *vptr, size_t n);
166 
173  static bool set_flags(int fd, int flags);
174 
183  static char *file_to_buffer(const String &fname, off_t *lenp);
184 
190  static String file_to_string(const String &fname);
191 
199  static void *mmap(const String &fname, off_t *lenp);
200 
206  static bool mkdirs(const String &dirname);
207 
212  static bool exists(const String &fname);
213 
218  static bool unlink(const String &fname);
219 
226  static bool rename(const String &oldpath, const String &newpath);
227 
233  static uint64_t size(const String &fname);
234 
240  static off_t length(const String &fname);
241 
243  static void add_trailing_slash(String &path);
244 
254  static bool expand_tilde(String &fname);
255 
265  static void readdir(const String &dirname, const String &fname_regex,
266  std::vector<struct dirent> &listing);
267 
270  };
271 
274 }
275 
276 #endif // Common_FileUtils_h
277 
static std::mutex mutex
Definition: Logger.cc:43
static char * file_to_buffer(const String &fname, off_t *lenp)
Reads a full file into a new buffer; the buffer is allocated with operator new[], and the caller has ...
Definition: FileUtils.cc:282
static std::mutex ms_mutex
Mutex for protecting thread-unsafe glibc library function calls.
Definition: FileUtils.h:269
static bool read(const String &fname, String &contents)
Reads a whole file into a String.
Definition: FileUtils.cc:59
std::string String
A String is simply a typedef to std::string.
Definition: String.h:44
static bool unlink(const String &fname)
Unlinks (deletes) a file or directory.
Definition: FileUtils.cc:427
static off_t length(const String &fname)
Returns the size of a file (-1 on error)
Definition: FileUtils.cc:458
The FileUtils class provides static functions to easily access and handle files and the file system...
Definition: FileUtils.h:51
Po::typed_value< String > * str(String *v=0)
Definition: Properties.h:166
static ssize_t write(const String &fname, const std::string &contents)
Writes a String buffer to a file; the file is overwritten if it already exists.
Definition: FileUtils.cc:124
static void add_trailing_slash(String &path)
Adds a trailing slash to a path.
Definition: FileUtils.cc:466
static bool exists(const String &fname)
Checks if a file or directory exists.
Definition: FileUtils.cc:420
static bool mkdirs(const String &dirname)
Creates a directory (with all parent directories, if required)
Definition: FileUtils.cc:366
static ssize_t recvfrom(int fd, void *vptr, size_t n, struct sockaddr *from, socklen_t *fromlen)
Receives data from a network connection and returns the sender's address.
Definition: FileUtils.cc:227
static ssize_t pread(int fd, void *vptr, size_t n, off_t offset)
Reads positional data from a file descriptor into a buffer.
Definition: FileUtils.cc:97
static ssize_t sendto(int fd, const void *vptr, size_t n, const sockaddr *to, socklen_t tolen)
Sends data through a network connection; if the socket is TCP then the address is ignored...
Definition: FileUtils.cc:178
static ssize_t send(int fd, const void *vptr, size_t n)
Sends data through a network connection.
Definition: FileUtils.cc:203
static uint64_t size(const String &fname)
Returns the size of a file (0 on error)
Definition: FileUtils.cc:450
static ssize_t write(int fd, const std::string &str)
Writes a string to a file descriptor.
Definition: FileUtils.h:104
static bool expand_tilde(String &fname)
Expands a leading tilde character in a filename.
Definition: FileUtils.cc:472
static bool set_flags(int fd, int flags)
Sets fcntl flags of a socket.
Definition: FileUtils.cc:256
Hypertable definitions
static void readdir(const String &dirname, const String &fname_regex, std::vector< struct dirent > &listing)
Reads all directory entries, applies a regular expression and returns those which match...
Definition: FileUtils.cc:511
static void * mmap(const String &fname, off_t *lenp)
Maps a full file into memory using mmap; the mapping will be released when the application terminates...
Definition: FileUtils.cc:343
static ssize_t writev(int fd, const struct iovec *vector, int count)
Atomically writes data from multiple buffers to a file descriptor.
Definition: FileUtils.cc:162
A String class based on std::string.
static bool rename(const String &oldpath, const String &newpath)
Renames a file or directory.
Definition: FileUtils.cc:438
static ssize_t recv(int fd, void *vptr, size_t n)
Receives data from a network connection.
Definition: FileUtils.cc:242
static String file_to_string(const String &fname)
Reads a full file into a String.
Definition: FileUtils.cc:333