plat/content.rs#
Source: lib/plat/content.rs
content -- the documents of other apps as files: Android's Storage Access Framework
("content://" names) behind the vfs; the name logic runs on every platform, so the desktop
tests it with a host of its own
ContentFs::new(host) -> a vfs Provider for the names after "content://". On Android the
vfs mounts it on first use with the ContentResolver as its host (android_host.rs,
with_journal in the app's data folder, recover() at once); elsewhere only a test
mounts it (apps/plattest/t_content.rs: a host in memory).
.with_journal(dir) writes go through a copy in dir first (below); recover() finishes the
writes a crash interrupted -> how many
DocHost (trait) what the provider asks the system: query (one document), children (of a
folder document), create, delete, rename, open (a Stream: Android a file descriptor),
generation (a counter the system bumps when a provider reports a change)
DocInfo uri, display name, folder?, size, mtime, flags (DOC_*)
uri_of(path) "content://" + path -- the document's own URI where the name is one
Names (the path after "content://"; the file dialog hands them out, apps join names below
a chosen folder as with any other folder):
AUTH/document/ID one document (ACTION_OPEN_DOCUMENT / ACTION_CREATE_DOCUMENT):
read, write, File streams, stat, display name -- no siblings
AUTH/tree/ID a folder the user chose (ACTION_OPEN_DOCUMENT_TREE)
AUTH/tree/ID/a/b.txt below it by display names (JFLib's own form: every name is
looked up among the children of the folder before it)
AUTH/tree/ID/document/ID2 Android's own form of a document inside a tree
anything else handed to the host as one document
Inside a tree mkdir, write (new files are created), delete (files, empty folders; never the
chosen folder itself), rename (inside one folder, replacing; to another folder: copy and
delete, files only) and dir_list work.
Names below a tree are cached: name -> the document's URI (and folder?), filled by every walk
and listing, forgotten on create / delete / rename through the provider, emptied when the
host's generation changes (Android: a ContentObserver on the provider's authority) and
unused after CACHE_TTL_US. A cached document is asked once (query) before it is used: gone,
renamed or no longer a file / folder, the name is walked again from the nearest cached folder
(from the tree's root at worst). So a name costs one query instead of one listing per
folder; only the leaf is checked, a folder moved elsewhere by another app stays cached until
a change is reported or its time is up.
Writing: SAF has no rename over an existing document (providers pick "name (1)", and a new
document would get a new ID, losing the grants), so the provider says atomic_rename() ==
false: AtomicFile (write_file in pieces, aio) collects the bytes and writes the document in
place. rename replaces by deleting the target first. write (write_file, AtomicFile's commit)
is made as safe as SAF allows:
1. with a journal folder, the new bytes (and the URI) go there first: one file per document
(JOURNAL, written atomically on internal storage);
2. the document is opened truncated ("rwt" / "wt") and written, synced, then read back and
compared (size and every byte) -- once more if that fails;
3. the journal file goes. A crash or a failed write leaves it: recover() (Android: when
content:// is first used in a process) writes it again into the document if it still
exists (else the copy is dropped).
The document keeps its URI (grants stay valid). What remains: between the truncation and the
end of the write other readers see a part; without a journal (none set) a crash leaves that
part; File streams (File::open) write in place without any of this; opening a document for
writing, deleting or renaming it drops its pending journal copy -- once the host did it (a
refused open or rename keeps the copy for recover). Writes of one document with a journal
run one after the other (also recover and the removal of a copy), so a write that succeeds
never removes the copy of another that failed.
/// DocInfo::flags: the document can be written
DOC_WRITE: u32 = 1
/// DocInfo::flags: the document can be deleted
DOC_DELETE: u32 = 2
/// DocInfo::flags: the document can be renamed
DOC_RENAME: u32 = 4
/// DocInfo::flags: the folder takes new documents
DOC_CREATE: u32 = 8
/// how long a cached name is used without a reported change (µs)
CACHE_TTL_US: u64 = 60_000_000
fn listings() -> u64(tests) how many folder listings (DocHost::children) the walks of every ContentFs asked forfn interrupt_next_write(n: u64)(tests) the next write of a document stops after n bytes and fails, its journal copy kept -- what a crash in the middle of a write leavesstruct DocInfoone document as the host describes ituri: String-- the document's URI ("content://AUTH/document/ID", inside a tree "content://AUTH/tree/T/document/ID")name: String-- what the user sees (OpenableColumns.DISPLAY_NAME)dir: boolsize: Option<u64>-- bytes; None when the provider does not knowmtime_ns: i64-- ns since 1970, 0 = unknownflags: u32-- DOC_*
trait DocHost: Send + Syncwhat ContentFs asks the system (Android: the ContentResolver and DocumentsContract)fn query(&self, uri: &str) -> Option<DocInfo>the document behind uri (a tree's URI: its root folder) -> None when it is missing or not allowedfn children(&self, uri: &str) -> Option<Vec<DocInfo>>the documents in the folder document uri (a URI of DocInfo::uri inside a tree)fn create(&self, parent: &str, name: &str, dir: bool) -> Option<DocInfo>a new document (dir: a folder) named name in the folder parent -> its descriptionfn delete(&self, uri: &str) -> boolfn rename(&self, uri: &str, name: &str) -> Option<DocInfo>the document under a new display name in its folder -> its description (the URI may change; a provider may pick another name when name is taken)fn open(&self, uri: &str, mode: u32) -> Option<Box<dyn Stream>>the document opened: FO_READ, FO_WRITE (truncated) or FO_RWfn generation(&self) -> u64a counter that changes when a provider reports a change of its documents (the name cache starts again then); 0 = never
fn uri_of(path: &str) -> String"content://" + pathstruct ContentFs<H: DocHost>the documents of other apps behind "content://" (see the file header)
impl<H: DocHost> ContentFs<H>
fn new(host: H) -> ContentFs<H>fn with_journal(mut self, dir: &str) -> ContentFs<H>keep a copy of every write in dir (a folder of the app's own, any plat name) until the document holds all of it; recover() finishes what a crash interruptedfn host(&self) -> &Hthe host the provider asksfn document(&self, path: &str) -> Option<DocInfo>the document a name (after "content://") means -- its own URI, display name, sizefn recover(&self) -> u32finish the writes a crash (or a failure) left in the journal: each copy is written into its document again if that still exists, then removed; copies of documents that are gone or not granted any more, and damaged ones, are removed -> how many documents were completed