Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: isSymbolicLink #754

Merged
merged 5 commits into from
Nov 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions docs/docs/standard-lib/path.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
34 changes: 33 additions & 1 deletion src/optionals/path.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,43 @@ 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;

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);
return EMPTY_VAL;
}

if (!IS_STRING(args[0])) {
runtimeError(vm, "isSymbolicLink() 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;

}
#endif

static Value listDirNative(DictuVM *vm, int argCount, Value *args) {
if (argCount > 1) {
Expand Down Expand Up @@ -330,6 +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);

Expand Down
1 change: 1 addition & 0 deletions tests/path/import.du
Original file line number Diff line number Diff line change
Expand Up @@ -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";
29 changes: 29 additions & 0 deletions tests/path/isSymbolicLink.du
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* isSymbolic.du
*
* Testing Path.isSymbolicLink()
*
* Returns true if the given string is a symbolic Link, 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();
Loading