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( 60 const std::string& path); 61 // Whether a file exists and is a unix socket 62 bool FileIsSocket(const std::string& path); 63 // Get disk usage of a path. If this path is a directory, disk usage will 64 // account for all files under this folder(recursively). 65 int GetDiskUsage(const std::string& path); 66 67 // acloud related API 68 std::string FindImage(const std::string& search_path, 69 const std::vector<std::string>& pattern); 70 71 // The returned value may contain .. or . if these are present in the path 72 // argument. 73 // path must not contain ~ 74 std::string AbsolutePath(const std::string& path); 75 76 std::string CurrentDirectory(); 77 78 struct FileSizes { 79 off_t sparse_size; 80 off_t disk_size; 81 }; 82 FileSizes SparseFileSizes(const std::string& path); 83 84 // Find file with name |target_name| under directory |path|, return path to 85 // found file(if any) 86 std::string FindFile(const std::string& path, const std::string& target_name); 87 88 Result<void> WalkDirectory( 89 const std::string& dir, 90 const std::function<bool(const std::string&)>& callback); 91 92 #ifdef __linux__ 93 Result<void> WaitForFile(const std::string& path, int timeoutSec); 94 Result<void> WaitForUnixSocket(const std::string& path, int timeoutSec); 95 Result<void> WaitForUnixSocketListeningWithoutConnect(const std::string& path, 96 int timeoutSec); 97 #endif 98 99 // parameter to EmulateAbsolutePath 100 struct InputPathForm { 101 /** If nullopt, uses the process' current working dir 102 * But if there is no preceding .. or ., this field is not used. 103 */ 104 std::optional<std::string> current_working_dir; 105 /** If nullopt, use SystemWideUserHome() 106 * But, if there's no preceding ~, this field is not used. 107 */ 108 std::optional<std::string> home_dir; 109 std::string path_to_convert; 110 bool follow_symlink; 111 }; 112 113 /** 114 * Returns emulated absolute path with a different process'/thread's 115 * context. 116 * 117 * This is useful when daemon(0, 0)-started server process wants to 118 * figure out a relative path that came from its client. 119 * 120 * The call mostly succeeds. It fails only if: 121 * home_dir isn't given so supposed to relies on the local SystemWideUserHome() 122 * but SystemWideUserHome() call fails. 123 */ 124 Result<std::string> EmulateAbsolutePath(const InputPathForm& path_info); 125 126 } // namespace cuttlefish 127