Symbian platform (C++)
SDK native APIs, runtime and tooling
Loading...
Searching...
No Matches
storage.h
Go to the documentation of this file.
1// Copyright 2026 The Symbian SDK Authors.
2// Licensed under the Apache License, Version 2.0.
3
4#ifndef SYMBIAN_API_STORAGE_STORAGE_H_
5#define SYMBIAN_API_STORAGE_STORAGE_H_
6
7#include <atomic>
8#include <cstddef>
9#include <cstdint>
10#include <optional>
11#include <span>
12#include <string>
13#include <string_view>
14
15#include "absl/status/statusor.h"
16
18
19struct NativeFile;
20struct NativeDirectory;
21
22// One open file and its owning File Server session. Move-only and read-only.
23// Keep use and destruction on the opening thread; native session ownership
24// across threads is not yet established.
26 public:
27 // Opens an absolute Symbian UTF-16 path (for example u"C:\\Data\\file.txt").
28 static absl::StatusOr<ReadOnlyFile> Open(std::u16string_view path);
29
30 ReadOnlyFile(ReadOnlyFile&& other) noexcept;
31 ReadOnlyFile& operator=(ReadOnlyFile&& other) noexcept;
32 ReadOnlyFile(const ReadOnlyFile&) = delete;
35
36 // Returns the file's size. This initial File Server profile supports 2 GiB.
37 absl::StatusOr<std::uint64_t> Size() const;
38 // Reads directly into caller memory, with no per-read allocation. An empty
39 // destination returns zero. Offsets above 2 GiB are unsupported here.
40 absl::StatusOr<std::size_t> ReadAt(std::uint64_t offset,
41 std::span<std::byte> destination) const;
42
43 private:
44 explicit ReadOnlyFile(NativeFile* native) : native_(native) {}
45
46 NativeFile* native_ = nullptr;
47};
48
49// Creation and replacement are explicit so opening a file cannot silently
50// truncate existing data.
51enum class WriteMode {
55};
56
57// One writable file and its File Server session. Move-only; use and destroy
58// on the opening thread. Calls can block, so run them on a worker if needed.
60 public:
61 // Opens an absolute UTF-16 path. Parent directories must already exist.
62 static absl::StatusOr<WritableFile> Open(std::u16string_view path,
63 WriteMode mode);
64
65 WritableFile(WritableFile&& other) noexcept;
66 WritableFile& operator=(WritableFile&& other) noexcept;
67 WritableFile(const WritableFile&) = delete;
70
71 // Writes all bytes directly from caller memory at an explicit offset.
72 // The caller keeps the buffer valid until the call returns.
73 absl::Status WriteAt(std::uint64_t offset, std::span<const std::byte> source);
74 // Requests that the File Server flush this file's pending writes.
75 absl::Status Flush();
76
77 private:
78 explicit WritableFile(NativeFile* native) : native_(native) {}
79
80 NativeFile* native_ = nullptr;
81};
82
83// Progress after one bounded-memory transfer step.
85 // Number of bytes already written to the destination.
86 std::uint64_t bytes_copied = 0;
87 // Source size recorded when copying began.
88 std::uint64_t total_bytes = 0;
89 // True after the final write and File Server flush have succeeded.
90 bool complete = false;
91};
92
93// Pull-driven file copy. Each Step performs at most one 32 KiB read and write;
94// callers can publish each result, then schedule the next Step on the same
95// worker. Cancel() is safe from another thread while the object stays alive.
96// Native synchronous I/O already in flight may finish before Step returns.
97class FileCopy {
98 public:
99 // Destination mode makes replacement explicit. Both paths must be absolute.
100 static absl::StatusOr<FileCopy> Open(std::u16string_view source,
101 std::u16string_view destination,
102 WriteMode destination_mode);
103
104 FileCopy(FileCopy&& other) noexcept;
105 FileCopy& operator=(FileCopy&& other) noexcept;
106 FileCopy(const FileCopy&) = delete;
107 FileCopy& operator=(const FileCopy&) = delete;
108 ~FileCopy();
109
110 // Requests cancellation without waiting for File Server I/O. The next
111 // cancellation checkpoint returns kCancelled. Do not destroy or move the
112 // operation concurrently with Cancel or Step.
113 void Cancel() noexcept;
114 // Runs one transfer step. A completed call is idempotent. No concurrent Step
115 // calls; all Steps and destruction stay on the opening thread.
116 absl::StatusOr<CopyProgress> Step();
117 // May be called from the worker between steps to recover partial progress.
118 CopyProgress progress() const noexcept;
119
120 private:
121 struct State;
122 explicit FileCopy(State* state);
123 State* state_ = nullptr;
124};
125
126// Creates missing parents for an absolute UTF-16 directory path. The native
127// File Server enforces drive and data-cage permissions.
128absl::Status CreateDirectories(std::u16string_view path);
129
130// A single directory entry; file size is meaningful only for files.
132 // Name relative to the opened directory, in native UTF-16.
133 std::u16string name;
134 // Whether this entry is a directory.
135 bool is_directory = false;
136 // Native read-only attribute.
137 bool is_read_only = false;
138 // Size in bytes for files; empty for directories.
139 std::optional<std::uint64_t> size_bytes;
140};
141
142// A streaming, move-only directory cursor with bounded memory use. Keep use
143// and destruction on the opening thread.
145 public:
146 // Opens a UTF-16 directory path. Entries are returned one at a time.
147 static absl::StatusOr<DirectoryReader> Open(std::u16string_view path);
148
149 DirectoryReader(DirectoryReader&& other) noexcept;
150 DirectoryReader& operator=(DirectoryReader&& other) noexcept;
154
155 // Requests that iteration stop. This is safe from another thread while the
156 // reader remains alive. An in-flight native read may finish before Next()
157 // returns kCancelled. Do not move or destroy concurrently with Cancel().
158 void Cancel() noexcept;
159
160 // Returns an empty optional at end. A cursor is not safe for simultaneous
161 // calls from multiple threads. File Server calls may block: use a worker.
162 absl::StatusOr<std::optional<DirectoryEntry>> Next();
163
164 private:
165 explicit DirectoryReader(NativeDirectory* native) : native_(native) {}
166
167 NativeDirectory* native_ = nullptr;
168 std::atomic<bool> cancel_requested_{false};
169};
170
171} // namespace symbian::api::storage
172
173#endif // SYMBIAN_API_STORAGE_STORAGE_H_
DirectoryReader(const DirectoryReader &)=delete
DirectoryReader & operator=(const DirectoryReader &)=delete
Definition storage.h:97
absl::StatusOr< CopyProgress > Step()
Definition storage.cc:214
~FileCopy()
Definition storage.cc:197
static absl::StatusOr< FileCopy > Open(std::u16string_view source, std::u16string_view destination, WriteMode destination_mode)
Definition storage.cc:159
CopyProgress progress() const noexcept
Definition storage.cc:207
void Cancel() noexcept
Definition storage.cc:201
FileCopy & operator=(FileCopy &&other) noexcept
Definition storage.cc:189
FileCopy(const FileCopy &)=delete
FileCopy & operator=(const FileCopy &)=delete
absl::StatusOr< std::uint64_t > Size() const
Definition storage.cc:261
absl::StatusOr< std::size_t > ReadAt(std::uint64_t offset, std::span< std::byte > destination) const
Definition storage.cc:276
ReadOnlyFile & operator=(const ReadOnlyFile &)=delete
~ReadOnlyFile()
Definition storage.cc:59
ReadOnlyFile(const ReadOnlyFile &)=delete
static absl::StatusOr< ReadOnlyFile > Open(std::u16string_view path)
Definition storage.cc:34
ReadOnlyFile & operator=(ReadOnlyFile &&other) noexcept
Definition storage.cc:51
absl::Status Flush()
Definition storage.cc:120
WritableFile & operator=(const WritableFile &)=delete
WritableFile(const WritableFile &)=delete
WritableFile & operator=(WritableFile &&other) noexcept
Definition storage.cc:86
static absl::StatusOr< WritableFile > Open(std::u16string_view path, WriteMode mode)
Definition storage.cc:63
absl::Status WriteAt(std::uint64_t offset, std::span< const std::byte > source)
Definition storage.cc:98
~WritableFile()
Definition storage.cc:94
Definition exception_count.cc:8
Definition storage.h:17
WriteMode
Definition storage.h:51
absl::Status CreateDirectories(std::u16string_view path)
Definition storage.cc:129
uint32_t offset
Definition exports.cc:70
std::uint64_t total_bytes
Definition storage.h:88
bool complete
Definition storage.h:90
std::uint64_t bytes_copied
Definition storage.h:86
std::u16string name
Definition storage.h:133
std::optional< std::uint64_t > size_bytes
Definition storage.h:139
Definition native_storage.cc:17
Definition native_storage.cc:11