-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathPosixFileSystem.kt
More file actions
106 lines (84 loc) · 3.28 KB
/
Copy pathPosixFileSystem.kt
File metadata and controls
106 lines (84 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
* Copyright (C) 2020 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package okio
import kotlinx.cinterop.get
import okio.Path.Companion.toPath
import okio.internal.openPosixDirectory
import okio.internal.toPath
import platform.posix.EEXIST
import platform.posix.errno
import platform.posix.set_posix_errno
internal object PosixFileSystem : FileSystem() {
private val SELF_DIRECTORY_ENTRY = ".".toPath()
private val PARENT_DIRECTORY_ENTRY = "..".toPath()
override fun canonicalize(path: Path) = variantCanonicalize(path)
override fun metadataOrNull(path: Path) = variantMetadataOrNull(path)
override fun list(dir: Path): List<Path> = list(dir, throwOnFailure = true)!!
override fun listOrNull(dir: Path): List<Path>? = list(dir, throwOnFailure = false)
private fun list(dir: Path, throwOnFailure: Boolean): List<Path>? {
val posixDir = openPosixDirectory(dir)
?: if (throwOnFailure) throw errnoToIOException(errno) else return null
posixDir.use {
val result = mutableListOf<Path>()
val buffer = Buffer()
set_posix_errno(0) // If readdir() returns null it's either the end or an error.
while (true) {
val dirent = it.nextEntry() ?: break
val childPath = buffer.writeNullTerminated(
bytes = dirent[0].d_name,
).toPath(normalize = true)
if (childPath == SELF_DIRECTORY_ENTRY || childPath == PARENT_DIRECTORY_ENTRY) {
continue // exclude '.' and '..' from the results.
}
result += dir / childPath
}
if (errno != 0) {
if (throwOnFailure) {
throw errnoToIOException(errno)
} else {
return null
}
}
result.sort()
return result
}
}
override fun openReadOnly(file: Path) = variantOpenReadOnly(file)
override fun openReadWrite(file: Path, mustCreate: Boolean, mustExist: Boolean): FileHandle {
return variantOpenReadWrite(file, mustCreate = mustCreate, mustExist = mustExist)
}
override fun source(file: Path) = variantSource(file)
override fun sink(file: Path, mustCreate: Boolean) = variantSink(file, mustCreate)
override fun appendingSink(file: Path, mustExist: Boolean) = variantAppendingSink(file, mustExist)
override fun createDirectory(dir: Path, mustCreate: Boolean) {
val result = variantMkdir(dir)
if (result != 0) {
if (errno == EEXIST && !mustCreate) return // Already exists.
throw errnoToIOException(errno)
}
}
override fun atomicMove(
source: Path,
target: Path,
) {
variantMove(source, target)
}
override fun delete(path: Path, mustExist: Boolean) {
variantDelete(path, mustExist)
}
override fun createSymlink(source: Path, target: Path) = variantCreateSymlink(source, target)
override fun toString() = "PosixSystemFileSystem"
}