From 63bd4a876e613e67f5cff065f55b04eac7ebf703 Mon Sep 17 00:00:00 2001 From: dzaima Date: Mon, 9 May 2022 22:57:37 +0300 Subject: [PATCH] =?UTF-8?q?=E2=80=A2file.CreateDir?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/builtins/sysfn.c | 10 ++++++++-- src/utils/file.c | 9 ++++++++- src/utils/file.h | 1 + 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/builtins/sysfn.c b/src/builtins/sysfn.c index 5743216b..5a3b0a9b 100644 --- a/src/builtins/sysfn.c +++ b/src/builtins/sysfn.c @@ -675,9 +675,14 @@ static NFnDesc* fTypeDesc; static NFnDesc* fExistsDesc; static NFnDesc* fListDesc; static NFnDesc* fMapBytesDesc; +static NFnDesc* createdirDesc; B list_c1(B d, B x) { return path_list(path_rel(nfn_objU(d), x)); } +B createdir_c1(B d, B x) { + if (dir_create(path_rel(nfn_objU(d), x))) return m_i32(1); + thrM("(file).CreateDir: Failed to create directory"); +} B ftype_c1(B d, B x) { char ty = path_type(path_rel(nfn_objU(d), x)); @@ -1095,7 +1100,7 @@ B sys_c1(B t, B x) { if(!fileNS.u) { REQ_PATH; #define F(X) m_nfn(X##Desc, inc(path)) - fileNS = m_nns(file_nsGen, q_N(path)? m_c32(0) : inc(path), F(fileAt), F(fList), F(fBytes), F(fChars), F(fLines), F(fType), F(fExists), inc(bi_fName), F(fMapBytes)); + fileNS = m_nns(file_nsGen, q_N(path)? m_c32(0) : inc(path), F(fileAt), F(fList), F(fBytes), F(fChars), F(fLines), F(fType), F(fExists), inc(bi_fName), F(fMapBytes), F(createdir)); #undef F } cr = inc(fileNS); @@ -1163,12 +1168,13 @@ void sysfn_init() { fBytesDesc = registerNFn(m_str8l("(file).Bytes"), fbytes_c1, fbytes_c2); fListDesc = registerNFn(m_str8l("(file).List"), list_c1, c2_bad); fTypeDesc = registerNFn(m_str8l("(file).Type"), ftype_c1, c2_bad); + createdirDesc = registerNFn(m_str8l("(file).CreateDir"), createdir_c1, c2_bad); fMapBytesDesc = registerNFn(m_str8l("(file).MapBytes"), mapBytes_c1, c2_bad); fExistsDesc = registerNFn(m_str8l("(file).Exists"), fexists_c1, c2_bad); importDesc = registerNFn(m_str32(U"•Import"), import_c1, import_c2); reBQNDesc = registerNFn(m_str8l("(REPL)"), repl_c1, repl_c2); } void sysfnPost_init() { - file_nsGen = m_nnsDesc("path","at","list","bytes","chars","lines","type","exists","name","mapbytes"); + file_nsGen = m_nnsDesc("path","at","list","bytes","chars","lines","type","exists","name","mapbytes","createdir"); c(BMd1,bi_bitcast)->im = bitcast_im; } diff --git a/src/utils/file.c b/src/utils/file.c index 126cdc27..5da570ac 100644 --- a/src/utils/file.c +++ b/src/utils/file.c @@ -302,10 +302,17 @@ B mmap_file(B path) { void mmap_init() { } #endif - #include #include +bool dir_create(B path) { + char* p = toCStr(path); + bool r = mkdir(p, S_IRWXU) == 0; + freeCStr(p); + dec(path); + return r; +} + char path_type(B path) { char* p = toCStr(path); struct stat path_stat; diff --git a/src/utils/file.h b/src/utils/file.h index 7cb85a16..b2127d46 100644 --- a/src/utils/file.h +++ b/src/utils/file.h @@ -14,6 +14,7 @@ B path_lines(B path); // consumes I8Arr* stream_bytes(FILE* f); B mmap_file(B path); // consumes +bool dir_create(B path); // consumes void path_wChars(B path, B x); // consumes path void path_wBytes(B path, B x); // consumes path