Scoop API

scoop:scoop.platform:0.1.0

src/fs/path_operations.scoop

Selected production source · src/fs/path_operations.scoop

1package scoop.platform.fs
3import scoop.platform.native.decodeFileMetadata
4import scoop.platform.native.requireFileMode
5import scoop.platform.system.*
7/**
8 * Moves the path to the destination, replacing an existing entry only when requested.
9 */
10public fun Path.moveTo(destination: Path, replaceExisting: Boolean = false) {
11 @Unsafe
12 {
13 this.withPlatformPath {
14 from -> @Unsafe
15 {
16 destination.withPlatformPath {
17 to -> @Unsafe
18 {
19 nativePathMove(from, to, replaceExisting).checked(
20 "move",
21 Some(destination.toOsString())
22 )
23 }
24 }
25 }
26 }
27 }
30/**
31 * Queries path metadata, following symbolic links unless followLinks is false.
32 */
33public fun Path.metadata(followLinks: Boolean = true): FileMetadata {
34 @Unsafe
35 {
36 return this.withPlatformPath {
37 pointer -> @Unsafe
38 {
39 val result = nativePathMetadata(pointer, followLinks)
40 return@withPlatformPath decodeFileMetadata(
41 result.data,
42 result.error,
43 Some(this.toOsString())
44 )
45 }
46 }
47 }
50/**
51 * Tests whether path metadata is available; ordinary absence returns false while other platform
52 * failures propagate.
53 */
54public fun Path.exists(followLinks: Boolean = true): Boolean {
55 try {
56 this.metadata(followLinks);
57 return true
58 }
59 catch (error: PlatformException) {
60 if (
61 error.kind == PlatformErrorKind.NotFound || error.kind == PlatformErrorKind.NotDirectory
62 )
63 return false
64 throw error
65 }
68/**
69 * Creates one directory with requested permissions, subject to umask.
70 */
71public fun Path.createDirectory(mode: Int = 0x1ff) {
72 requireFileMode(mode)
73 @Unsafe
74 {
75 this.withPlatformPath {
76 pointer -> @Unsafe
77 {
78 nativePathMkdir(pointer, mode).checked("create directory", Some(this.toOsString()))
79 }
80 }
81 }
84/**
85 * Removes the selected file or empty directory.
86 */
87public fun Path.delete() {
88 @Unsafe
89 {
90 this.withPlatformPath {
91 pointer -> @Unsafe
92 {
93 nativePathDelete(pointer).checked("delete", Some(this.toOsString()))
94 }
95 }
96 }
99@Extern(name = "scoop_platform_path_mkdir_result")
100private fun nativePathMkdir(path: Ptr<Byte>, mode: Int): NativeFileResult
102@Extern(name = "scoop_platform_path_delete_result")
103private fun nativePathDelete(path: Ptr<Byte>): NativeFileResult
105@Extern(name = "scoop_platform_path_move_result")
106private fun nativePathMove(
107 source: Ptr<Byte>,
108 destination: Ptr<Byte>,
109 replace: Boolean
110): NativeFileResult