file.cc
Go to the documentation of this file.
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 // Author: kenton@google.com (Kenton Varda)
32 // emulates google3/file/base/file.cc
33 
35 #include <stdio.h>
36 #include <sys/stat.h>
37 #include <sys/types.h>
38 #ifdef _MSC_VER
39 #define WIN32_LEAN_AND_MEAN // yeah, right
40 #include <windows.h> // Find*File(). :(
41 // #include <direct.h>
42 #else
43 #include <dirent.h>
44 #include <unistd.h>
45 #endif
46 #include <errno.h>
47 
50 
51 namespace google {
52 namespace protobuf {
53 
54 #ifdef _WIN32
55 // Windows doesn't have symbolic links.
56 #define lstat stat
57 // DO NOT include <io.h>, instead create functions in io_win32.{h,cc} and import
58 // them like we do below.
59 #endif
60 
61 #ifdef _WIN32
63 using google::protobuf::io::win32::chdir;
64 using google::protobuf::io::win32::fopen;
65 using google::protobuf::io::win32::mkdir;
66 using google::protobuf::io::win32::stat;
67 #endif
68 
69 bool File::Exists(const string& name) {
70  return access(name.c_str(), F_OK) == 0;
71 }
72 
73 bool File::ReadFileToString(const string& name, string* output, bool text_mode) {
74  char buffer[1024];
75  FILE* file = fopen(name.c_str(), text_mode ? "rt" : "rb");
76  if (file == NULL) return false;
77 
78  while (true) {
79  size_t n = fread(buffer, 1, sizeof(buffer), file);
80  if (n <= 0) break;
81  output->append(buffer, n);
82  }
83 
84  int error = ferror(file);
85  if (fclose(file) != 0) return false;
86  return error == 0;
87 }
88 
89 void File::ReadFileToStringOrDie(const string& name, string* output) {
90  GOOGLE_CHECK(ReadFileToString(name, output)) << "Could not read: " << name;
91 }
92 
93 bool File::WriteStringToFile(const string& contents, const string& name) {
94  FILE* file = fopen(name.c_str(), "wb");
95  if (file == NULL) {
96  GOOGLE_LOG(ERROR) << "fopen(" << name << ", \"wb\"): " << strerror(errno);
97  return false;
98  }
99 
100  if (fwrite(contents.data(), 1, contents.size(), file) != contents.size()) {
101  GOOGLE_LOG(ERROR) << "fwrite(" << name << "): " << strerror(errno);
102  fclose(file);
103  return false;
104  }
105 
106  if (fclose(file) != 0) {
107  return false;
108  }
109  return true;
110 }
111 
112 void File::WriteStringToFileOrDie(const string& contents, const string& name) {
113  FILE* file = fopen(name.c_str(), "wb");
114  GOOGLE_CHECK(file != NULL)
115  << "fopen(" << name << ", \"wb\"): " << strerror(errno);
116  GOOGLE_CHECK_EQ(fwrite(contents.data(), 1, contents.size(), file),
117  contents.size())
118  << "fwrite(" << name << "): " << strerror(errno);
119  GOOGLE_CHECK(fclose(file) == 0)
120  << "fclose(" << name << "): " << strerror(errno);
121 }
122 
123 bool File::CreateDir(const string& name, int mode) {
124  if (!name.empty()) {
125  GOOGLE_CHECK_OK(name[name.size() - 1] != '.');
126  }
127  return mkdir(name.c_str(), mode) == 0;
128 }
129 
130 bool File::RecursivelyCreateDir(const string& path, int mode) {
131  if (CreateDir(path, mode)) return true;
132 
133  if (Exists(path)) return false;
134 
135  // Try creating the parent.
136  string::size_type slashpos = path.find_last_of('/');
137  if (slashpos == string::npos) {
138  // No parent given.
139  return false;
140  }
141 
142  return RecursivelyCreateDir(path.substr(0, slashpos), mode) &&
143  CreateDir(path, mode);
144 }
145 
146 void File::DeleteRecursively(const string& name,
147  void* dummy1, void* dummy2) {
148  if (name.empty()) return;
149 
150  // We don't care too much about error checking here since this is only used
151  // in tests to delete temporary directories that are under /tmp anyway.
152 
153 #ifdef _MSC_VER
154  // This interface is so weird.
155  WIN32_FIND_DATAA find_data;
156  HANDLE find_handle = FindFirstFileA((name + "/*").c_str(), &find_data);
157  if (find_handle == INVALID_HANDLE_VALUE) {
158  // Just delete it, whatever it is.
159  DeleteFileA(name.c_str());
160  RemoveDirectoryA(name.c_str());
161  return;
162  }
163 
164  do {
165  string entry_name = find_data.cFileName;
166  if (entry_name != "." && entry_name != "..") {
167  string path = name + "/" + entry_name;
168  if (find_data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
170  RemoveDirectoryA(path.c_str());
171  } else {
172  DeleteFileA(path.c_str());
173  }
174  }
175  } while(FindNextFileA(find_handle, &find_data));
176  FindClose(find_handle);
177 
178  RemoveDirectoryA(name.c_str());
179 #else
180  // Use opendir()! Yay!
181  // lstat = Don't follow symbolic links.
182  struct stat stats;
183  if (lstat(name.c_str(), &stats) != 0) return;
184 
185  if (S_ISDIR(stats.st_mode)) {
186  DIR* dir = opendir(name.c_str());
187  if (dir != NULL) {
188  while (true) {
189  struct dirent* entry = readdir(dir);
190  if (entry == NULL) break;
191  string entry_name = entry->d_name;
192  if (entry_name != "." && entry_name != "..") {
193  DeleteRecursively(name + "/" + entry_name, NULL, NULL);
194  }
195  }
196  }
197 
198  closedir(dir);
199  rmdir(name.c_str());
200 
201  } else if (S_ISREG(stats.st_mode)) {
202  remove(name.c_str());
203  }
204 #endif
205 }
206 
207 bool File::ChangeWorkingDirectory(const string& new_working_directory) {
208  return chdir(new_working_directory.c_str()) == 0;
209 }
210 
211 } // namespace protobuf
212 } // namespace google
GOOGLE_CHECK_EQ
#define GOOGLE_CHECK_EQ(A, B)
Definition: logging.h:156
name
GLuint const GLchar * name
Definition: glcorearb.h:3055
NULL
NULL
Definition: test_security_zap.cpp:405
readdir
static struct dirent * readdir(DIR *dirp)
Definition: dirent.h:732
mode
GLenum mode
Definition: glcorearb.h:2764
dirent::d_name
char d_name[PATH_MAX+1]
Definition: dirent.h:278
errno
int errno
google::protobuf::File::WriteStringToFileOrDie
static void WriteStringToFileOrDie(const string &contents, const string &name)
Definition: file.cc:112
error
Definition: cJSON.c:88
closedir
static int closedir(DIR *dirp)
Definition: dirent.h:843
google::protobuf::File::WriteStringToFile
static bool WriteStringToFile(const string &contents, const string &name)
Definition: file.cc:93
S_ISREG
#define S_ISREG(mode)
Definition: dirent.h:193
access
GLuint GLint GLboolean GLint GLenum access
Definition: glcorearb.h:4266
dirent.h
path
GLsizei const GLchar ** path
Definition: glcorearb.h:3658
google::protobuf::File::ReadFileToStringOrDie
static void ReadFileToStringOrDie(const string &name, string *output)
Definition: file.cc:89
dirent
Definition: dirent.h:261
GOOGLE_LOG
#define GOOGLE_LOG(LEVEL)
Definition: logging.h:146
buffer
Definition: buffer_processor.h:43
google::protobuf::ERROR
static const LogLevel ERROR
Definition: protobuf/src/google/protobuf/testing/googletest.h:70
S_ISDIR
#define S_ISDIR(mode)
Definition: dirent.h:190
google::protobuf::File::ReadFileToString
static bool ReadFileToString(const string &name, string *output, bool text_mode=false)
Definition: file.cc:73
GOOGLE_CHECK
#define GOOGLE_CHECK(EXPRESSION)
Definition: logging.h:153
n
GLdouble n
Definition: glcorearb.h:4153
DIR
Definition: dirent.h:282
GOOGLE_CHECK_OK
#define GOOGLE_CHECK_OK(A)
Definition: logging.h:155
google::protobuf::File::RecursivelyCreateDir
static bool RecursivelyCreateDir(const string &path, int mode)
Definition: file.cc:130
logging.h
google::protobuf::File::ChangeWorkingDirectory
static bool ChangeWorkingDirectory(const string &new_working_directory)
Definition: file.cc:207
HANDLE
void * HANDLE
Definition: wepoll.c:70
strerror
char * strerror(int errno)
io_win32.h
opendir
static DIR * opendir(const char *dirname)
Definition: dirent.h:676
file.h
output
const upb_json_parsermethod const upb_symtab upb_sink * output
Definition: ruby/ext/google/protobuf_c/upb.h:10503
google::protobuf::File::CreateDir
static bool CreateDir(const string &name, int mode)
Definition: file.cc:123
google
Definition: data_proto2_to_proto3_util.h:11
google::protobuf::File::DeleteRecursively
static void DeleteRecursively(const string &name, void *dummy1, void *dummy2)
Definition: file.cc:146
dummy2
int dummy2
Definition: register_benchmark_test.cc:91
google::protobuf::File::Exists
static bool Exists(const string &name)
Definition: file.cc:69


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:51