Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-31 10:12:04

0001 // Protocol Buffers - Google's data interchange format
0002 // Copyright 2008 Google Inc.  All rights reserved.
0003 //
0004 // Use of this source code is governed by a BSD-style
0005 // license that can be found in the LICENSE file or at
0006 // https://developers.google.com/open-source/licenses/bsd
0007 
0008 // Author: kenton@google.com (Kenton Varda)
0009 // emulates google3/file/base/file.h
0010 
0011 #ifndef GOOGLE_PROTOBUF_TESTING_FILE_H__
0012 #define GOOGLE_PROTOBUF_TESTING_FILE_H__
0013 
0014 #include "absl/status/status.h"
0015 #include "absl/strings/string_view.h"
0016 #include "google/protobuf/stubs/common.h"
0017 
0018 namespace google {
0019 namespace protobuf {
0020 
0021 const int DEFAULT_FILE_MODE = 0777;
0022 
0023 // Protocol buffer code only uses a couple static methods of File, and only
0024 // in the Rust plugin and in tests.
0025 class File {
0026  public:
0027   File(const File&) = delete;
0028   File& operator=(const File&) = delete;
0029 
0030   // Check if the file exists.
0031   static bool Exists(const std::string& name);
0032 
0033   // Read an entire file to a string.  Return true if successful, false
0034   // otherwise.
0035   static absl::Status ReadFileToString(const std::string& name,
0036                                        std::string* output,
0037                                        bool text_mode = false);
0038 
0039   // Same as above, but crash on failure.
0040   static void ReadFileToStringOrDie(const std::string& name,
0041                                     std::string* output);
0042 
0043   // Create a file and write a string to it.
0044   static absl::Status WriteStringToFile(absl::string_view contents,
0045                                         const std::string& name);
0046 
0047   // Same as above, but crash on failure.
0048   static void WriteStringToFileOrDie(absl::string_view contents,
0049                                      const std::string& name);
0050 
0051   // Create a directory.
0052   static absl::Status CreateDir(const std::string& name, int mode);
0053 
0054   // Create a directory and all parent directories if necessary.
0055   static absl::Status RecursivelyCreateDir(const std::string& path, int mode);
0056 
0057   // If "name" is a file, we delete it.  If it is a directory, we
0058   // call DeleteRecursively() for each file or directory (other than
0059   // dot and double-dot) within it, and then delete the directory itself.
0060   // The "dummy" parameters have a meaning in the original version of this
0061   // method but they are not used anywhere in protocol buffers.
0062   static void DeleteRecursively(const std::string& name, void* dummy1,
0063                                 void* dummy2);
0064 
0065   // Change working directory to given directory.
0066   static bool ChangeWorkingDirectory(const std::string& new_working_directory);
0067 
0068   static absl::Status GetContents(const std::string& name, std::string* output,
0069                                   bool /*is_default*/) {
0070     return ReadFileToString(name, output);
0071   }
0072 
0073   static absl::Status GetContentsAsText(const std::string& name,
0074                                         std::string* output,
0075                                         bool /*is_default*/) {
0076     return ReadFileToString(name, output, true);
0077   }
0078 
0079   static absl::Status SetContents(const std::string& name,
0080                                   absl::string_view contents,
0081                                   bool /*is_default*/) {
0082     return WriteStringToFile(contents, name);
0083   }
0084 };
0085 
0086 }  // namespace protobuf
0087 }  // namespace google
0088 
0089 #endif  // GOOGLE_PROTOBUF_TESTING_FILE_H__