Android-cuttlefish cvd tool
files.h
Go to the documentation of this file.
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 <cstddef>
23#include <optional>
24#include <string>
25#include <vector>
26
28
29namespace cuttlefish {
30bool FileExists(const std::string& path, bool follow_symlinks = true);
31Result<dev_t> FileDeviceId(const std::string& path);
32Result<bool> CanHardLink(const std::string& source,
33 const std::string& destination);
34inline Result<bool> CanRename(const std::string& source,
35 const std::string& destination) {
36 return CanHardLink(source, destination);
37}
38Result<ino_t> FileInodeNumber(const std::string& path);
39Result<bool> AreHardLinked(const std::string& source,
40 const std::string& destination);
41Result<std::string> CreateHardLink(const std::string& target,
42 const std::string& hardlink,
43 bool overwrite_existing = false);
44Result<void> CreateSymLink(const std::string& target, const std::string& link,
45 bool overwrite_existing = false);
47 const std::string& source, const std::string& destination);
48// Merges the contents of the source directory into the destination directory.
49// The source directory is empty after this operation.
50Result<void> MoveDirectoryContents(const std::string& source,
51 const std::string& destination);
52bool FileHasContent(const std::string& path);
53Result<std::vector<std::string>> DirectoryContents(const std::string& path);
54Result<std::vector<std::string>> DirectoryContentsPaths(
55 const std::string& path);
56bool DirectoryExists(const std::string& path, bool follow_symlinks = true);
57inline bool IsDirectory(const std::string& path) {
58 return DirectoryExists(path);
59};
60Result<void> EnsureDirectoryExists(const std::string& directory_path,
61 mode_t mode = S_IRWXU | S_IRWXG | S_IROTH |
62 S_IXOTH,
63 const std::string& group_name = "");
64Result<void> ChangeGroup(const std::string& path,
65 const std::string& group_name);
66bool CanAccess(const std::string& path, int mode);
67bool IsDirectoryEmpty(const std::string& path);
68Result<void> RecursivelyRemoveDirectory(const std::string& path);
69bool Copy(const std::string& from, const std::string& to);
70off_t FileSize(const std::string& path);
71bool RemoveFile(const std::string& file);
72Result<std::string> RenameFile(const std::string& current_filepath,
73 const std::string& target_filepath);
74std::string ReadFile(const std::string& file);
75Result<std::string> ReadFileContents(const std::string& filepath);
76bool MakeFileExecutable(const std::string& path);
77Result<std::chrono::system_clock::time_point> FileModificationTime(
78 const std::string& path);
79// Whether a file exists and is a unix socket
80bool FileIsSocket(const std::string& path);
81
82// acloud related API
83std::string FindImage(const std::string& search_path,
84 const std::vector<std::string>& pattern);
85
86// The returned value may contain .. or . if these are present in the path
87// argument.
88// path must not contain ~
89std::string AbsolutePath(const std::string& path);
90
91std::string CurrentDirectory();
92
93struct FileSizes {
95 off_t disk_size;
96};
97FileSizes SparseFileSizes(const std::string& path);
98
99// Find file with name |target_name| under directory |path|, return path to
100// found file(if any)
101Result<std::string> FindFile(const std::string& path,
102 const std::string& target_name);
103
105 const std::string& dir,
106 const std::function<bool(const std::string&)>& callback);
107
108// parameter to EmulateAbsolutePath
113 std::optional<std::string> current_working_dir;
117 std::optional<std::string> home_dir;
118 std::string path_to_convert;
120};
121
134
135} // namespace cuttlefish
Definition: expected.h:86
static struct et_list link
Definition: ext2_err.c:210
Definition: alloc_utils.cpp:23
Result< dev_t > FileDeviceId(const std::string &path)
Definition: files.cpp:81
Result< std::string > RenameFile(const std::string &current_filepath, const std::string &target_filepath)
Definition: files.cpp:497
std::string AbsolutePath(const std::string &path)
Definition: files.cpp:446
bool IsDirectoryEmpty(const std::string &path)
Definition: files.cpp:303
Result< ino_t > FileInodeNumber(const std::string &path)
Definition: files.cpp:97
bool RemoveFile(const std::string &file)
Definition: files.cpp:507
off_t FileSize(const std::string &path)
Definition: files.cpp:467
bool DirectoryExists(const std::string &path, bool follow_symlinks)
Definition: files.cpp:235
Result< void > ChangeGroup(const std::string &path, const std::string &group_name)
Definition: files.cpp:283
Result< void > MoveDirectoryContents(const std::string &source, const std::string &destination)
Definition: files.cpp:188
bool IsDirectory(const std::string &path)
Definition: files.h:57
Result< void > HardLinkDirecoryContentsRecursively(const std::string &source, const std::string &destination)
Definition: files.cpp:164
Result< void > EnsureDirectoryExists(const std::string &directory_path, const mode_t mode, const std::string &group_name)
Definition: files.cpp:246
bool FileHasContent(const std::string &path)
Definition: files.cpp:160
Result< void > WalkDirectory(const std::string &dir, const std::function< bool(const std::string &)> &callback)
Definition: files.cpp:651
Result< std::string > FindFile(const std::string &path, const std::string &target_name)
Definition: files.cpp:633
std::string FindImage(const std::string &search_path, const std::vector< std::string > &pattern)
Definition: files.cpp:621
Result< std::vector< std::string > > DirectoryContentsPaths(const std::string &path)
Definition: files.cpp:226
bool CanAccess(const std::string &path, const int mode)
Definition: files.cpp:299
Result< void > CreateSymLink(const std::string &target, const std::string &link, const bool overwrite_existing)
Definition: files.cpp:141
Result< std::vector< std::string > > DirectoryContents(const std::string &path)
Definition: files.cpp:212
Result< std::chrono::system_clock::time_point > FileModificationTime(const std::string &path)
Definition: files.cpp:480
Result< bool > CanRename(const std::string &source, const std::string &destination)
Definition: files.h:34
bool FileExists(const std::string &path, bool follow_symlinks)
Definition: files.cpp:76
bool FileIsSocket(const std::string &path)
Definition: files.cpp:610
Result< std::string > ReadFileContents(const std::string &filepath)
Definition: files.cpp:536
std::string ReadFile(const std::string &file)
Definition: files.cpp:517
Result< bool > CanHardLink(const std::string &source, const std::string &destination)
Definition: files.cpp:91
std::string CurrentDirectory()
Definition: files.cpp:549
Result< void > RecursivelyRemoveDirectory(const std::string &path)
Definition: files.cpp:323
Result< bool > AreHardLinked(const std::string &source, const std::string &destination)
Definition: files.cpp:107
bool MakeFileExecutable(const std::string &path)
Definition: files.cpp:475
bool Copy(const std::string &from, const std::string &to)
Definition: files.cpp:388
Result< std::string > CreateHardLink(const std::string &target, const std::string &hardlink, const bool overwrite_existing)
Definition: files.cpp:115
Result< std::string > EmulateAbsolutePath(const InputPathForm &path_info)
Definition: files.cpp:704
FileSizes SparseFileSizes(const std::string &path)
Definition: files.cpp:563
Definition: files.h:93
off_t sparse_size
Definition: files.h:94
off_t disk_size
Definition: files.h:95
Definition: files.h:109
bool follow_symlink
Definition: files.h:119
std::string path_to_convert
Definition: files.h:118
std::optional< std::string > current_working_dir
Definition: files.h:113
std::optional< std::string > home_dir
Definition: files.h:117