Scoop API

scoop:scoop.platform:0.1.0

src/fs/directory.scoop

Selected production source · src/fs/directory.scoop

1package scoop.platform.fs
3import scoop.platform.system.failOperatingSystem
4import scoop.platform.system.OsString
6/**
7 * One directory entry's native name and path relative to the enumerated directory.
8 */
9public struct DirectoryEntry(val name: OsString, val path: Path)
11/**
12 * An owned, single-pass directory cursor implementing both Iterable and Iterator. Close it when
13 * finished or when abandoning enumeration.
14 */
15public class DirectoryEntries private constructor(private val path: Path)
16: Iterable<DirectoryEntry>, Iterator<DirectoryEntry>, Closeable {
17 private var directory: Ptr<Unit>? = None
18 private var finished: Boolean = false
19 private var active: Boolean = false
20 /**
21 * Whether the directory handle has been closed.
22 */
23 public val isClosed: Boolean get() = directory == None
25 /**
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 @Unsafe
32 {
33 val opened =
34 path.withPlatformPath {
35 name -> @Unsafe
36 {
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.owner
43 }
44 return result
45 }
46 }
48 /**
49 * Returns this same single-pass cursor; it does not restart directory enumeration.
50 */
51 public override operator fun iterator(): Iterator<DirectoryEntry> = this
53 /**
54 * Returns the next entry, or None when enumeration is exhausted.
55 */
56 public override fun next(): DirectoryEntry? {
57 if (finished)
58 return None
59 if (directory == None)
60 throw IllegalStateException(Some("directory is closed"))
61 if (active)
62 throw IllegalStateException(Some("directory operation is active"))
63 active = true
64 try {
65 @Unsafe
66 {
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 = true
72 closeNative()
73 return None
74 }
75 val name = copyPlatformString(entry.name!!, entry.length)
76 return Some(DirectoryEntry(name, path.resolve(Path.fromOsString(name))))
77 }
78 } finally {
79 active = false
80 }
81 }
83 private fun closeNative() {
84 val owned = directory
85 directory = None
86 when (owned) {
87 case Some(val pointer) -> @Unsafe
88 {
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 return
101 if (active)
102 throw IllegalStateException(Some("directory operation is active"))
103 closeNative()
104 }
106 release {
107 when (directory) {
108 case Some(val pointer) -> @Unsafe
109 {
110 nativeDirectoryClose(pointer)
111 }
112 case None -> {}
113 }
114 }
117@CLayout
118private struct NativeDirectoryOpened(val owner: Ptr<Unit>?, val error: Int)
120@CLayout
121private struct NativeDirectoryEntry(val name: Ptr<Byte>?, val length: Long, val error: Int)
123@Extern(name = "scoop_platform_directory_owner_open")
124private fun nativeDirectoryOpen(path: Ptr<Byte>): NativeDirectoryOpened
126@Extern(name = "scoop_platform_directory_owner_next")
127private fun nativeDirectoryNext(directory: Ptr<Unit>): NativeDirectoryEntry
129@Extern(name = "scoop_platform_directory_owner_close")
130private fun nativeDirectoryClose(directory: Ptr<Unit>): NativeFileResult