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