xref: /aosp_15_r20/external/okio/docs/file_system.md (revision f9742813c14b702d71392179818a9e591da8620c)
1*f9742813SAndroid Build Coastguard WorkerFile System
2*f9742813SAndroid Build Coastguard Worker===========
3*f9742813SAndroid Build Coastguard Worker
4*f9742813SAndroid Build Coastguard WorkerOkio's file system is designed to be easy, testable, multiplatform, and efficient.
5*f9742813SAndroid Build Coastguard Worker
6*f9742813SAndroid Build Coastguard Worker### Easy
7*f9742813SAndroid Build Coastguard Worker
8*f9742813SAndroid Build Coastguard WorkerReading and writing files is concise yet flexible.
9*f9742813SAndroid Build Coastguard Worker
10*f9742813SAndroid Build Coastguard Worker```kotlin
11*f9742813SAndroid Build Coastguard Workerval path = "README.md".toPath()
12*f9742813SAndroid Build Coastguard Worker
13*f9742813SAndroid Build Coastguard Workerval readmeContent = FileSystem.SYSTEM.read(path) {
14*f9742813SAndroid Build Coastguard Worker  readUtf8()
15*f9742813SAndroid Build Coastguard Worker}
16*f9742813SAndroid Build Coastguard Worker
17*f9742813SAndroid Build Coastguard Workerval updatedContent = readmeContent.replace("red", "blue")
18*f9742813SAndroid Build Coastguard Worker
19*f9742813SAndroid Build Coastguard WorkerFileSystem.SYSTEM.write(path) {
20*f9742813SAndroid Build Coastguard Worker  writeUtf8(updatedContent)
21*f9742813SAndroid Build Coastguard Worker}
22*f9742813SAndroid Build Coastguard Worker```
23*f9742813SAndroid Build Coastguard Worker
24*f9742813SAndroid Build Coastguard Worker
25*f9742813SAndroid Build Coastguard Worker### Testable
26*f9742813SAndroid Build Coastguard Worker
27*f9742813SAndroid Build Coastguard WorkerIt's easy to swap out the real file system with a fake. This makes tests run faster and more
28*f9742813SAndroid Build Coastguard Workerreliably.
29*f9742813SAndroid Build Coastguard Worker
30*f9742813SAndroid Build Coastguard Worker```kotlin
31*f9742813SAndroid Build Coastguard Workerval fileSystem = FakeFileSystem()
32*f9742813SAndroid Build Coastguard Workerval userHome = "/Users/sandy".toPath()
33*f9742813SAndroid Build Coastguard Workerval gitConfig = userHome / ".gitconfig"
34*f9742813SAndroid Build Coastguard Worker
35*f9742813SAndroid Build Coastguard WorkerfileSystem.createDirectories(userHome)
36*f9742813SAndroid Build Coastguard Workerval original = """
37*f9742813SAndroid Build Coastguard Worker    |[user]
38*f9742813SAndroid Build Coastguard Worker    |  email = [email protected]
39*f9742813SAndroid Build Coastguard Worker    |""".trimMargin()
40*f9742813SAndroid Build Coastguard WorkerfileSystem.write(gitConfig) { writeUtf8(original) }
41*f9742813SAndroid Build Coastguard Worker
42*f9742813SAndroid Build Coastguard WorkerGitConfigFixer(fileSystem).fix(userHome)
43*f9742813SAndroid Build Coastguard Worker
44*f9742813SAndroid Build Coastguard Workerval expected = """
45*f9742813SAndroid Build Coastguard Worker  |[user]
46*f9742813SAndroid Build Coastguard Worker  |  email = [email protected]
47*f9742813SAndroid Build Coastguard Worker  |[diff]
48*f9742813SAndroid Build Coastguard Worker  |  renames = true
49*f9742813SAndroid Build Coastguard Worker  |  indentHeuristic = on
50*f9742813SAndroid Build Coastguard Worker  """.trimIndent()
51*f9742813SAndroid Build Coastguard WorkerassertEquals(expected, fileSystem.read(gitConfig) { readUtf8() })
52*f9742813SAndroid Build Coastguard Worker```
53*f9742813SAndroid Build Coastguard Worker
54*f9742813SAndroid Build Coastguard WorkerWith `ForwardingFileSystem` you can easily inject faults to confirm your program is graceful even
55*f9742813SAndroid Build Coastguard Workerwhen the user's disk fills up.
56*f9742813SAndroid Build Coastguard Worker
57*f9742813SAndroid Build Coastguard Worker
58*f9742813SAndroid Build Coastguard Worker### Multiplatform
59*f9742813SAndroid Build Coastguard Worker
60*f9742813SAndroid Build Coastguard WorkerOkio’s `Path` class supports Windows-style (like `C:\autoexec.bat`) and UNIX-style paths
61*f9742813SAndroid Build Coastguard Worker(like `/etc/passwd`). It supports manipulating Windows paths on UNIX, and UNIX paths on Windows.
62*f9742813SAndroid Build Coastguard Worker
63*f9742813SAndroid Build Coastguard WorkerThe system `FileSystem` abstracts over these platform APIs:
64*f9742813SAndroid Build Coastguard Worker
65*f9742813SAndroid Build Coastguard Worker * Android API levels <26: [java.io.File](https://developer.android.com/reference/java/io/File)
66*f9742813SAndroid Build Coastguard Worker * Java and Android API level 26+: [java.nio.file](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/nio/file/FileSystem.html)
67*f9742813SAndroid Build Coastguard Worker * Linux: [man pages](https://www.kernel.org/doc/man-pages/)
68*f9742813SAndroid Build Coastguard Worker * UNIX: [stdio.h](https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/stdio.h.html)
69*f9742813SAndroid Build Coastguard Worker * Windows: [fileapi.h](https://docs.microsoft.com/en-us/windows/win32/api/fileapi/)
70*f9742813SAndroid Build Coastguard Worker * Node.js: [file system](https://nodejs.org/api/fs.html)
71*f9742813SAndroid Build Coastguard Worker
72*f9742813SAndroid Build Coastguard Worker
73*f9742813SAndroid Build Coastguard Worker### Efficient
74*f9742813SAndroid Build Coastguard Worker
75*f9742813SAndroid Build Coastguard WorkerRead and write operations integrate with Okio buffers to reduce the number of system calls.
76*f9742813SAndroid Build Coastguard Worker
77*f9742813SAndroid Build Coastguard WorkerIt exposes high-level operations like `atomicMove()` and `metadata` to get the OS to do all the work
78*f9742813SAndroid Build Coastguard Workerwhen appropriate.
79*f9742813SAndroid Build Coastguard Worker
80*f9742813SAndroid Build Coastguard Worker
81*f9742813SAndroid Build Coastguard Worker## Known Issues
82*f9742813SAndroid Build Coastguard Worker
83*f9742813SAndroid Build Coastguard Worker
84*f9742813SAndroid Build Coastguard WorkerOkio's implementation is constrained by the capabilities its underlying APIs. This page is an
85*f9742813SAndroid Build Coastguard Workeroverview of these limitations.
86*f9742813SAndroid Build Coastguard Worker
87*f9742813SAndroid Build Coastguard Worker
88*f9742813SAndroid Build Coastguard Worker### All Platforms
89*f9742813SAndroid Build Coastguard Worker
90*f9742813SAndroid Build Coastguard Worker * There are no APIs for file permissions, watches, volume management, memory mapping, or locking.
91*f9742813SAndroid Build Coastguard Worker * Paths that cannot be represented as UTF-8 strings are unsupported. The underlying APIs that Okio
92*f9742813SAndroid Build Coastguard Worker   calls through, including `java.io.File`, all treat paths as strings.
93*f9742813SAndroid Build Coastguard Worker
94*f9742813SAndroid Build Coastguard Worker
95*f9742813SAndroid Build Coastguard Worker### Kotlin/JVM
96*f9742813SAndroid Build Coastguard Worker
97*f9742813SAndroid Build Coastguard Worker#### On Android, API level less than 26:
98*f9742813SAndroid Build Coastguard Worker
99*f9742813SAndroid Build Coastguard Worker * Creating and accessing symlinks is unsupported.
100*f9742813SAndroid Build Coastguard Worker
101*f9742813SAndroid Build Coastguard Worker
102*f9742813SAndroid Build Coastguard Worker#### On Windows:
103*f9742813SAndroid Build Coastguard Worker
104*f9742813SAndroid Build Coastguard Worker * `FileSystem.atomicMove()` fails if the target file already exists.
105*f9742813SAndroid Build Coastguard Worker
106*f9742813SAndroid Build Coastguard Worker
107*f9742813SAndroid Build Coastguard Worker### Kotlin/Native
108*f9742813SAndroid Build Coastguard Worker
109*f9742813SAndroid Build Coastguard Worker * FakeFileSystem does not support concurrent use. We are [holding off on this][fake_fs_concurrency]
110*f9742813SAndroid Build Coastguard Worker   until the upcoming memory model is released.
111*f9742813SAndroid Build Coastguard Worker
112*f9742813SAndroid Build Coastguard Worker#### On Windows:
113*f9742813SAndroid Build Coastguard Worker
114*f9742813SAndroid Build Coastguard Worker * Creating and accessing symlinks is unsupported.
115*f9742813SAndroid Build Coastguard Worker
116*f9742813SAndroid Build Coastguard Worker
117*f9742813SAndroid Build Coastguard Worker### Kotlin/JS
118*f9742813SAndroid Build Coastguard Worker
119*f9742813SAndroid Build Coastguard Worker * NodeJsFileSystem's `source()` and `sink()` cannot access UNIX pipes.
120*f9742813SAndroid Build Coastguard Worker * Instead of returning null, `NodeJsFileSystem.metadataOrNull()` throws `IOException` if the path
121*f9742813SAndroid Build Coastguard Worker   is invalid. (In the Node.js API there's no mechanism to differentiate between a failure to read
122*f9742813SAndroid Build Coastguard Worker   a valid path and a rejection of an invalid path.)
123*f9742813SAndroid Build Coastguard Worker
124*f9742813SAndroid Build Coastguard Worker
125*f9742813SAndroid Build Coastguard Worker[fake_fs_concurrency]: https://github.com/square/okio/issues/950
126