3#if ROO_IO_FS_SUPPORT_POSIX
22std::unique_ptr<char[]>
cat(
const char*
mount_point,
const char* path) {
50bool PosixMountImpl::isReadOnly()
const {
return read_only_; }
52Stat PosixMountImpl::stat(
const char* path)
const {
53 if (path ==
nullptr || path[0] !=
'/') {
57 auto full_path = cat(mount_point_.get(), path);
60 if (::stat(full_path.get(), &st) == 0) {
62 Stat::Type type = S_ISDIR(st.st_mode) ? Stat::kDir : Stat::kFile;
63 uint64_t size = S_ISDIR(st.st_mode) ? 0 : st.st_size;
64 return Stat(type, size);
80Status PosixMountImpl::remove(
const char* path) {
81 if (path ==
nullptr || path[0] !=
'/') {
86 auto full_path = cat(mount_point_.get(), path);
88 if (::unlink(full_path.get()) == 0) {
105Status PosixMountImpl::rename(
const char* pathFrom,
const char* pathTo) {
106 if (pathFrom ==
nullptr || pathFrom[0] !=
'/') {
109 if (pathTo ==
nullptr || pathTo[0] !=
'/') {
115 auto full_src_path = cat(mount_point_.get(), pathFrom);
117 auto full_dst_path = cat(mount_point_.get(), pathTo);
120 if (::rename(full_src_path.get(), full_dst_path.get()) == 0)
return kOk;
125 return ResolveExistsError(full_dst_path.get());
134Status PosixMountImpl::mkdir(
const char* path) {
135 if (path ==
nullptr || path[0] !=
'/') {
140 auto full_path = cat(mount_point_.get(), path);
142 if (::mkdir(full_path.get(), 0777) == 0)
return kOk;
145 return ResolveExistsError(full_path.get());
160Status PosixMountImpl::rmdir(
const char* path) {
161 if (path ==
nullptr || path[0] !=
'/') {
166 auto full_path = cat(mount_point_.get(), path);
168 if (::rmdir(full_path.get()) == 0)
return kOk;
189std::unique_ptr<DirectoryImpl> PosixMountImpl::opendir(
190 std::shared_ptr<MountImpl> mount,
const char* path) {
191 if (path ==
nullptr || path[0] !=
'/') {
196 auto full_path = cat(mount_point_.get(), path);
197 if (full_path.get() ==
nullptr)
return DirectoryError(kOutOfMemory);
198 DIR* dir = ::opendir(full_path.get());
199 if (dir !=
nullptr) {
200 return std::unique_ptr<DirectoryImpl>(
201 new PosixDirectoryImpl(std::move(mount), path, dir, kOk));
217std::unique_ptr<MultipassInputStream> PosixMountImpl::fopen(
218 std::shared_ptr<MountImpl> mount,
const char* path) {
219 if (path ==
nullptr || path[0] !=
'/') {
222 if (mount_point_ ==
nullptr)
return InputError(kNotMounted);
223 auto full_path = cat(mount_point_.get(), path);
224 if (full_path.get() ==
nullptr)
return InputError(kOutOfMemory);
225 FILE* f = ::fopen(full_path.get(),
"r");
227 return std::unique_ptr<MultipassInputStream>(
228 new PosixFileInputStream(std::move(mount), f));
246const char* Policy2Mode(FileUpdatePolicy policy) {
258std::unique_ptr<OutputStream> PosixMountImpl::fopenForWrite(
259 std::shared_ptr<MountImpl> mount,
const char* path,
260 FileUpdatePolicy update_policy) {
261 if (path ==
nullptr || path[0] !=
'/') {
264 if (mount_point_ ==
nullptr)
return OutputError(kNotMounted);
268 auto full_path = cat(mount_point_.get(), path);
269 if (full_path.get() ==
nullptr)
return OutputError(kOutOfMemory);
270 FILE* f = ::fopen(full_path.get(), Policy2Mode(update_policy));
272 return std::unique_ptr<OutputStream>(
273 new PosixFileOutputStream(std::move(mount), f));
293void PosixMountImpl::deactivate() { mount_point_ =
nullptr; }
std::unique_ptr< DirectoryImpl > DirectoryError(Status error)
roo::basic_string_view< CharT, Traits > basic_string_view
std::unique_ptr< OutputStream > OutputError(Status error)
std::unique_ptr< MultipassInputStream > InputError(Status error)