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 <cstdint>
23#include <functional>
24#include <string>
25#include <vector>
26
27#include "absl/strings/string_view.h"
28
30
31namespace cuttlefish {
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}
38
40 const std::string& source, const std::string& destination);
41// Merges the contents of the source directory into the destination directory.
42// The source directory is empty after this operation.
43Result<void> MoveDirectoryContents(const std::string& source,
44 const std::string& destination);
45bool FileHasContent(const std::string& path);
46Result<std::vector<std::string>> DirectoryContentsPaths(
47 const std::string& path);
48Result<void> EnsureDirectoryExists(const std::string& directory_path,
49 mode_t mode = S_IRWXU | S_IRWXG | S_IROTH |
50 S_IXOTH,
51 const std::string& group_name = "");
52Result<void> ChangeGroup(const std::string& path,
53 const std::string& group_name);
54off_t FileSize(const std::string& path);
55Result<void> 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<std::vector<uint8_t>> ReadBinaryFileContents(const std::string& path);
60bool MakeFileExecutable(const std::string& path);
61Result<std::chrono::system_clock::time_point> FileModificationTime(
62 const std::string& path);
63Result<uid_t> FileOwner(const std::string& path);
64
65// The returned value may contain .. or . if these are present in the path
66// argument.
67std::string AbsolutePath(absl::string_view path);
68
69std::string CurrentDirectory();
70
71// Find file with name |target_name| under directory |path|, return path to
72// found file(if any)
73Result<std::string> FindFile(const std::string& path,
74 const std::string& target_name);
75
76using WalkDirectoryCallback = std::function<Result<void>(const std::string&)>;
77
78// Invokes `callback` on every entry beneath `dir`, recursively. Note that the
79// callback receives directories as well as files.
80Result<void> WalkDirectory(const std::string& dir,
81 const WalkDirectoryCallback& callback);
82
83// Like `WalkDirectory`, but only invokes `callback` on entries that are not
84// directories. Symlinked directories are not descended into, so a symlink loop
85// cannot trap the walk.
86Result<void> WalkDirectoryFiles(const std::string& dir,
87 const WalkDirectoryCallback& callback);
88
89std::vector<std::string> Path(const std::string& env_name = "PATH");
90
91Result<std::string> Search(const std::vector<std::string>& path,
92 absl::string_view name);
93
94} // namespace cuttlefish
Definition: alloc_driver.h:21
Result< uid_t > FileOwner(const std::string &path)
Definition: files.cpp:219
Result< void > WalkDirectoryFiles(const std::string &dir, const WalkDirectoryCallback &callback)
Definition: files.cpp:337
Result< void > RenameFile(const std::string &current_filepath, const std::string &target_filepath)
Definition: files.cpp:239
off_t FileSize(const std::string &path)
Definition: files.cpp:214
Result< void > ChangeGroup(const std::string &path, const std::string &group_name)
Definition: files.cpp:181
std::string AbsolutePath(absl::string_view path)
Definition: files.cpp:197
Result< std::string > Search(const std::vector< std::string > &path, absl::string_view name)
Definition: files.cpp:360
Result< void > MoveDirectoryContents(const std::string &source, const std::string &destination)
Definition: files.cpp:102
Result< std::vector< uint8_t > > ReadBinaryFileContents(const std::string &path)
Definition: files.cpp:274
Result< void > EnsureDirectoryExists(const std::string &directory_path, const mode_t mode, const std::string &group_name)
Definition: files.cpp:133
bool FileHasContent(const std::string &path)
Definition: files.cpp:75
Result< std::string > FindFile(const std::string &path, const std::string &target_name)
Definition: files.cpp:306
Result< std::string > ReadFileContents(const std::string &path)
Definition: files.cpp:268
Result< std::vector< std::string > > DirectoryContentsPaths(const std::string &path)
Definition: files.cpp:124
std::vector< std::string > Path(const std::string &env_name)
Definition: files.cpp:354
Result< std::chrono::system_clock::time_point > FileModificationTime(const std::string &path)
Definition: files.cpp:228
Result< void > WalkDirectory(const std::string &dir, const WalkDirectoryCallback &callback)
Definition: files.cpp:324
Result< bool > CanRename(const std::string &source, const std::string &destination)
Definition: files.h:34
std::function< Result< void >(const std::string &)> WalkDirectoryCallback
Definition: files.h:76
Result< void > LinkOrCopyDirectoryContentsRecursively(const std::string &source, const std::string &destination)
Definition: files.cpp:77
std::string ReadFile(const std::string &file)
Definition: files.cpp:247
Result< bool > CanHardLink(const std::string &source, const std::string &destination)
Definition: files.cpp:69
tl::expected< T, StackTraceError< E > > Result
Definition: result_type.h:30
std::string CurrentDirectory()
Definition: files.cpp:292
bool MakeFileExecutable(const std::string &path)
Definition: files.cpp:223