1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17 
18 #include <sys/stat.h>
19 #include <sys/types.h>
20 
21 #include <chrono>
22 #include <optional>
23 #include <string>
24 #include <vector>
25 
26 #include "common/libs/utils/result.h"
27 
28 namespace cuttlefish {
29 bool FileExists(const std::string& path, bool follow_symlinks = true);
30 Result<dev_t> FileDeviceId(const std::string& path);
31 Result<bool> CanHardLink(const std::string& source,
32                          const std::string& destination);
33 Result<ino_t> FileInodeNumber(const std::string& path);
34 Result<bool> AreHardLinked(const std::string& source,
35                            const std::string& destination);
36 Result<std::string> CreateHardLink(const std::string& target,
37                                    const std::string& hardlink,
38                                    const bool overwrite_existing = false);
39 bool FileHasContent(const std::string& path);
40 Result<std::vector<std::string>> DirectoryContents(const std::string& path);
41 bool DirectoryExists(const std::string& path, bool follow_symlinks = true);
42 Result<void> EnsureDirectoryExists(const std::string& directory_path,
43                                    const mode_t mode = S_IRWXU | S_IRWXG |
44                                                        S_IROTH | S_IXOTH,
45                                    const std::string& group_name = "");
46 Result<void> ChangeGroup(const std::string& path,
47                          const std::string& group_name);
48 bool CanAccess(const std::string& path, const int mode);
49 bool IsDirectoryEmpty(const std::string& path);
50 Result<void> RecursivelyRemoveDirectory(const std::string& path);
51 bool Copy(const std::string& from, const std::string& to);
52 off_t FileSize(const std::string& path);
53 bool RemoveFile(const std::string& file);
54 Result<std::string> RenameFile(const std::string& current_filepath,
55                                const std::string& target_filepath);
56 std::string ReadFile(const std::string& file);
57 Result<std::string> ReadFileContents(const std::string& filepath);
58 bool MakeFileExecutable(const std::string& path);
59 std::chrono::system_clock::time_point FileModificationTime(const std::string& path);
60 std::string cpp_dirname(const std::string& str);
61 std::string cpp_basename(const std::string& str);
62 // Whether a file exists and is a unix socket
63 bool FileIsSocket(const std::string& path);
64 // Get disk usage of a path. If this path is a directory, disk usage will
65 // account for all files under this folder(recursively).
66 int GetDiskUsage(const std::string& path);
67 
68 // acloud related API
69 std::string FindImage(const std::string& search_path,
70                       const std::vector<std::string>& pattern);
71 
72 // The returned value may contain .. or . if these are present in the path
73 // argument.
74 // path must not contain ~
75 std::string AbsolutePath(const std::string& path);
76 
77 std::string CurrentDirectory();
78 
79 struct FileSizes {
80   off_t sparse_size;
81   off_t disk_size;
82 };
83 FileSizes SparseFileSizes(const std::string& path);
84 
85 // Find file with name |target_name| under directory |path|, return path to
86 // found file(if any)
87 std::string FindFile(const std::string& path, const std::string& target_name);
88 
89 Result<void> WalkDirectory(
90     const std::string& dir,
91     const std::function<bool(const std::string&)>& callback);
92 
93 #ifdef __linux__
94 Result<void> WaitForFile(const std::string& path, int timeoutSec);
95 Result<void> WaitForUnixSocket(const std::string& path, int timeoutSec);
96 Result<void> WaitForUnixSocketListeningWithoutConnect(const std::string& path,
97                                                       int timeoutSec);
98 #endif
99 
100 // parameter to EmulateAbsolutePath
101 struct InputPathForm {
102   /** If nullopt, uses the process' current working dir
103    *  But if there is no preceding .. or ., this field is not used.
104    */
105   std::optional<std::string> current_working_dir;
106   /** If nullopt, use SystemWideUserHome()
107    *  But, if there's no preceding ~, this field is not used.
108    */
109   std::optional<std::string> home_dir;
110   std::string path_to_convert;
111   bool follow_symlink;
112 };
113 
114 /**
115  * Returns emulated absolute path with a different process'/thread's
116  * context.
117  *
118  * This is useful when daemon(0, 0)-started server process wants to
119  * figure out a relative path that came from its client.
120  *
121  * The call mostly succeeds. It fails only if:
122  *  home_dir isn't given so supposed to relies on the local SystemWideUserHome()
123  *  but SystemWideUserHome() call fails.
124  */
125 Result<std::string> EmulateAbsolutePath(const InputPathForm& path_info);
126 
127 }  // namespace cuttlefish
128