scoop:scoop.platform:0.1.0
src/fs/directory.scoop
Selected production source · src/fs/directory.scoop
1
package scoop.platform.fs3
import scoop.platform.system.failOperatingSystem4
import scoop.platform.system.OsString6
/**7
* One directory entry's native name and path relative to the enumerated directory.8
*/9
public struct DirectoryEntry(val name: OsString, val path: Path)11
/**12
* An owned, single-pass directory cursor implementing both Iterable and Iterator. Close it when13
* finished or when abandoning enumeration.14
*/15
public class DirectoryEntries private constructor(private val path: Path)16
: Iterable<DirectoryEntry>, Iterator<DirectoryEntry>, Closeable {17
private var directory: Ptr<Unit>? = None18
private var finished: Boolean = false19
private var active: Boolean = false20
/**21
* Whether the directory handle has been closed.22
*/23
public val isClosed: Boolean get() = directory == None25
/**26
* Directory cursor construction used by path enumeration.27
*/28
public companion object {29
internal fun open(path: Path): DirectoryEntries {30
val result = DirectoryEntries(path)31
@Unsafe32
{33
val opened =34
path.withPlatformPath {35
name -> @Unsafe36
{37
return@withPlatformPath nativeDirectoryOpen(name)38
}39
}40
if (opened.error != 0)41
failOperatingSystem("open directory", opened.error, Some(path.toOsString()))42
result.directory = opened.owner43
}44
return result45
}46
}48
/**49
* Returns this same single-pass cursor; it does not restart directory enumeration.50
*/51
public override operator fun iterator(): Iterator<DirectoryEntry> = this53
/**54
* Returns the next entry, or None when enumeration is exhausted.55
*/56
public override fun next(): DirectoryEntry? {57
if (finished)58
return None59
if (directory == None)60
throw IllegalStateException(Some("directory is closed"))61
if (active)62
throw IllegalStateException(Some("directory operation is active"))63
active = true64
try {65
@Unsafe66
{67
val entry = nativeDirectoryNext(directory!!)68
if (entry.error != 0)69
failOperatingSystem("read directory", entry.error, Some(path.toOsString()))70
if (entry.name == None) {71
finished = true72
closeNative()73
return None74
}75
val name = copyPlatformString(entry.name!!, entry.length)76
return Some(DirectoryEntry(name, path.resolve(Path.fromOsString(name))))77
}78
} finally {79
active = false80
}81
}83
private fun closeNative() {84
val owned = directory85
directory = None86
when (owned) {87
case Some(val pointer) -> @Unsafe88
{89
nativeDirectoryClose(pointer).checked("close directory", Some(path.toOsString()))90
}91
case None -> {}92
}93
}95
/**96
* Closes the directory enumeration handle.97
*/98
public override fun close() {99
if (directory == None)100
return101
if (active)102
throw IllegalStateException(Some("directory operation is active"))103
closeNative()104
}106
release {107
when (directory) {108
case Some(val pointer) -> @Unsafe109
{110
nativeDirectoryClose(pointer)111
}112
case None -> {}113
}114
}115
}117
@CLayout118
private struct NativeDirectoryOpened(val owner: Ptr<Unit>?, val error: Int)120
@CLayout121
private struct NativeDirectoryEntry(val name: Ptr<Byte>?, val length: Long, val error: Int)123
@Extern(name = "scoop_platform_directory_owner_open")124
private fun nativeDirectoryOpen(path: Ptr<Byte>): NativeDirectoryOpened126
@Extern(name = "scoop_platform_directory_owner_next")127
private fun nativeDirectoryNext(directory: Ptr<Unit>): NativeDirectoryEntry129
@Extern(name = "scoop_platform_directory_owner_close")130
private fun nativeDirectoryClose(directory: Ptr<Unit>): NativeFileResult