From b8277be5b23fb2bb8979f649021b846391d7432d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C5=8Dshin?= Date: Mon, 31 Jan 2022 13:19:32 -0600 Subject: [PATCH] vere: remove walk, move save to unix --- pkg/urbit/include/vere/vere.h | 32 +--- pkg/urbit/vere/io/term.c | 29 +-- pkg/urbit/vere/io/unix.c | 77 ++++++++ pkg/urbit/vere/pier.c | 9 +- pkg/urbit/vere/walk.c | 326 ---------------------------------- 5 files changed, 91 insertions(+), 382 deletions(-) delete mode 100644 pkg/urbit/vere/walk.c diff --git a/pkg/urbit/include/vere/vere.h b/pkg/urbit/include/vere/vere.h index 3bf787856..5f1ad15e0 100644 --- a/pkg/urbit/include/vere/vere.h +++ b/pkg/urbit/include/vere/vere.h @@ -1034,33 +1034,6 @@ void u3_lord_peek(u3_lord* god_u, u3_pico* pic_u); - /** Filesystem (new api). - **/ - /* u3_walk_load(): load file or bail. - */ - u3_noun - u3_walk_load(c3_c* pas_c); - - /* u3_walk_safe(): load file or 0. - */ - u3_noun - u3_walk_safe(c3_c* pas_c); - - /* u3_walk_save(): save file or bail. - */ - void - u3_walk_save(c3_c* pas_c, u3_noun tim, u3_atom pad, c3_c* bas_c, u3_noun pax); - - /* u3_walk(): traverse `dir_c` to produce an arch, updating `old`. - */ - u3_noun - u3_walk(const c3_c* dir_c, u3_noun old); - - /* u3_path(): C unix path in computer for file or directory. - */ - c3_c* - u3_path(c3_o fyl, u3_noun pax); - /** Filesystem (async) **/ /* u3_foil_folder(): load directory, blockingly. create if nonexistent. @@ -1168,6 +1141,11 @@ /** Storage, new school. **/ + /* u3_unix_save(): save file undir .../.urb/[bas_m] or bail. + */ + void + u3_unix_save(c3_m bas_m, c3_c* pax_c, u3_atom pad); + /* u3_unix_cane(): true iff (unix) path is canonical. */ c3_t diff --git a/pkg/urbit/vere/io/term.c b/pkg/urbit/vere/io/term.c index fcccf748a..a4c16daed 100644 --- a/pkg/urbit/vere/io/term.c +++ b/pkg/urbit/vere/io/term.c @@ -549,14 +549,13 @@ _term_it_show_more(u3_utty* uty_u) /* _term_it_path(): path for console file. */ static c3_c* -_term_it_path(c3_o fyl, u3_noun pax) +_term_it_path(u3_noun pax) { - c3_w len_w; + c3_w len_w = 0; c3_c *pas_c; // measure // - len_w = strlen(u3_Host.dir_c); { u3_noun wiz = pax; @@ -569,7 +568,6 @@ _term_it_path(c3_o fyl, u3_noun pax) // cut // pas_c = c3_malloc(len_w + 1); - strncpy(pas_c, u3_Host.dir_c, len_w); pas_c[len_w] = '\0'; { u3_noun wiz = pax; @@ -578,7 +576,7 @@ _term_it_path(c3_o fyl, u3_noun pax) while ( u3_nul != wiz ) { c3_w tis_w = u3r_met(3, u3h(wiz)); - if ( (c3y == fyl) && (u3_nul == u3t(wiz)) ) { + if ( (u3_nul == u3t(wiz)) ) { *waq_c++ = '.'; } else *waq_c++ = '/'; @@ -598,27 +596,10 @@ _term_it_path(c3_o fyl, u3_noun pax) static void _term_it_save(u3_noun pax, u3_noun pad) { - c3_c* pax_c; - c3_c* bas_c = 0; - c3_w xap_w = u3kb_lent(u3k(pax)); - u3_noun xap = u3_nul; - u3_noun urb = c3_s4('.','u','r','b'); - u3_noun put = c3_s3('p','u','t'); - - // directory base and relative path - if ( 2 < xap_w ) { - u3_noun bas = u3nt(urb, put, u3_nul); - bas_c = _term_it_path(c3n, bas); - xap = u3qb_scag(xap_w - 2, pax); - } - - pax = u3nt(urb, put, pax); - pax_c = _term_it_path(c3y, pax); - - u3_walk_save(pax_c, 0, pad, bas_c, xap); + c3_c* pax_c = _term_it_path(pax); + u3_unix_save(c3__put, pax_c, pad); c3_free(pax_c); - c3_free(bas_c); } /* _term_ovum_plan(): plan term ovums, configuring spinner. diff --git a/pkg/urbit/vere/io/unix.c b/pkg/urbit/vere/io/unix.c index e6423f495..26067ea56 100644 --- a/pkg/urbit/vere/io/unix.c +++ b/pkg/urbit/vere/io/unix.c @@ -243,6 +243,83 @@ _unix_string_to_path(u3_unix* unx_u, c3_c* pax_c) } } +/* _unix_mkdirp(): recursive mkdir of dirname of pax_c. +*/ +static void +_unix_mkdirp(c3_c* pax_c) +{ + c3_c* fas_c = strchr(pax_c + 1, '/'); + + while ( fas_c ) { + *fas_c = 0; + if ( 0 != mkdir(pax_c, 0777) && EEXIST != errno ) { + u3l_log("unix: mkdir %s: %s\n", pax_c, strerror(errno)); + u3m_bail(c3__fail); + } + *fas_c++ = '/'; + fas_c = strchr(fas_c, '/'); + } +} + +/* u3_unix_save(): save file under .../.urb/[bas_m] or bail. +** +** XX this is quite bad, and doesn't share much in common with +** the rest of unix.c. at minimum it should instead take pax as +** a noun and build the C path from that. it would also be nice +** if it could take a u3_udir or something corresponding to the +** base directory. +*/ +void +u3_unix_save(c3_m bas_m, c3_c* pax_c, u3_atom pad) +{ + c3_i fid_i; + c3_w lod_w, len_w, fln_w, rit_w; + c3_y* pad_y; + c3_c* ful_c; + + c3_assert(3 == u3r_met(3, bas_m)); + c3_assert(c3_s3('b','h','k') != bas_m && + c3_s3('c','h','k') != bas_m && + c3_s3('g','e','t') != bas_m && + c3_s3('l','o','g') != bas_m); + if ( '/' == *pax_c) { + pax_c++; + } + if ( !u3_unix_cane(pax_c) ) { + u3l_log("%s: non-canonical path\n", pax_c); + u3z(pad); u3m_bail(c3__fail); + } + lod_w = strlen(u3_Host.dir_c); + len_w = lod_w + sizeof("/.urb/xxx/") + strlen(pax_c); + ful_c = c3_malloc(len_w); + rit_w = snprintf(ful_c, len_w, "%s/.urb/xxx/%s", u3_Host.dir_c, pax_c); + c3_assert(len_w == rit_w + 1); + u3r_bytes(0, 3, (c3_y*)ful_c + lod_w + sizeof("/.urb/") - 1, bas_m); + + _unix_mkdirp(ful_c); + fid_i = c3_open(ful_c, O_WRONLY | O_CREAT | O_TRUNC, 0666); + if ( fid_i < 0 ) { + u3l_log("%s: %s\n", ful_c, strerror(errno)); + c3_free(ful_c); + u3z(pad); u3m_bail(c3__fail); + } + + fln_w = u3r_met(3, pad); + pad_y = c3_malloc(fln_w); + u3r_bytes(0, fln_w, pad_y, pad); + u3z(pad); + rit_w = write(fid_i, pad_y, fln_w); + close(fid_i); + c3_free(pad_y); + + if ( rit_w != fln_w ) { + u3l_log("%s: %s\n", ful_c, strerror(errno)); + c3_free(ful_c); + u3m_bail(c3__fail); + } + c3_free(ful_c); +} + /* _unix_rm_r_cb(): callback to delete individual files/directories */ static c3_i diff --git a/pkg/urbit/vere/pier.c b/pkg/urbit/vere/pier.c index 9aad54105..2f7fc5b85 100644 --- a/pkg/urbit/vere/pier.c +++ b/pkg/urbit/vere/pier.c @@ -526,12 +526,11 @@ _pier_on_scry_done(void* ptr_v, u3_noun nun) // if serialization and export path succeeded, write to disk // if ( (u3_none != out) && (u3_none != pad) ) { - c3_c fil_c[2048]; - snprintf(fil_c, 2048, "%s/.urb/put/%s.%s", - pir_u->pax_c, pac_c+1, ext_c); + c3_c fil_c[256]; + snprintf(fil_c, 256, "%s.%s", pac_c + 1, ext_c); - u3_walk_save(fil_c, 0, out, pir_u->pax_c, pad); - u3l_log("pier: scry result in %s\n", fil_c); + u3_unix_save(c3__put, fil_c, pad); + u3l_log("pier: scry result in %s/.urb/put/%s\n", u3_Host.dir_c, fil_c); } } diff --git a/pkg/urbit/vere/walk.c b/pkg/urbit/vere/walk.c deleted file mode 100644 index 1e0eb1f47..000000000 --- a/pkg/urbit/vere/walk.c +++ /dev/null @@ -1,326 +0,0 @@ -/* vere/walk.c -** -*/ -#include "all.h" -#include "vere/vere.h" - - /* |% - ** ++ arch :: fs node - ** $% [& p=@uvI q=*] :: file, hash/data - ** [| p=(map ,@ta arch)] :: directory - ** == :: - ** -- - */ - -#if 0 -static u3_noun -_walk_ok(u3_noun nod) -{ - u3_noun don = u3n_mung(u3k(u2A->toy.arch), u3k(nod)); - - if ( c3n == u3_sing(nod, don) ) { - c3_assert(0); - } - u3z(don); - return nod; -} -#endif - -/* u3_walk_safe(): load file or 0. -*/ -u3_noun -u3_walk_safe(c3_c* pas_c) -{ - struct stat buf_b; - c3_i fid_i = c3_open(pas_c, O_RDONLY, 0644); - c3_w fln_w, red_w; - c3_y* pad_y; - - if ( (fid_i < 0) || (fstat(fid_i, &buf_b) < 0) ) { - // u3l_log("%s: %s\n", pas_c, strerror(errno)); - return 0; - } - fln_w = buf_b.st_size; - pad_y = c3_malloc(buf_b.st_size); - - red_w = read(fid_i, pad_y, fln_w); - close(fid_i); - - if ( fln_w != red_w ) { - c3_free(pad_y); - return 0; - } - else { - u3_noun pad = u3i_bytes(fln_w, (c3_y *)pad_y); - c3_free(pad_y); - - return pad; - } -} - -/* u3_walk_load(): load file or bail. -*/ -u3_noun -u3_walk_load(c3_c* pas_c) -{ - struct stat buf_b; - c3_i fid_i = c3_open(pas_c, O_RDONLY, 0644); - c3_w fln_w, red_w; - c3_y* pad_y; - - if ( (fid_i < 0) || (fstat(fid_i, &buf_b) < 0) ) { - u3l_log("%s: %s\n", pas_c, strerror(errno)); - return u3m_bail(c3__fail); - } - fln_w = buf_b.st_size; - pad_y = c3_malloc(buf_b.st_size); - - red_w = read(fid_i, pad_y, fln_w); - close(fid_i); - - if ( fln_w != red_w ) { - c3_free(pad_y); - u3l_log("u3_walk_load failed"); - return u3m_bail(c3__fail); - } - else { - u3_noun pad = u3i_bytes(fln_w, (c3_y *)pad_y); - c3_free(pad_y); - - return pad; - } -} - -/* _walk_mkdirp(): recursively make directories in pax at bas_c (RETAIN) -*/ -static void -_walk_mkdirp(c3_c* bas_c, u3_noun pax) -{ - c3_c* pax_c; - c3_y* waq_y; - c3_w pax_w, fas_w, len_w; - - if ( u3_nul == pax ) { - return; - } - - pax_w = u3r_met(3, u3h(pax)); - fas_w = strlen(bas_c); - len_w = 1 + fas_w + pax_w; - - pax_c = c3_malloc(1 + len_w); - strcpy(pax_c, bas_c); - - pax_c[fas_w] = '/'; - waq_y = (void*)(1 + pax_c + fas_w); - u3r_bytes(0, pax_w, waq_y, u3h(pax)); - pax_c[len_w] = '\0'; - - if ( 0 != c3_mkdir(pax_c, 0755) && EEXIST != errno ) { - u3l_log("error mkdiring %s: %s\n", pax_c, strerror(errno)); - u3m_bail(c3__fail); - } - - _walk_mkdirp(pax_c, u3t(pax)); - c3_free(pax_c); -} - -/* u3_walk_save(): save file or bail. -*/ -void -u3_walk_save(c3_c* pas_c, u3_noun tim, u3_atom pad, c3_c* bas_c, u3_noun pax) -{ - c3_i fid_i = c3_open(pas_c, O_WRONLY | O_CREAT | O_TRUNC, 0666); - c3_w fln_w, rit_w; - c3_y* pad_y; - - if ( fid_i < 0 ) { - if ( ENOENT == errno && u3_nul != pax ) { - _walk_mkdirp(bas_c, pax); - return u3_walk_save(pas_c, tim, pad, 0, u3_nul); - } - - u3l_log("%s: %s\n", pas_c, strerror(errno)); - u3m_bail(c3__fail); - } - - fln_w = u3r_met(3, pad); - pad_y = c3_malloc(fln_w); - u3r_bytes(0, fln_w, pad_y, pad); - u3z(pad); - u3z(pax); - - rit_w = write(fid_i, pad_y, fln_w); - close(fid_i); - c3_free(pad_y); - - if ( rit_w != fln_w ) { - u3l_log("%s: %s\n", pas_c, strerror(errno)); - u3m_bail(c3__fail); - } - - if ( 0 != tim ) { - struct timeval tim_tv[2]; - - u3_time_out_tv(&tim_tv[0], u3k(tim)); - u3_time_out_tv(&tim_tv[1], tim); - - utimes(pas_c, tim_tv); - } -} - -/* _walk_in(): inner loop of _walk(), producing map. -*/ -static u3_noun -_walk_in(const c3_c* dir_c, c3_w len_w) -{ - DIR* dir_d = c3_opendir(dir_c); - u3_noun map = u3_nul; - - if ( !dir_d ) { - return u3_nul; - } - else while ( 1 ) { - struct dirent ent_n; - struct dirent* out_n; - - if ( u3_readdir_r(dir_d, &ent_n, &out_n) != 0 ) { - u3l_log("%s: %s\n", dir_c, strerror(errno)); - break; - } - else if ( !out_n ) { - break; - } - else if ( !strcmp(out_n->d_name, ".") || - !strcmp(out_n->d_name, "..") || - ('~' == out_n->d_name[0]) || - ('.' == out_n->d_name[0]) ) // XX restricts some spans - { - continue; - } - else { - c3_c* fil_c = out_n->d_name; - c3_w lef_w = len_w + 1 + strlen(fil_c); - c3_c* pat_c = c3_malloc(lef_w + 1); - struct stat buf_b; - - strncpy(pat_c, dir_c, lef_w); - pat_c[len_w] = '/'; - strncpy(pat_c + len_w + 1, fil_c, lef_w); - pat_c[lef_w] = '\0'; - - if ( 0 != stat(pat_c, &buf_b) ) { - c3_free(pat_c); - } else { - u3_noun tim = c3_stat_mtime(&buf_b); - - if ( !S_ISDIR(buf_b.st_mode) ) { - c3_c* dot_c = strrchr(fil_c, '.'); - c3_c* nam_c = strdup(fil_c); - c3_c* ext_c = strdup(dot_c + 1); - - nam_c[dot_c - fil_c] = 0; - { - u3_noun nam = u3i_string(nam_c); - u3_noun ext = u3i_string(ext_c); - u3_noun get = u3kdb_get(u3k(map), u3k(nam)); - u3_noun dat = u3_walk_load(pat_c); - u3_noun hax; - - if ( !strcmp("noun", ext_c) ) { - dat = u3ke_cue(dat); - } - hax = u3do("sham", u3k(dat)); - if ( u3_none == get ) { get = u3_nul; } - - get = u3kdb_put(get, ext, u3nt(c3y, hax, dat)); - map = u3kdb_put(map, nam, u3nc(c3n, get)); - } - c3_free(nam_c); - c3_free(ext_c); - } - else { - u3_noun dir = _walk_in(pat_c, lef_w); - - if ( u3_nul != dir ) { - map = u3kdb_put - (map, u3i_string(fil_c), u3nc(c3n, dir)); - } - else u3z(tim); - } - c3_free(pat_c); - } - } - } - closedir(dir_d); - return map; -} - -/* u3_walk(): traverse `dir_c` to produce an arch, updating `old`. -*/ -u3_noun -u3_walk(const c3_c* dir_c, u3_noun old) -{ - // XX - obviously, cheaper to update old data. - u3z(old); - { - struct stat buf_b; - - if ( 0 != stat(dir_c, &buf_b) ) { - u3l_log("can't stat %s\n", dir_c); - // return u3m_bail(c3__fail); - c3_assert(0); - } - else { - return u3nc(c3n, - _walk_in(dir_c, strlen(dir_c))); - } - } -} - -/* u3_path(): C unix path in computer for file or directory. -*/ -c3_c* -u3_path(c3_o fyl, u3_noun pax) -{ - c3_w len_w; - c3_c *pas_c; - - // measure - // - len_w = strlen(u3_Local); - { - u3_noun wiz = pax; - - while ( u3_nul != wiz ) { - len_w += (1 + u3r_met(3, u3h(wiz))); - wiz = u3t(wiz); - } - } - - // cut - // - pas_c = c3_malloc(len_w + 1); - strncpy(pas_c, u3_Local, len_w); - pas_c[len_w] = '\0'; - { - u3_noun wiz = pax; - c3_c* waq_c = (pas_c + strlen(pas_c)); - - while ( u3_nul != wiz ) { - c3_w tis_w = u3r_met(3, u3h(wiz)); - - if ( (c3y == fyl) && (u3_nul == u3t(wiz)) ) { - *waq_c++ = '.'; - } else *waq_c++ = '/'; - - u3r_bytes(0, tis_w, (c3_y*)waq_c, u3h(wiz)); - waq_c += tis_w; - - wiz = u3t(wiz); - } - *waq_c = 0; - } - u3z(pax); - return pas_c; -}