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 <functional>
23#include <string>
24#include <string_view>
25#include <vector>
26
29
30namespace cuttlefish {
31Result<bool> CanHardLink(const std::string& source,
32 const std::string& destination);
33inline Result<bool> CanRename(const std::string& source,
34 const std::string& destination) {
35 return CanHardLink(source, destination);
36}
37
39 const std::string& source, const std::string& destination);
40// Merges the contents of the source directory into the destination directory.
41// The source directory is empty after this operation.
42Result<void> MoveDirectoryContents(const std::string& source,
43 const std::string& destination);
44bool FileHasContent(const std::string& path);
45Result<std::vector<std::string>> DirectoryContentsPaths(
46 const std::string& path);
47Result<void> EnsureDirectoryExists(const std::string& directory_path,
48 mode_t mode = S_IRWXU | S_IRWXG | S_IROTH |
49 S_IXOTH,
50 const std::string& group_name = "");
51Result<void> ChangeGroup(const std::string& path,
52 const std::string& group_name);
53bool CanAccess(const std::string& path, int mode);
54off_t FileSize(const std::string& path);
55Result<std::string> RenameFile(const std::string& current_filepath,
56 const std::string& target_filepath);
57std::string ReadFile(const std::string& file);
58Result<std::string> ReadFileContents(const std::string& path);
59Result<void> WriteNewFile(const std::string& filepath, std::string_view content,
60 mode_t mode = S_IRWXU | S_IRGRP | S_IROTH);
61bool MakeFileExecutable(const std::string& path);
62Result<std::chrono::system_clock::time_point> FileModificationTime(
63 const std::string& path);
64Result<uid_t> FileOwner(const std::string& path);
65
66// The returned value may contain .. or . if these are present in the path
67// argument.
68std::string AbsolutePath(std::string_view path);
69
70std::string CurrentDirectory();
71
72struct FileSizes {
74 off_t disk_size;
75};
76FileSizes SparseFileSizes(const std::string& path);
77
78// Find file with name |target_name| under directory |path|, return path to
79// found file(if any)
80Result<std::string> FindFile(const std::string& path,
81 const std::string& target_name);
82
83using WalkDirectoryCallback = std::function<Result<void>(const std::string&)>;
84
85Result<void> WalkDirectory(const std::string& dir,
86 const WalkDirectoryCallback& callback);
87
88std::vector<std::string> Path(const std::string& env_name = "PATH");
89
90Result<std::string> Search(const std::vector<std::string>& path,
91 std::string_view name);
92
93Result<SharedFD> CreateOrReuseAndDrainFifo(const std::string& path,
94 mode_t mode);
95
96} // namespace cuttlefish
char * name
Definition: libf2fs.c:1403
Definition: alloc_driver.h:20
Result< std::string > RenameFile(const std::string &current_filepath, const std::string &target_filepath)
Definition: files.cpp:240
std::string AbsolutePath(std::string_view path)
Definition: files.cpp:196
Result< uid_t > FileOwner(const std::string &path)
Definition: files.cpp:218
off_t FileSize(const std::string &path)
Definition: files.cpp:213
Result< void > WriteNewFile(const std::string &filepath, std::string_view content, mode_t mode)
Definition: files.cpp:274
Result< void > ChangeGroup(const std::string &path, const std::string &group_name)
Definition: files.cpp:176
Result< void > MoveDirectoryContents(const std::string &source, const std::string &destination)
Definition: files.cpp:108
Result< void > EnsureDirectoryExists(const std::string &directory_path, const mode_t mode, const std::string &group_name)
Definition: files.cpp:139
bool FileHasContent(const std::string &path)
Definition: files.cpp:81
Result< std::string > FindFile(const std::string &path, const std::string &target_name)
Definition: files.cpp:348
Result< std::string > ReadFileContents(const std::string &path)
Definition: files.cpp:269
Result< std::vector< std::string > > DirectoryContentsPaths(const std::string &path)
Definition: files.cpp:130
bool CanAccess(const std::string &path, const int mode)
Definition: files.cpp:192
tl::expected< T, StackTraceError > Result
Definition: result_type.h:30
std::vector< std::string > Path(const std::string &env_name)
Definition: files.cpp:379
Result< std::chrono::system_clock::time_point > FileModificationTime(const std::string &path)
Definition: files.cpp:227
Result< void > WalkDirectory(const std::string &dir, const WalkDirectoryCallback &callback)
Definition: files.cpp:366
Result< bool > CanRename(const std::string &source, const std::string &destination)
Definition: files.h:33
std::function< Result< void >(const std::string &)> WalkDirectoryCallback
Definition: files.h:83
Result< void > LinkOrCopyDirectoryContentsRecursively(const std::string &source, const std::string &destination)
Definition: files.cpp:83
std::string ReadFile(const std::string &file)
Definition: files.cpp:248
Result< bool > CanHardLink(const std::string &source, const std::string &destination)
Definition: files.cpp:75
std::string CurrentDirectory()
Definition: files.cpp:285
Result< std::string > Search(const std::vector< std::string > &path, std::string_view name)
Definition: files.cpp:385
Result< SharedFD > CreateOrReuseAndDrainFifo(const std::string &path, mode_t mode)
Definition: files.cpp:398
bool MakeFileExecutable(const std::string &path)
Definition: files.cpp:222
FileSizes SparseFileSizes(const std::string &path)
Definition: files.cpp:299
Definition: files.h:72
off_t sparse_size
Definition: files.h:73
off_t disk_size
Definition: files.h:74