xref: /aosp_15_r20/external/private-join-and-compute/private_join_and_compute/util/file.h (revision a6aa18fbfbf9cb5cd47356a9d1b057768998488c)
1 /*
2  * Copyright 2019 Google LLC.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     https://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef PRIVATE_JOIN_AND_COMPUTE_INTERNAL_UTIL_FILE_H_
17 #define PRIVATE_JOIN_AND_COMPUTE_INTERNAL_UTIL_FILE_H_
18 
19 #include <string>
20 
21 #include "private_join_and_compute/util/status.inc"
22 
23 namespace private_join_and_compute {
24 
25 // Renames a file. Overwrites the new file if it exists.
26 // Returns Status::OK for success.
27 // Error code in case of an error depends on the underlying implementation.
28 Status RenameFile(absl::string_view from, absl::string_view to);
29 
30 // Deletes a file.
31 // Returns Status::OK for success.
32 // Error code in case of an error depends on the underlying implementation.
33 Status DeleteFile(absl::string_view file_name);
34 
35 class File {
36  public:
37   virtual ~File() = default;
38 
39   // Opens the file_name for file operations applicable based on mode.
40   // Returns Status::OK for success.
41   // Error code in case of an error depends on the underlying implementation.
42   virtual Status Open(absl::string_view file_name, absl::string_view mode) = 0;
43 
44   // Closes the opened file. Must be called after opening a file.
45   // Returns Status::OK for success.
46   // Error code in case of an error depends on the underlying implementation.
47   virtual Status Close() = 0;
48 
49   // Returns true if there are more data in the file to be read.
50   // Returns a status instead in case of an io error in determining if there is
51   // more data.
52   virtual StatusOr<bool> HasMore() = 0;
53 
54   // Returns a data string of size length from reading file if successful.
55   // Returns a status in case of an error.
56   // This would also return an error status if the read data size is less than
57   // the length since it indicates file corruption.
58   virtual StatusOr<std::string> Read(size_t length) = 0;
59 
60   // Returns a line as string from the file without the trailing '\n' (or "\r\n"
61   // in the case of Windows).
62   //
63   // Returns a status in case of an error.
64   virtual StatusOr<std::string> ReadLine() = 0;
65 
66   // Writes the given content of size length into the file.
67   // Error code in case of an error depends on the underlying implementation.
68   virtual Status Write(absl::string_view content, size_t length) = 0;
69 
70   // Returns a File object depending on the linked implementation.
71   // Caller takes the ownership.
72   static File* GetFile();
73 
74  protected:
75   File() = default;
76 };
77 
78 namespace internal {
79 std::string JoinPathImpl(std::initializer_list<std::string> paths);
80 }  // namespace internal
81 
82 // Joins multiple paths together such that only the first argument directory
83 // structure is represented. A dot as a separator is added for other arguments.
84 //
85 //  Arguments                  | JoinPath            |
86 //  ---------------------------+---------------------+
87 //  '/foo', 'bar'              | /foo/bar            |
88 //  '/foo/', 'bar'             | /foo/bar            |
89 //  '/foo', '/bar'             | /foo/bar            |
90 //  '/foo', '/bar', '/baz'     | /foo/bar.baz        |
91 //
92 // All paths will be treated as relative paths, regardless of whether or not
93 // they start with a leading '/'. That is, all paths will be concatenated
94 // together, with the appropriate path separator inserted in between.
95 // After the first path, all paths will be joined with a dot instead of the path
96 // separator so that there is no level of directory after the first argument.
97 // Arguments must be convertible to string.
98 //
99 // Usage:
100 // string path = file::JoinPath("/tmp", dirname, filename);
101 template <typename... T>
JoinPath(const T &...args)102 std::string JoinPath(const T&... args) {
103   return internal::JoinPathImpl({args...});
104 }
105 
106 }  // namespace private_join_and_compute
107 
108 #endif  // PRIVATE_JOIN_AND_COMPUTE_INTERNAL_UTIL_FILE_H_
109