Wrap std::filesystem calls.

PiperOrigin-RevId: 620315145
This commit is contained in:
Francis Tsui
2024-03-29 12:42:04 -07:00
committed by Copybara-Service
parent ebe6d0dade
commit f1d3c2d740
8 changed files with 144 additions and 53 deletions
+7
View File
@@ -61,3 +61,10 @@ cc_library(
hdrs = ["compatible_u8_string.h"],
visibility = ["//sharing:__subpackages__"],
)
cc_library(
name = "files",
srcs = ["files.cc"],
hdrs = ["files.h"],
visibility = ["//visibility:public"],
)
+63
View File
@@ -0,0 +1,63 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "sharing/common/files.h"
#include <cstdint>
#include <filesystem> // NOLINT(build/c++17)
#include <optional>
#include <system_error> // NOLINT(build/c++11)
namespace nearby::sharing {
bool FileExists(const std::filesystem::path& path) {
std::error_code error_code;
if (std::filesystem::exists(path, error_code) &&
!std::filesystem::is_directory(path, error_code)) {
// is_directory returns false on error.
return (!error_code);
}
return false;
}
std::optional<uintmax_t> GetFileSize(const std::filesystem::path& path) {
if (!FileExists(path)) {
return std::nullopt;
}
std::error_code error_code;
uintmax_t size = std::filesystem::file_size(path, error_code);
if (size == static_cast<uintmax_t>(-1)) {
return std::nullopt;
}
return size;
}
bool DirectoryExists(const std::filesystem::path& path) {
std::error_code error_code;
if (std::filesystem::exists(path, error_code) &&
std::filesystem::is_directory(path, error_code)) {
return true;
}
return false;
}
bool RemoveFile(const std::filesystem::path& path) {
if (!FileExists(path)) {
return false;
}
std::error_code error_code;
return std::filesystem::remove(path, error_code);
}
} // namespace nearby::sharing
+41
View File
@@ -0,0 +1,41 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef THIRD_PARTY_NEARBY_SHARING_COMMON_FILES_H_
#define THIRD_PARTY_NEARBY_SHARING_COMMON_FILES_H_
#include <cstdint>
#include <filesystem> // NOLINT(build/c++17)
#include <optional>
// This file contains exception safe wrappers to access common std::filesystem
// functions.
namespace nearby::sharing {
// Returns true if path exists and is not a directory.
bool FileExists(const std::filesystem::path& path);
// Returns the size of the file at path, or nullopt if not found or not a file.
std::optional<uintmax_t> GetFileSize(const std::filesystem::path& path);
// Returns true if path exists and is a directory.
bool DirectoryExists(const std::filesystem::path& path);
// Removes the file at path and returns true.
// Returns false if path does not exist, is not a file or cannot be removed.
bool RemoveFile(const std::filesystem::path& path);
} // namespace nearby::sharing
#endif // THIRD_PARTY_NEARBY_SHARING_COMMON_FILES_H_