Skip to content

Commit 8500f00

Browse files
committed
Create a NativeFilesystem implementation of ReadWriteFilesystem
Bug: b/485385998
1 parent d4cbe87 commit 8500f00

4 files changed

Lines changed: 119 additions & 1 deletion

File tree

base/cvd/cuttlefish/io/BUILD.bazel

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,21 @@ cf_cc_library(
5656
],
5757
)
5858

59+
cf_cc_library(
60+
name = "native_filesystem",
61+
srcs = ["native_filesystem.cc"],
62+
hdrs = ["native_filesystem.h"],
63+
deps = [
64+
"//cuttlefish/common/libs/fs",
65+
"//cuttlefish/io",
66+
"//cuttlefish/io:filesystem",
67+
"//cuttlefish/io:shared_fd",
68+
"//cuttlefish/posix:strerror",
69+
"//cuttlefish/result:expect",
70+
"//cuttlefish/result:result_type",
71+
],
72+
)
73+
5974
cf_cc_library(
6075
name = "shared_fd",
6176
srcs = ["shared_fd.cc"],
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//
2+
// Copyright (C) 2026 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+
#include "cuttlefish/io/native_filesystem.h"
17+
18+
#include <errno.h>
19+
#include <stdint.h>
20+
#include <unistd.h>
21+
22+
#include <string_view>
23+
24+
#include "cuttlefish/common/libs/fs/shared_fd.h"
25+
#include "cuttlefish/io/shared_fd.h"
26+
#include "cuttlefish/posix/strerror.h"
27+
#include "cuttlefish/result/expect.h"
28+
#include "cuttlefish/result/result_type.h"
29+
30+
namespace cuttlefish {
31+
32+
Result<std::unique_ptr<ReaderWriterSeeker>> NativeFilesystem::CreateFile(
33+
std::string_view path) {
34+
SharedFD fd =
35+
SharedFD::Open(std::string(path), O_CLOEXEC | O_CREAT | O_EXCL | O_RDWR);
36+
CF_EXPECTF(fd->IsOpen(),
37+
"Failed to open '{}' with O_CREAT | O_EXCL | O_RDWR: '{}'", path,
38+
fd->StrError());
39+
return std::make_unique<SharedFdIo>(fd);
40+
}
41+
42+
Result<void> NativeFilesystem::DeleteFile(std::string_view path) {
43+
CF_EXPECT_GE(unlink(std::string(path).c_str()), 0, StrError(errno));
44+
return {};
45+
}
46+
47+
Result<std::unique_ptr<ReaderSeeker>> NativeFilesystem::OpenReadOnly(
48+
std::string_view path) {
49+
SharedFD fd = SharedFD::Open(std::string(path), O_CLOEXEC | O_RDONLY);
50+
CF_EXPECTF(fd->IsOpen(), "Failed to open '{}' with O_RDONLY: '{}'", path,
51+
fd->StrError());
52+
return std::make_unique<SharedFdIo>(fd);
53+
}
54+
55+
Result<std::unique_ptr<ReaderWriterSeeker>> NativeFilesystem::OpenReadWrite(
56+
std::string_view path) {
57+
SharedFD fd = SharedFD::Open(std::string(path), O_CLOEXEC | O_RDWR);
58+
CF_EXPECTF(fd->IsOpen(), "Failed to open '{}' with O_RDWR: '{}'", path,
59+
fd->StrError());
60+
return std::make_unique<SharedFdIo>(fd);
61+
}
62+
63+
} // namespace cuttlefish
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//
2+
// Copyright (C) 2026 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 <stdint.h>
19+
20+
#include "cuttlefish/io/filesystem.h"
21+
#include "cuttlefish/io/io.h"
22+
#include "cuttlefish/result/result_type.h"
23+
24+
namespace cuttlefish {
25+
26+
class NativeFilesystem : public ReadWriteFilesystem {
27+
public:
28+
Result<std::unique_ptr<ReaderSeeker>> OpenReadOnly(
29+
std::string_view path) override;
30+
31+
Result<std::unique_ptr<ReaderWriterSeeker>> CreateFile(
32+
std::string_view path) override;
33+
34+
Result<void> DeleteFile(std::string_view path) override;
35+
36+
Result<std::unique_ptr<ReaderWriterSeeker>> OpenReadWrite(
37+
std::string_view path) override;
38+
};
39+
40+
} // namespace cuttlefish

base/cvd/cuttlefish/io/shared_fd.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
namespace cuttlefish {
2525

26-
class SharedFdIo : public ReaderSeeker, public WriterSeeker {
26+
class SharedFdIo : public ReaderWriterSeeker {
2727
public:
2828
explicit SharedFdIo(SharedFD);
2929

0 commit comments

Comments
 (0)