From 2751fbc51da58a01cef2d3289ee32399ca2668f2 Mon Sep 17 00:00:00 2001 From: Brian Downs Date: Fri, 20 Oct 2023 15:20:06 -0700 Subject: [PATCH 1/9] move copyFile to IO module Signed-off-by: Brian Downs --- docs/docs/standard-lib/io.md | 10 ++++++ docs/docs/standard-lib/system.md | 10 ------ examples/copyFile.du | 3 +- src/optionals/io.c | 57 ++++++++++++++++++++++++++++++++ src/optionals/system.c | 38 --------------------- tests/io/copyFile.du | 34 +++++++++++++++++++ tests/io/import.du | 7 ++++ tests/runTests.du | 1 + 8 files changed, 111 insertions(+), 49 deletions(-) create mode 100644 tests/io/copyFile.du create mode 100644 tests/io/import.du diff --git a/docs/docs/standard-lib/io.md b/docs/docs/standard-lib/io.md index f3a81781..1dad57b7 100644 --- a/docs/docs/standard-lib/io.md +++ b/docs/docs/standard-lib/io.md @@ -41,3 +41,13 @@ Prints a given list of values to stdout with an appended newline character. IO.println("Dictu!"); // Dictu! ``` + +### IO.copyFile(String: src, String: dst) -> Result + +Copies the contents from the source file to the destination file. + +Returns a Result type and on success will unwrap to nil. + +```cs +IO.copyFile(src, dst); +``` diff --git a/docs/docs/standard-lib/system.md b/docs/docs/standard-lib/system.md index 05238970..9306457a 100644 --- a/docs/docs/standard-lib/system.md +++ b/docs/docs/standard-lib/system.md @@ -280,13 +280,3 @@ Note: This is not available on Windows systems. System.mkdirTemp().unwrap(); // "VOO16s" System.mkdirTemp("test_XXXXXX").unwrap(); // "test_0bL2qS" ``` - -### System.copyFile(String: src, String: dst) -> Result - -Copies the contents from the source file to the destination file. - -Returns a Result type and on success will unwrap to nil. - -```cs -System.copyFile(src, dst); -``` diff --git a/examples/copyFile.du b/examples/copyFile.du index 9f8ec5db..76c47da1 100644 --- a/examples/copyFile.du +++ b/examples/copyFile.du @@ -1,3 +1,4 @@ +import IO; import Path; import System; @@ -36,7 +37,7 @@ def cleanup(tmpDir) { print(file.read()); } - System.copyFile(Path.join(tmpDir, srcFile), Path.join(tmpDir, dstFile)).match( + IO.copyFile(Path.join(tmpDir, srcFile), Path.join(tmpDir, dstFile)).match( def(result) => result, def(error) => { print(error); diff --git a/src/optionals/io.c b/src/optionals/io.c index 5ef8ad48..267f5f6c 100644 --- a/src/optionals/io.c +++ b/src/optionals/io.c @@ -1,3 +1,11 @@ +#include +#include +#if defined(__APPLE__) || defined(__FreeBSD__) +#include +#else +#include +#endif + #include "io.h" @@ -28,6 +36,52 @@ static Value printlnIO(DictuVM *vm, int argCount, Value *args) { return NIL_VAL; } +//#ifndef _WIN32 +static Value copyFileIO(DictuVM *vm, int argCount, Value *args) { + if (argCount != 2) { + runtimeError(vm, "copyFile() takes 2 arguments (%d given)", argCount); + return EMPTY_VAL; + } + + if (!IS_STRING(args[0])) { + runtimeError(vm, "src argument needs to be a string"); + return EMPTY_VAL; + } + + if (!IS_STRING(args[1])) { + runtimeError(vm, "dst argument needs to be a string"); + return EMPTY_VAL; + } + + char *src = AS_STRING(args[0])->chars; + char *dst = AS_STRING(args[1])->chars; + + int in = 0; + int out = 0; + + if ((in = open(src, O_RDONLY)) == -1) { + return newResultError(vm, "failed to open src file"); + } + if ((out = creat(dst, 0660)) == -1) { + close(in); + return newResultError(vm, "failed to create/open dst file"); + } + +#if defined(__APPLE__) || defined(__FreeBSD__) + fcopyfile(in, out, 0, COPYFILE_ALL); +#else + off_t bytes = 0; + struct stat fileinfo = {0}; + fstat(in, &fileinfo); + sendfile(out, in, &bytes, fileinfo.st_size); +#endif + close(in); + close(out); + + return newResultSuccess(vm, NIL_VAL); +} +//#endif + Value createIOModule(DictuVM *vm) { ObjString *name = copyString(vm, "IO", 2); push(vm, OBJ_VAL(name)); @@ -43,6 +97,9 @@ Value createIOModule(DictuVM *vm) { */ defineNative(vm, &module->values, "print", printIO); defineNative(vm, &module->values, "println", printlnIO); +//#ifndef _WIN32 + defineNative(vm, &module->values, "copyFile", copyFileIO); +//#endif pop(vm); pop(vm); diff --git a/src/optionals/system.c b/src/optionals/system.c index f8ac6f95..da7ed6e6 100644 --- a/src/optionals/system.c +++ b/src/optionals/system.c @@ -490,43 +490,6 @@ static Value chmodNative(DictuVM *vm, int argCount, Value *args) { return newResultSuccess(vm, NIL_VAL); } -static Value copyFileNative(DictuVM *vm, int argCount, Value *args) { - if (argCount != 2) { - runtimeError(vm, "copyFile() takes 2 arguments (%d given).", argCount); - return EMPTY_VAL; - } - - if (!IS_STRING(args[0]) || !IS_STRING(args[1])) { - runtimeError(vm, "copyFile() arguments must be strings."); - return EMPTY_VAL; - } - - char *srcFile = AS_STRING(args[0])->chars; - char *dstFile = AS_STRING(args[1])->chars; - - FILE *sf = fopen(srcFile, "r"); - if (sf == NULL) { - return newResultError(vm, "cannot open src file"); - } - - FILE *df = fopen(dstFile, "w"); - if (df == NULL) { - fclose(sf); - return newResultError(vm, "cannot open dst file"); - } - - int buffer = fgetc(sf); - while (buffer != EOF) { - fputc(buffer, df); - buffer = fgetc(sf); - } - - fclose(sf); - fclose(df); - - return newResultSuccess(vm, NIL_VAL); -} - void initArgv(DictuVM *vm, Table *table, int argc, char **argv) { ObjList *list = newList(vm); push(vm, OBJ_VAL(list)); @@ -627,7 +590,6 @@ Value createSystemModule(DictuVM *vm) { defineNative(vm, &module->values, "sleep", sleepNative); defineNative(vm, &module->values, "exit", exitNative); defineNative(vm, &module->values, "chmod", chmodNative); - defineNative(vm, &module->values, "copyFile", copyFileNative); /** * Define System properties diff --git a/tests/io/copyFile.du b/tests/io/copyFile.du new file mode 100644 index 00000000..5b306ba6 --- /dev/null +++ b/tests/io/copyFile.du @@ -0,0 +1,34 @@ +/** + * copyFile.du + * + * Testing the IO.copyFile() method + */ +from UnitTest import UnitTest; + +import IO; +import System; + +class TestIOCopyFile < UnitTest { + const testFile1 = "tests/io/test1"; + const testFile2 = "tests/io/test2"; + + setUp() { + with(this.testFile1, "w") { + this.written = file.write("Dictu!"); + } + } + + tearDown() { + System.remove(this.testFile1); + System.remove(this.testFile2); + } + + testIOCopyFile() { + const res = IO.copyFile(this.testFile1, this.testFile2); + this.assertSuccess(res); + } +} + +//if (System.platform != "windows") { + TestIOCopyFile().run(); +//} diff --git a/tests/io/import.du b/tests/io/import.du new file mode 100644 index 00000000..5324db54 --- /dev/null +++ b/tests/io/import.du @@ -0,0 +1,7 @@ +/** + * import.du + * + * General import file for the IO module tests + */ + +import "copyFile.du"; diff --git a/tests/runTests.du b/tests/runTests.du index 75b366b3..77e3e641 100644 --- a/tests/runTests.du +++ b/tests/runTests.du @@ -35,6 +35,7 @@ import "base64/import.du"; import "sqlite/import.du"; import "process/import.du"; import "inspect/import.du"; +import "io/import.du"; import "unittest/import.du"; if (isDefined("HTTP")) { From b26c3c1451f509ad0fe6662e2dd0b4e86a2ebf66 Mon Sep 17 00:00:00 2001 From: Brian Downs Date: Fri, 20 Oct 2023 15:26:58 -0700 Subject: [PATCH 2/9] move test Signed-off-by: Brian Downs --- tests/io/copyFile.du | 47 +++++++++++++++++++++++++----------- tests/system/copyFile.du | 52 ---------------------------------------- 2 files changed, 33 insertions(+), 66 deletions(-) delete mode 100644 tests/system/copyFile.du diff --git a/tests/io/copyFile.du b/tests/io/copyFile.du index 5b306ba6..60d38836 100644 --- a/tests/io/copyFile.du +++ b/tests/io/copyFile.du @@ -1,34 +1,53 @@ /** * copyFile.du * - * Testing the IO.copyFile() method + * Testing the IO.copyFile() function + * + * copyFile() copies the contents from the source file to the + * destination file. */ from UnitTest import UnitTest; import IO; +import Path; import System; -class TestIOCopyFile < UnitTest { - const testFile1 = "tests/io/test1"; - const testFile2 = "tests/io/test2"; +const srcFile = "src_file", + dstFile = "dst_file"; + +class TestSystemCopyFile < UnitTest { + private tmpDir; setUp() { - with(this.testFile1, "w") { - this.written = file.write("Dictu!"); - } + this.tmpDir = "temp"; + System.mkdir("temp"); } tearDown() { - System.remove(this.testFile1); - System.remove(this.testFile2); + Path.listDir(this.tmpDir).forEach(def(f) => { + System.remove(Path.join(this.tmpDir, f)); + }); + + System.rmdir(this.tmpDir); } - testIOCopyFile() { - const res = IO.copyFile(this.testFile1, this.testFile2); + testCopyFile() { + with(Path.join(this.tmpDir, srcFile), 'w') { + file.write("lots and lots of temp data!"); + } + + with(Path.join(this.tmpDir, srcFile), 'r') { + print(file.read()); + } + + const srcFullPath = Path.join(this.tmpDir, srcFile); + const dstFullPath = Path.join(this.tmpDir, dstFile); + + const res = IO.copyFile(srcFullPath, dstFullPath); + + this.assertNotNil(res); this.assertSuccess(res); } } -//if (System.platform != "windows") { - TestIOCopyFile().run(); -//} +TestSystemCopyFile().run(); \ No newline at end of file diff --git a/tests/system/copyFile.du b/tests/system/copyFile.du deleted file mode 100644 index 9ef369aa..00000000 --- a/tests/system/copyFile.du +++ /dev/null @@ -1,52 +0,0 @@ -/** - * copyFile.du - * - * Testing the System.copyFile() function - * - * copyFile() copies the contents from the source file to the - * destination file. - */ -from UnitTest import UnitTest; - -import Path; -import System; - -const srcFile = "src_file", - dstFile = "dst_file"; - -class TestSystemCopyFile < UnitTest { - private tmpDir; - - setUp() { - this.tmpDir = "temp"; - System.mkdir("temp"); - } - - tearDown() { - Path.listDir(this.tmpDir).forEach(def(f) => { - System.remove(Path.join(this.tmpDir, f)); - }); - - System.rmdir(this.tmpDir); - } - - testCopyFile() { - with(Path.join(this.tmpDir, srcFile), 'w') { - file.write("lots and lots of temp data!"); - } - - with(Path.join(this.tmpDir, srcFile), 'r') { - print(file.read()); - } - - const srcFullPath = Path.join(this.tmpDir, srcFile); - const dstFullPath = Path.join(this.tmpDir, dstFile); - - const res = System.copyFile(srcFullPath, dstFullPath); - - this.assertNotNil(res); - this.assertSuccess(res); - } -} - -TestSystemCopyFile().run(); From ff8a81dc7966519a93dcc39d25a10a6ef166ae62 Mon Sep 17 00:00:00 2001 From: Brian Downs Date: Fri, 20 Oct 2023 21:28:46 -0700 Subject: [PATCH 3/9] update for windows Signed-off-by: Brian Downs --- src/optionals/io.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/optionals/io.c b/src/optionals/io.c index 267f5f6c..07c2eb63 100644 --- a/src/optionals/io.c +++ b/src/optionals/io.c @@ -1,8 +1,9 @@ #include -#include #if defined(__APPLE__) || defined(__FreeBSD__) +#include #include #else +#include #include #endif @@ -69,11 +70,17 @@ static Value copyFileIO(DictuVM *vm, int argCount, Value *args) { #if defined(__APPLE__) || defined(__FreeBSD__) fcopyfile(in, out, 0, COPYFILE_ALL); -#else +#elif defined(__linux__) off_t bytes = 0; struct stat fileinfo = {0}; fstat(in, &fileinfo); sendfile(out, in, &bytes, fileinfo.st_size); +#elif defined(_WIN32) + int buffer = fgetc(in); + while (buffer != EOF) { + fputc(buffer, out); + buffer = fgetc(in); + } #endif close(in); close(out); @@ -97,9 +104,7 @@ Value createIOModule(DictuVM *vm) { */ defineNative(vm, &module->values, "print", printIO); defineNative(vm, &module->values, "println", printlnIO); -//#ifndef _WIN32 defineNative(vm, &module->values, "copyFile", copyFileIO); -//#endif pop(vm); pop(vm); From 357dbf7b7ce9a345cfb60741c42465aa4a111b4d Mon Sep 17 00:00:00 2001 From: Brian Downs Date: Fri, 20 Oct 2023 21:54:23 -0700 Subject: [PATCH 4/9] update for windows Signed-off-by: Brian Downs --- src/optionals/io.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/optionals/io.c b/src/optionals/io.c index 07c2eb63..3ae0d172 100644 --- a/src/optionals/io.c +++ b/src/optionals/io.c @@ -1,9 +1,10 @@ #include -#if defined(__APPLE__) || defined(__FreeBSD__) +#ifndef _WIN32 #include +#endif +#if defined(__APPLE__) || defined(__FreeBSD__) #include #else -#include #include #endif From a7ea54160eb0eb625a6c586c5c31f69297b023e9 Mon Sep 17 00:00:00 2001 From: Brian Downs Date: Tue, 24 Oct 2023 16:31:47 -0700 Subject: [PATCH 5/9] update for windows Signed-off-by: Brian Downs --- src/optionals/io.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/optionals/io.c b/src/optionals/io.c index 3ae0d172..1cd6f5d0 100644 --- a/src/optionals/io.c +++ b/src/optionals/io.c @@ -4,7 +4,7 @@ #endif #if defined(__APPLE__) || defined(__FreeBSD__) #include -#else +#elif defined(__linux__) #include #endif From 027324b7677e814cf388fc7504d3de295d62666a Mon Sep 17 00:00:00 2001 From: Brian Downs Date: Tue, 24 Oct 2023 20:33:32 -0700 Subject: [PATCH 6/9] update for windows Signed-off-by: Brian Downs --- src/optionals/io.c | 49 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 8 deletions(-) diff --git a/src/optionals/io.c b/src/optionals/io.c index 1cd6f5d0..664d2f86 100644 --- a/src/optionals/io.c +++ b/src/optionals/io.c @@ -38,7 +38,46 @@ static Value printlnIO(DictuVM *vm, int argCount, Value *args) { return NIL_VAL; } -//#ifndef _WIN32 +#ifdef _WIN32 +static Value copyFileIO(DictuVM *vm, int argCount, Value *args) { + if (argCount != 2) { + runtimeError(vm, "copyFile() takes 2 arguments (%d given).", argCount); + return EMPTY_VAL; + } + + if (!IS_STRING(args[0]) || !IS_STRING(args[1])) { + runtimeError(vm, "copyFile() arguments must be strings."); + return EMPTY_VAL; + } + + char *srcFile = AS_STRING(args[0])->chars; + char *dstFile = AS_STRING(args[1])->chars; + + FILE *sf = fopen(srcFile, "r"); + if (sf == NULL) { + return newResultError(vm, "cannot open src file"); + } + + FILE *df = fopen(dstFile, "w"); + if (df == NULL) { + fclose(sf); + return newResultError(vm, "cannot open dst file"); + } + + int buffer = fgetc(sf); + while (buffer != EOF) { + fputc(buffer, df); + buffer = fgetc(sf); + } + + fclose(sf); + fclose(df); + + return newResultSuccess(vm, NIL_VAL); +} +#endif + +#ifndef _WIN32 static Value copyFileIO(DictuVM *vm, int argCount, Value *args) { if (argCount != 2) { runtimeError(vm, "copyFile() takes 2 arguments (%d given)", argCount); @@ -76,19 +115,13 @@ static Value copyFileIO(DictuVM *vm, int argCount, Value *args) { struct stat fileinfo = {0}; fstat(in, &fileinfo); sendfile(out, in, &bytes, fileinfo.st_size); -#elif defined(_WIN32) - int buffer = fgetc(in); - while (buffer != EOF) { - fputc(buffer, out); - buffer = fgetc(in); - } #endif close(in); close(out); return newResultSuccess(vm, NIL_VAL); } -//#endif +#endif Value createIOModule(DictuVM *vm) { ObjString *name = copyString(vm, "IO", 2); From 85de82f1bb6f06290378ea211f106be5fe1da6c5 Mon Sep 17 00:00:00 2001 From: Brian Downs Date: Tue, 24 Oct 2023 20:39:48 -0700 Subject: [PATCH 7/9] update for windows Signed-off-by: Brian Downs --- tests/system/import.du | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/system/import.du b/tests/system/import.du index 49c45b7d..218f88fb 100644 --- a/tests/system/import.du +++ b/tests/system/import.du @@ -5,7 +5,6 @@ */ import "access.du"; -import "copyFile.du"; import "version.du"; import "sleep.du"; import "getCWD.du"; From bbce099ce9f6068cd45c4981814125b3820f2faa Mon Sep 17 00:00:00 2001 From: Brian Downs Date: Tue, 24 Oct 2023 20:46:33 -0700 Subject: [PATCH 8/9] update arg handling Signed-off-by: Brian Downs --- src/optionals/io.c | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/optionals/io.c b/src/optionals/io.c index 664d2f86..877233b3 100644 --- a/src/optionals/io.c +++ b/src/optionals/io.c @@ -84,14 +84,9 @@ static Value copyFileIO(DictuVM *vm, int argCount, Value *args) { return EMPTY_VAL; } - if (!IS_STRING(args[0])) { - runtimeError(vm, "src argument needs to be a string"); - return EMPTY_VAL; - } - - if (!IS_STRING(args[1])) { - runtimeError(vm, "dst argument needs to be a string"); - return EMPTY_VAL; + if (!IS_STRING(args[0]) || !IS_STRING(args[1])) { + runtimeError(vm, "copyFile() arguments must be strings."); + return EMPTY_VAL; } char *src = AS_STRING(args[0])->chars; From de2ad2fa267a4e5e10775e01232bf4e96dd003db Mon Sep 17 00:00:00 2001 From: Jason_000 Date: Wed, 1 Nov 2023 00:49:30 +0000 Subject: [PATCH 9/9] Update docs/docs/standard-lib/io.md --- docs/docs/standard-lib/io.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/docs/standard-lib/io.md b/docs/docs/standard-lib/io.md index 1dad57b7..b0b59bd7 100644 --- a/docs/docs/standard-lib/io.md +++ b/docs/docs/standard-lib/io.md @@ -42,7 +42,7 @@ IO.println("Dictu!"); // Dictu! ``` -### IO.copyFile(String: src, String: dst) -> Result +### IO.copyFile(String: src, String: dst) -> Result\ Copies the contents from the source file to the destination file.