• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7 
8 // Author: kenton@google.com (Kenton Varda)
9 // emulates google3/file/base/file.h
10 
11 #ifndef GOOGLE_PROTOBUF_TESTING_FILE_H__
12 #define GOOGLE_PROTOBUF_TESTING_FILE_H__
13 
14 #include "absl/status/status.h"
15 #include "absl/strings/string_view.h"
16 #include "google/protobuf/stubs/common.h"
17 
18 namespace google {
19 namespace protobuf {
20 
21 const int DEFAULT_FILE_MODE = 0777;
22 
23 // Protocol buffer code only uses a couple static methods of File, and only
24 // in the Rust plugin and in tests.
25 class File {
26  public:
27   File(const File&) = delete;
28   File& operator=(const File&) = delete;
29 
30   // Check if the file exists.
31   static bool Exists(const std::string& name);
32 
33   // Read an entire file to a string.  Return true if successful, false
34   // otherwise.
35   static absl::Status ReadFileToString(const std::string& name,
36                                        std::string* output,
37                                        bool text_mode = false);
38 
39   // Same as above, but crash on failure.
40   static void ReadFileToStringOrDie(const std::string& name,
41                                     std::string* output);
42 
43   // Create a file and write a string to it.
44   static absl::Status WriteStringToFile(absl::string_view contents,
45                                         const std::string& name);
46 
47   // Same as above, but crash on failure.
48   static void WriteStringToFileOrDie(absl::string_view contents,
49                                      const std::string& name);
50 
51   // Create a directory.
52   static absl::Status CreateDir(const std::string& name, int mode);
53 
54   // Create a directory and all parent directories if necessary.
55   static absl::Status RecursivelyCreateDir(const std::string& path, int mode);
56 
57   // If "name" is a file, we delete it.  If it is a directory, we
58   // call DeleteRecursively() for each file or directory (other than
59   // dot and double-dot) within it, and then delete the directory itself.
60   // The "dummy" parameters have a meaning in the original version of this
61   // method but they are not used anywhere in protocol buffers.
62   static void DeleteRecursively(const std::string& name, void* dummy1,
63                                 void* dummy2);
64 
65   // Change working directory to given directory.
66   static bool ChangeWorkingDirectory(const std::string& new_working_directory);
67 
GetContents(const std::string & name,std::string * output,bool)68   static absl::Status GetContents(const std::string& name, std::string* output,
69                                   bool /*is_default*/) {
70     return ReadFileToString(name, output);
71   }
72 
GetContentsAsText(const std::string & name,std::string * output,bool)73   static absl::Status GetContentsAsText(const std::string& name,
74                                         std::string* output,
75                                         bool /*is_default*/) {
76     return ReadFileToString(name, output, true);
77   }
78 
SetContents(const std::string & name,absl::string_view contents,bool)79   static absl::Status SetContents(const std::string& name,
80                                   absl::string_view contents,
81                                   bool /*is_default*/) {
82     return WriteStringToFile(contents, name);
83   }
84 };
85 
86 }  // namespace protobuf
87 }  // namespace google
88 
89 #endif  // GOOGLE_PROTOBUF_TESTING_FILE_H__
90