scoop:scoop.platform:0.1.0
src/fs/path_operations.scoop
Selected production source · src/fs/path_operations.scoop
1
package scoop.platform.fs3
import scoop.platform.native.decodeFileMetadata4
import scoop.platform.native.requireFileMode5
import scoop.platform.system.*7
/**8
* Moves the path to the destination, replacing an existing entry only when requested.9
*/10
public fun Path.moveTo(destination: Path, replaceExisting: Boolean = false) {11
@Unsafe12
{13
this.withPlatformPath {14
from -> @Unsafe15
{16
destination.withPlatformPath {17
to -> @Unsafe18
{19
nativePathMove(from, to, replaceExisting).checked(20
"move",21
Some(destination.toOsString())22
)23
}24
}25
}26
}27
}28
}30
/**31
* Queries path metadata, following symbolic links unless followLinks is false.32
*/33
public fun Path.metadata(followLinks: Boolean = true): FileMetadata {34
@Unsafe35
{36
return this.withPlatformPath {37
pointer -> @Unsafe38
{39
val result = nativePathMetadata(pointer, followLinks)40
return@withPlatformPath decodeFileMetadata(41
result.data,42
result.error,43
Some(this.toOsString())44
)45
}46
}47
}48
}50
/**51
* Tests whether path metadata is available; ordinary absence returns false while other platform52
* failures propagate.53
*/54
public fun Path.exists(followLinks: Boolean = true): Boolean {55
try {56
this.metadata(followLinks);57
return true58
}59
catch (error: PlatformException) {60
if (61
error.kind == PlatformErrorKind.NotFound || error.kind == PlatformErrorKind.NotDirectory62
)63
return false64
throw error65
}66
}68
/**69
* Creates one directory with requested permissions, subject to umask.70
*/71
public fun Path.createDirectory(mode: Int = 0x1ff) {72
requireFileMode(mode)73
@Unsafe74
{75
this.withPlatformPath {76
pointer -> @Unsafe77
{78
nativePathMkdir(pointer, mode).checked("create directory", Some(this.toOsString()))79
}80
}81
}82
}84
/**85
* Removes the selected file or empty directory.86
*/87
public fun Path.delete() {88
@Unsafe89
{90
this.withPlatformPath {91
pointer -> @Unsafe92
{93
nativePathDelete(pointer).checked("delete", Some(this.toOsString()))94
}95
}96
}97
}99
@Extern(name = "scoop_platform_path_mkdir_result")100
private fun nativePathMkdir(path: Ptr<Byte>, mode: Int): NativeFileResult102
@Extern(name = "scoop_platform_path_delete_result")103
private fun nativePathDelete(path: Ptr<Byte>): NativeFileResult105
@Extern(name = "scoop_platform_path_move_result")106
private fun nativePathMove(107
source: Ptr<Byte>,108
destination: Ptr<Byte>,109
replace: Boolean110
): NativeFileResult