- Notifications
You must be signed in to change notification settings - Fork1
Async file system for Kotlin built on libuv
License
datkt/fs
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Asynchronous file system operations for Kotlin built on libuv and basedon thefile system API for Node.js.
Thedatkt.fs
package an be installed with NPM.
$ npm install @datkt/fs
- Kotlin/Native and the
konanc
command line program.
## Compile a program in 'main.kt' and link fs.klib found in `node_modules/`$ konanc main.kt$(konanc-config -clr node_modules/@datkt/fs)
wheremain.kt
might be
importdatkt.fs.*funmain(args:Array<String>) { mkdir("./directory") { err->if (null!= err) {println("Failed to make directory:${err.message}") }else { writeFile("./directory/file","hello world") { err->if (null!= err) {println("Failed to write file:${err.message}") } } } }// kick off file system event loop datkt.fs.loop.run()}
- object loop
- fun access(path, mode, callback)
- fun chmod(path, mode, callback)
- fun chown(path, uid, gid, callback)
- fun lchown(path, uid, gid, callback)
- fun link(source, path, callback)
- fun symlink(source, path, type, callback)
- fun stat(path, callback)
- fun lstat(path, callback)
- fun mkdir(path, mode, callback)
- fun readdir(path, callback)
- fun open(path, flags, mode, callback)
- class Stats(...)
Anobject
that represents an interface into theuv event loop used internally forasynchronous work done by the functions exposed in this package.datkt.fs.loop.run()
will invoke any queued work for the event loop.
object loop {funrun():Intfunstop()}
Invoke the uv event loop. Callsuv_run internally.
Stop the uv event loop. Callsuv_stop internally.
Test user permissions for a file specified atpath
andmode
callingcallback
with anError
if one errors.
access("/home") { err->if (null!= err) {println("Something went wrong checking access to /home") }}
The possible values for themode
argument to test file accesspermissions can be seen below.
F_OK
- Test for the existence of a fileR_OK
- Test for read permissions on a fileW_OK
- Test for write permissions on a fileX_OK
- Test for execution permissions on a file
Change user permissions of a file specified atpath
andmode
callingcallback
with anError
if one occurs.
// make read, write, execute permissions for everyoneval mode= (S_IRUSRorS_IWUSRorS_IXUSRorS_IRGRPorS_IWGRPorS_IXGRPorS_IROTHorS_IWOTHorS_IXOTH)// modify program permissionschmod("/home/program", mode) { err->if (null!= err) {println("Something went wrong changing program permissions:${err.message}") }}
The possible values for themode
argument can be seen below. They canbe grouped into a bit mask by the logical OR (or
) operator.
S_IRWXU
- Read, write, and execute by owner (700
)S_IRUSR
- Read by owner (400
)S_IWUSR
- Write by owner (200
)S_IXUSR
- Execute by owner (100
)S_IRWXG
- Read, write, and Execute by group (070
)S_IRGRP
- Read by group (040
)S_IWGRP
- Write by group (020
)S_IXGRP
- Execute by group (010
)S_IRWXO
- Read, write, and execute by others (007
)S_IROTH
- Read by others (004
)S_IWOTH
- Write by others (002
)S_IXOTH
- Execute by others (001
)
Change user and group ownership of a file specified atpath
for useriduid
, and group idgid
callingcallback
with anError
, if oneoccurs.
chown("/home/file",1000,10) { err->if (null!= err) {println("Something went wrong changing file ownership:${err.message}") }}
Change user and group ownership of a symbolic link specified atpath
foruser iduid
, and group idgid
callingcallback
with anError
, if oneoccurs.
symlink("/home/file","/home/link") { err-> lchown("/home/link",1000,10) { err->if (null!= err) {println("Something went wrong changing link ownership:${err.message}") } }}
Create a new hard link atpath
for a file specified atsource
callingcallback
with anError
, if one occurs.
link("/home/file","/home/link") { err->if (null!= err) {println("Something went creating hard link:${err.message}") }}
symlink("/home/file","/home/symlink") { err->if (null!= err) {println("Something went creating soft link:${err.message}") }}
Query the stats of a file specified atpath
, if it exists, callingcallback
with anError
, if one occurs, otherwise an instance ofStats
as the second argument.
stat("/home/file") { err, stats->if (null!= err) {println(err.message) }else {println(stats) }}
Query the stats of a file specified atpath
, if it is a link or file, callingcallback
with anError
, if one occurs, otherwise an instance ofStats
as the second argument.
lstat("/home/file") { err, stats->if (null!= err) {println(err.message) }else {println(stats) }}
Make a directory specified atpath
with withmode
callingcalling
with anError
, if one occurs. Themode
defaults toDEFAULT_MKDIR_MODE
(see below) if not specified.
mkdir("/path/to/directory") { err->if (null!= err) {println(err.message) }}
SeeFile Modes for a list of all file modes.The default mode is defined by theDEFAULT_MKDIR_MODE
constant.
valDEFAULT_MKDIR_MODE= (S_IRWXUorS_IRWXGorS_IRWXO)
Read a directory specified atpath
for files entries callingcallback
with anError
, if one occurs, otherwise anArray<String>
with file names.
readdir("/home") { err, entries,->for (entryin entries) {println(entry) }}
Open a file specified atpath
with optional flags and mode callingcallback
with anError
, if one occurs, otherwise with a valid filedescriptor.
open("/path/to/file") { err, fd->if (null!= err) {println(err.message) }else {// do something with fd }}
By default, all files will be opened inDEFAULT_OPEN_MODE
where:
// equivalent to (0666)valDEFAULT_OPEN_MODE= (S_IRUSRorS_IWUSRorS_IRGRPorS_IWGRPorS_IROTHorS_IWOTH)
"r"
- Open file for reading. An error occurs if the file does not exist."r+"
- Open file for reading and writing. An error occurs if the file does not exist."w"
- Open file for writing. The file is created if it does notexist, otherwise it is truncated."w+"
- Open file for reading and writing. The file is created if it does notexist, otherwise it is truncated."a"
- Open file for appending. Writes start at the end of the file.The file is created if it does not exist, otherwise it is truncated.
classStats(valdev:Long =0,valmode:Long =0,valnlink:Long =0,valuid:Long =0,valgid:Long =0,valrdev:Long =0,valino:Long =0,valsize:Long =0,valblksize:Long =0,valblocks:Long =0,valatime:Long =0,valmtime:Long =0,valctime:Long =0,valbirthtime:Long =0)
Check if a given mode is present in the stat's mode bit field.
if (stat.hasMode(S_IFREG)) {// stat points to a regular file}
Check if the stat points to acharacterdevice.Equivalent tostat.hasMode(S_IFCHR)
.
if (stat.isCharacterDevice()) {// stat points to a character device}
Check if the stat points to asymbolic link.Equivalent tostat.hasMode(S_IFLNK)
.
if (stat.isSymbolicLink()) {// stat points to a symbolic link}
Check if the stat points to ablockdevice.Equivalent tostat.hasMode(S_IFBLK)
.
if (stat.isBlockDevice()) {// stat points to a block device}
Check if the stat points to a directory. Equivalent tostat.hasMode(S_IFDIR)
.
if (stat.isDirectory()) {// stat points to a directory}
Check if the stat points to a socket. Equivalent tostat.hasMode(S_IFSOCK)
.
if (stat.isSocket()) {// stat points to a socket}
Check if the stat points to aFIFO.Equivalent tostat.hasMode(S_IFIFO)
.
if (stat.isFIFO()) {// stat points to a FIFO (named pipe)}
Check if the stat points to a regular file.Equivalent tostat.hasMode(S_IFREG)
.
if (stat.isFile()) {// stat points to a file}
MIT