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 <string> 23 #include <vector> 24 25 #include "common/libs/utils/result.h" 26 27 namespace cuttlefish { 28 bool FileExists(const std::string& path, bool follow_symlinks = true); 29 bool FileHasContent(const std::string& path); 30 Result<std::vector<std::string>> DirectoryContents(const std::string& path); 31 bool DirectoryExists(const std::string& path, bool follow_symlinks = true); 32 Result<void> EnsureDirectoryExists(const std::string& directory_path, 33 const mode_t mode = S_IRWXU | S_IRWXG | 34 S_IROTH | S_IXOTH, 35 const std::string& group_name = ""); 36 Result<void> ChangeGroup(const std::string& path, 37 const std::string& group_name); 38 bool CanAccess(const std::string& path, const int mode); 39 bool IsDirectoryEmpty(const std::string& path); 40 bool RecursivelyRemoveDirectory(const std::string& path); 41 bool Copy(const std::string& from, const std::string& to); 42 off_t FileSize(const std::string& path); 43 bool RemoveFile(const std::string& file); 44 Result<std::string> RenameFile(const std::string& current_filepath, 45 const std::string& target_filepath); 46 std::string ReadFile(const std::string& file); 47 bool MakeFileExecutable(const std::string& path); 48 std::chrono::system_clock::time_point FileModificationTime(const std::string& path); 49 std::string cpp_dirname(const std::string& str); 50 std::string cpp_basename(const std::string& str); 51 // Whether a file exists and is a unix socket 52 bool FileIsSocket(const std::string& path); 53 // Get disk usage of a path. If this path is a directory, disk usage will 54 // account for all files under this folder(recursively). 55 int GetDiskUsage(const std::string& path); 56 57 // The returned value may contain .. or . if these are present in the path 58 // argument. 59 // path must not contain ~ 60 std::string AbsolutePath(const std::string& path); 61 62 std::string CurrentDirectory(); 63 64 struct FileSizes { 65 off_t sparse_size; 66 off_t disk_size; 67 }; 68 FileSizes SparseFileSizes(const std::string& path); 69 70 // Find file with name |target_name| under directory |path|, return path to 71 // found file(if any) 72 std::string FindFile(const std::string& path, const std::string& target_name); 73 74 Result<void> WalkDirectory( 75 const std::string& dir, 76 const std::function<bool(const std::string&)>& callback); 77 78 Result<void> WaitForFile(const std::string& path, int timeoutSec); 79 Result<void> WaitForUnixSocket(const std::string& path, int timeoutSec); 80 81 } // namespace cuttlefish 82