mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-16 15:36:12 -04:00
Fixed remaining filesystem calls that throws exceptions.
PiperOrigin-RevId: 620379472
This commit is contained in:
committed by
Copybara-Service
parent
c8168ba1de
commit
1e08b2d886
@@ -52,6 +52,13 @@ cc_library(
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "files",
|
||||
srcs = ["files.cc"],
|
||||
hdrs = ["files.h"],
|
||||
visibility = ["//visibility:public"],
|
||||
)
|
||||
|
||||
cc_test(
|
||||
name = "base_test",
|
||||
size = "small",
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
// 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 "internal/base/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);
|
||||
}
|
||||
|
||||
std::optional<std::filesystem::path> GetTemporaryDirectory() {
|
||||
std::error_code error_code;
|
||||
std::filesystem::path temp_dir =
|
||||
std::filesystem::temp_directory_path(error_code);
|
||||
if (temp_dir.empty()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return temp_dir;
|
||||
}
|
||||
|
||||
std::filesystem::path CurrentDirectory() {
|
||||
// temp_directory_path() returns empty path on error.
|
||||
std::error_code error_code;
|
||||
return std::filesystem::current_path(error_code);
|
||||
}
|
||||
|
||||
bool Rename(std::filesystem::path old_path, std::filesystem::path new_path) {
|
||||
std::error_code error_code;
|
||||
std::filesystem::rename(old_path, new_path, error_code);
|
||||
if (error_code) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CreateDirectories(std::filesystem::path path) {
|
||||
std::error_code error_code;
|
||||
std::filesystem::create_directories(path, error_code);
|
||||
if (error_code) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace nearby::sharing
|
||||
@@ -0,0 +1,55 @@
|
||||
// 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_INTERNAL_BASE_FILES_H_
|
||||
#define THIRD_PARTY_NEARBY_INTERNAL_BASE_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);
|
||||
|
||||
// Returns path to a temporary directory if available.
|
||||
std::optional<std::filesystem::path> GetTemporaryDirectory();
|
||||
|
||||
// Returns path to the current directory. On failure returns an empty path.
|
||||
std::filesystem::path CurrentDirectory();
|
||||
|
||||
// Renames the file at old_path to new_path.
|
||||
// Returns true on success.
|
||||
bool Rename(std::filesystem::path old_path, std::filesystem::path new_path);
|
||||
|
||||
// Creates all directory leading to path.
|
||||
// Returns true on success.
|
||||
bool CreateDirectories(std::filesystem::path path);
|
||||
|
||||
} // namespace nearby::sharing
|
||||
|
||||
#endif // THIRD_PARTY_NEARBY_INTERNAL_BASE_FILES_H_
|
||||
Reference in New Issue
Block a user