• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
34 #include <google/protobuf/testing/file.h>
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 
48 #include <google/protobuf/io/io_win32.h>
49 #include <google/protobuf/stubs/logging.h>
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
62 using google::protobuf::io::win32::access;
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 
Exists(const string & name)69 bool File::Exists(const string& name) {
70   return access(name.c_str(), F_OK) == 0;
71 }
72 
ReadFileToString(const string & name,string * output,bool text_mode)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 
ReadFileToStringOrDie(const string & name,string * output)89 void File::ReadFileToStringOrDie(const string& name, string* output) {
90   GOOGLE_CHECK(ReadFileToString(name, output)) << "Could not read: " << name;
91 }
92 
WriteStringToFile(const string & contents,const string & name)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 
WriteStringToFileOrDie(const string & contents,const string & name)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 
CreateDir(const string & name,int mode)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 
RecursivelyCreateDir(const string & path,int mode)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 
DeleteRecursively(const string & name,void * dummy1,void * dummy2)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) {
169         DeleteRecursively(path, NULL, NULL);
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 
ChangeWorkingDirectory(const string & new_working_directory)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
213