From c3aa5927c722d86468734b213d5741571f131b79 Mon Sep 17 00:00:00 2001 From: Liz3 Date: Thu, 31 Oct 2024 01:32:15 +0100 Subject: [PATCH 1/5] feat: isSymbolicLink Theres a issue where isDir is checked on a broken symbolic link, it will return true falsely. This adds checks for the return value of isDir and also adds a new function called `isSymbolicLink` which checks if a file is a symbolicLink --- docs/docs/standard-lib/path.md | 10 ++++++++++ src/optionals/path.c | 31 ++++++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/docs/docs/standard-lib/path.md b/docs/docs/standard-lib/path.md index d17ccae90..29d6f9692 100644 --- a/docs/docs/standard-lib/path.md +++ b/docs/docs/standard-lib/path.md @@ -93,6 +93,16 @@ Checks whether a given path points to a directory or not. Path.isDir("/usr/bin/"); //true ``` +### Path.isSymbolicLink(String) -> Boolean + +Checks whether a given path is a symbolic link. + +**Note:** This is not available on windows systems. + +```cs +Path.isSymbolicLink("/usr/bin/"); //false +``` + ### Path.listDir(String) -> List Returns a list of strings containing the contents of the input path. diff --git a/src/optionals/path.c b/src/optionals/path.c index b7336f422..372b23d03 100644 --- a/src/optionals/path.c +++ b/src/optionals/path.c @@ -146,7 +146,10 @@ static Value isdirNative(DictuVM *vm, int argCount, Value *args) { char *path = AS_CSTRING(args[0]); struct stat path_stat; - stat(path, &path_stat); + int ret = stat(path, &path_stat); + + if (ret < 0) + return FALSE_VAL; if (S_ISDIR(path_stat.st_mode)) return TRUE_VAL; @@ -155,6 +158,31 @@ static Value isdirNative(DictuVM *vm, int argCount, Value *args) { } +static Value isSymlinkNative(DictuVM *vm, int argCount, Value *args) { + if (argCount != 1) { + runtimeError(vm, "isDir() takes 1 argument (%d given)", argCount); + return EMPTY_VAL; + } + + if (!IS_STRING(args[0])) { + runtimeError(vm, "isDir() argument must be a string"); + return EMPTY_VAL; + } + + char *path = AS_CSTRING(args[0]); + struct stat path_stat; + int ret = lstat(path, &path_stat); + + if(ret < 0) + return FALSE_VAL; + + if (S_ISLNK(path_stat.st_mode)) + return TRUE_VAL; + + return FALSE_VAL; + +} + static Value listDirNative(DictuVM *vm, int argCount, Value *args) { if (argCount > 1) { runtimeError(vm, "listDir() takes 0 or 1 arguments (%d given)", argCount); @@ -330,6 +358,7 @@ Value createPathModule(DictuVM *vm) { defineNative(vm, &module->values, "dirname", dirnameNative); defineNative(vm, &module->values, "exists", existsNative); defineNative(vm, &module->values, "isDir", isdirNative); + defineNative(vm, &module->values, "isSymbolicLink", isSymlinkNative); defineNative(vm, &module->values, "listDir", listDirNative); defineNative(vm, &module->values, "join", joinNative); From 860c0a57971cd8a2aee38a9074ad5ac1689683b7 Mon Sep 17 00:00:00 2001 From: Liz3 Date: Thu, 31 Oct 2024 01:36:57 +0100 Subject: [PATCH 2/5] fix --- src/optionals/path.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/optionals/path.c b/src/optionals/path.c index 372b23d03..da4ed6944 100644 --- a/src/optionals/path.c +++ b/src/optionals/path.c @@ -160,12 +160,12 @@ static Value isdirNative(DictuVM *vm, int argCount, Value *args) { static Value isSymlinkNative(DictuVM *vm, int argCount, Value *args) { if (argCount != 1) { - runtimeError(vm, "isDir() takes 1 argument (%d given)", argCount); + runtimeError(vm, "isSymbolicLink() takes 1 argument (%d given)", argCount); return EMPTY_VAL; } if (!IS_STRING(args[0])) { - runtimeError(vm, "isDir() argument must be a string"); + runtimeError(vm, "isSymbolicLink() argument must be a string"); return EMPTY_VAL; } From 336c1a9407a98820a5544b73c652a57ec699ec7e Mon Sep 17 00:00:00 2001 From: Liz3 Date: Thu, 31 Oct 2024 06:00:33 +0100 Subject: [PATCH 3/5] add tests --- tests/path/import.du | 1 + tests/path/isSymbolicLink.du | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 tests/path/isSymbolicLink.du diff --git a/tests/path/import.du b/tests/path/import.du index d58d0bc89..ad647228d 100644 --- a/tests/path/import.du +++ b/tests/path/import.du @@ -11,5 +11,6 @@ import "isAbsolute.du"; import "realpath.du"; import "exists.du"; import "isDir.du"; +import "isSymbolicLink.du"; import "join.du"; import "listDir.du"; diff --git a/tests/path/isSymbolicLink.du b/tests/path/isSymbolicLink.du new file mode 100644 index 000000000..97c8bd8be --- /dev/null +++ b/tests/path/isSymbolicLink.du @@ -0,0 +1,29 @@ +/** + * isDir.du + * + * Testing Path.isDir() + * + * Returns true if the given string is a path to a directory, else false. (Linux only) + */ +from UnitTest import UnitTest; + +import Path; +import System; +import Process; + +class TestPathIsSL < UnitTest { + setUp() { + Process.run(["ln", "-s", "README.md", "README.md.sl"]); + } + tearDown() { + System.remove("README.md.sl"); + } + testIsSL() { + this.assertTruthy(Path.isSymbolicLink("README.md.sl")); + this.assertFalsey(Path.isSymbolicLink("README.md")); + } + +} + +if (System.platform != "windows") + TestPathIsSL().run(); From de799f5716125a9e7c4ce2c90bc4d89f875b1ce4 Mon Sep 17 00:00:00 2001 From: Liz3 Date: Thu, 31 Oct 2024 06:02:21 +0100 Subject: [PATCH 4/5] fix comment --- tests/path/isSymbolicLink.du | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/path/isSymbolicLink.du b/tests/path/isSymbolicLink.du index 97c8bd8be..b2cd06b19 100644 --- a/tests/path/isSymbolicLink.du +++ b/tests/path/isSymbolicLink.du @@ -1,9 +1,9 @@ /** - * isDir.du + * isSymbolic.du * - * Testing Path.isDir() + * Testing Path.isSymbolicLink() * - * Returns true if the given string is a path to a directory, else false. (Linux only) + * Returns true if the given string is a symbolic Link, else false. (Linux only) */ from UnitTest import UnitTest; From 7e125f02e5cad2c5596cb77d4550db701d1ac7fa Mon Sep 17 00:00:00 2001 From: Liz3 Date: Sun, 3 Nov 2024 02:32:44 +0100 Subject: [PATCH 5/5] not on windows --- src/optionals/path.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/optionals/path.c b/src/optionals/path.c index da4ed6944..26735e1df 100644 --- a/src/optionals/path.c +++ b/src/optionals/path.c @@ -157,7 +157,7 @@ static Value isdirNative(DictuVM *vm, int argCount, Value *args) { return FALSE_VAL; } - +#ifndef _WIN32 static Value isSymlinkNative(DictuVM *vm, int argCount, Value *args) { if (argCount != 1) { runtimeError(vm, "isSymbolicLink() takes 1 argument (%d given)", argCount); @@ -182,6 +182,7 @@ static Value isSymlinkNative(DictuVM *vm, int argCount, Value *args) { return FALSE_VAL; } +#endif static Value listDirNative(DictuVM *vm, int argCount, Value *args) { if (argCount > 1) { @@ -358,7 +359,9 @@ Value createPathModule(DictuVM *vm) { defineNative(vm, &module->values, "dirname", dirnameNative); defineNative(vm, &module->values, "exists", existsNative); defineNative(vm, &module->values, "isDir", isdirNative); +#ifndef _WIN32 defineNative(vm, &module->values, "isSymbolicLink", isSymlinkNative); +#endif defineNative(vm, &module->values, "listDir", listDirNative); defineNative(vm, &module->values, "join", joinNative);