shrub/v/unix.c

1711 lines
38 KiB
C
Raw Normal View History

2013-09-29 00:21:18 +04:00
/* v/unix.c
**
** This file is in the public domain.
*/
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <setjmp.h>
#include <gmp.h>
#include <dirent.h>
#include <stdint.h>
#include <uv.h>
#include <termios.h>
#include <term.h>
#include <errno.h>
2015-02-27 09:35:41 +03:00
#include <libgen.h>
2013-09-29 00:21:18 +04:00
#include "all.h"
#include "v/vere.h"
2014-11-20 03:46:16 +03:00
/* _unix_ship_in(): c3_w[4] to ship.
*/
2015-02-27 09:35:41 +03:00
static u3_noun
2014-11-20 03:46:16 +03:00
_unix_ship_in(c3_w* who_w)
{
return u3i_words(4, who_w);
}
/* _unix_ship_out(): ship to c3_w[4]. RETAIN.
*/
static void
_unix_ship_out(u3_noun own, c3_w* who_w)
{
u3r_words(0, 4, who_w, own);
{
u3_noun inn = _unix_ship_in(who_w);
c3_assert(_(u3r_sing(inn, own)));
u3z(inn);
}
}
/* _unix_ship_sing(): yes iff two ships are identical.
*/
static c3_o
_unix_ship_sing(c3_w* one_w, c3_w* two_w)
{
return __((one_w[0] == two_w[0]) &&
(one_w[1] == two_w[1]) &&
(one_w[2] == two_w[2]) &&
(one_w[3] == two_w[3]));
}
2013-09-29 00:21:18 +04:00
/* _unix_down(): descend path.
*/
static c3_c*
_unix_down(c3_c* pax_c, c3_c* sub_c)
{
c3_w pax_w = strlen(pax_c);
c3_w sub_w = strlen(sub_c);
2014-04-02 04:47:01 +04:00
c3_c* don_c = c3_malloc(pax_w + strlen(sub_c) + 2);
2013-09-29 00:21:18 +04:00
strncpy(don_c, pax_c, pax_w + 1);
2013-09-29 00:21:18 +04:00
don_c[pax_w] = '/';
strncpy(don_c + pax_w + 1, sub_c, sub_w + 1);
don_c[pax_w + sub_w + 1] = '\0';
2013-09-29 00:21:18 +04:00
return don_c;
}
/* _unix_mkdir(): mkdir, asserting.
2013-09-29 00:21:18 +04:00
*/
static void
_unix_mkdir(c3_c* pax_c)
2013-09-29 00:21:18 +04:00
{
if ( 0 != mkdir(pax_c, 0755) && EEXIST != errno) {
2015-03-05 03:36:29 +03:00
uL(fprintf(uH, "error mkdiring %s: %s\n", pax_c, strerror(errno)));
2013-09-29 00:21:18 +04:00
c3_assert(0);
}
}
2015-02-27 09:35:41 +03:00
/* _unix_mkpath(): mkdir -p, asserting
*
* adapted from
* http://niallohiggins.com/2009/01/08/mkpath-mkdir-p-alike-in-c-for-unix/
*/
static void
_unix_mkpath(const char *s)
{
char *q, *r = NULL, *path = NULL, *up = NULL;
if (strcmp(s, ".") == 0 || strcmp(s, "/") == 0)
return;
if ((path = strdup(s)) == NULL)
c3_assert(0);
if ((q = strdup(s)) == NULL)
c3_assert(0);
if ((r = dirname(q)) == NULL)
goto out;
if ((up = strdup(r)) == NULL)
exit(1);
_unix_mkpath(up);
_unix_mkdir(path);
out:
if (up != NULL)
free(up);
free(q);
free(path);
}
/* _unix_opendir(): opendir, recreating if nonexistent.
2013-09-29 00:21:18 +04:00
*/
static DIR*
_unix_opendir(c3_c* pax_c)
2013-09-29 00:21:18 +04:00
{
DIR* rid_u = opendir(pax_c);
if ( !rid_u ) {
// uL(fprintf(uH, "%s: %s\n", pax_c, strerror(errno)));
_unix_mkdir(pax_c);
rid_u = opendir(pax_c);
if ( !rid_u ) {
uL(fprintf(uH, "%s: %s\n", pax_c, strerror(errno)));
c3_assert(0);
}
2013-09-29 00:21:18 +04:00
}
return rid_u;
2013-09-29 00:21:18 +04:00
}
/* _unix_unlink(): unlink, asserting.
*/
static void
_unix_unlink(c3_c* pax_c)
{
2015-02-27 09:35:41 +03:00
if ( 0 != unlink(pax_c) && ENOENT != errno ) {
uL(fprintf(uH, "error unlinking %s: %s\n", pax_c, strerror(errno)));
2013-09-29 00:21:18 +04:00
c3_assert(0);
}
}
2014-09-11 04:01:32 +04:00
/* u3_unix_acquire(): acquire a lockfile, killing anything that holds it.
2013-09-29 00:21:18 +04:00
*/
void
2014-09-11 04:01:32 +04:00
u3_unix_acquire(c3_c* pax_c)
2013-09-29 00:21:18 +04:00
{
c3_c* paf_c = _unix_down(pax_c, ".vere.lock");
c3_w pid_w;
FILE* loq_u;
if ( NULL != (loq_u = fopen(paf_c, "r")) ) {
if ( 1 != fscanf(loq_u, "%" SCNu32, &pid_w) ) {
2013-09-29 00:21:18 +04:00
uL(fprintf(uH, "lockfile %s is corrupt!\n", paf_c));
kill(getpid(), SIGTERM);
sleep(1); c3_assert(0);
}
else {
c3_w i_w;
if ( -1 != kill(pid_w, SIGTERM) ) {
uL(fprintf(uH, "unix: stopping process %d, live in %s...\n",
2013-09-29 00:21:18 +04:00
pid_w, pax_c));
for ( i_w = 0; i_w < 16; i_w++ ) {
sleep(1);
if ( -1 == kill(pid_w, SIGTERM) ) {
break;
}
}
if ( 16 == i_w ) {
for ( i_w = 0; i_w < 16; i_w++ ) {
if ( -1 == kill(pid_w, SIGKILL) ) {
break;
}
sleep(1);
}
}
if ( 16 == i_w ) {
uL(fprintf(uH, "process %d seems unkillable!\n", pid_w));
c3_assert(0);
}
uL(fprintf(uH, "unix: stopped old process %u\n", pid_w));
}
}
fclose(loq_u);
unlink(paf_c);
}
loq_u = fopen(paf_c, "w");
fprintf(loq_u, "%u\n", getpid());
{
c3_i fid_i = fileno(loq_u);
#if defined(U3_OS_linux)
2013-09-29 00:21:18 +04:00
fdatasync(fid_i);
#elif defined(U3_OS_osx)
fcntl(fid_i, F_FULLFSYNC);
#elif defined(U3_OS_bsd)
fsync(fid_i);
#else
2014-05-17 00:44:40 +04:00
# error "port: datasync"
2013-09-29 00:21:18 +04:00
#endif
}
fclose(loq_u);
free(paf_c);
2013-09-29 00:21:18 +04:00
}
2014-09-11 04:01:32 +04:00
/* u3_unix_release(): release a lockfile.
2013-09-29 00:21:18 +04:00
*/
void
2014-09-11 04:01:32 +04:00
u3_unix_release(c3_c* pax_c)
2013-09-29 00:21:18 +04:00
{
c3_c* paf_c = _unix_down(pax_c, ".vere.lock");
unlink(paf_c);
free(paf_c);
}
/* _unix_dir_dry(): recursively dry a directory.
*/
static void
2014-09-11 04:01:32 +04:00
_unix_dir_dry(u3_udir* dir_u)
2013-09-29 00:21:18 +04:00
{
2014-09-11 04:01:32 +04:00
u3_udir* dis_u;
u3_ufil* fil_u;
2013-09-29 00:21:18 +04:00
2014-11-05 04:18:47 +03:00
dir_u->dry = c3y;
2013-09-29 00:21:18 +04:00
for ( dis_u = dir_u->dis_u; dis_u; dis_u = dis_u->nex_u ) {
_unix_dir_dry(dis_u);
}
for ( fil_u = dir_u->fil_u; fil_u; fil_u = fil_u->nex_u ) {
2014-11-05 04:18:47 +03:00
fil_u->dry = c3y;
2013-09-29 00:21:18 +04:00
}
}
/* _unix_fs_event_cb(): filesystem event callback.
*/
static void
_unix_fs_event_cb(uv_fs_event_t* was_u,
const c3_c* pax_c,
2014-07-03 22:43:53 +04:00
c3_i evt_i,
c3_i sas_i)
2013-09-29 00:21:18 +04:00
{
2014-08-23 01:33:27 +04:00
// note that we're doing something tricky and weird here.
//
// * libuv passes around a pointer to a uv_fs_event_t
// * we define a struct that STARTS with a uv_fs_event_t and then has
// more fields after it
// * this is what we pass into libuv up top
// * this is what we get out of libuv down below
// * thus a cast is cool
2014-09-11 04:01:32 +04:00
u3_unod* nod_u = (u3_unod*) was_u;
2013-09-29 00:21:18 +04:00
#ifdef SYNCLOG
2014-08-23 01:33:27 +04:00
2014-09-11 04:01:32 +04:00
c3_w slot = u3_Host.unx_u.lot_w++ % 1024;
free(u3_Host.unx_u.sylo[slot].pax_c);
u3_Host.unx_u.sylo[slot].pax_c = 0;
2014-11-05 04:18:47 +03:00
u3_Host.unx_u.sylo[slot].unx = c3y;
2014-09-11 04:01:32 +04:00
u3_Host.unx_u.sylo[slot].wer_m = c3_s4('u','v','s','y');
u3_Host.unx_u.sylo[slot].wot_m = 0;
u3_Host.unx_u.sylo[slot].pax_c = strdup(nod_u->pax_c);
#endif
2013-09-29 00:21:18 +04:00
{
while ( nod_u ) {
2014-11-05 04:18:47 +03:00
nod_u->dry = c3n;
2014-09-11 04:01:32 +04:00
nod_u = (u3_unod*) nod_u->par_u;
2013-09-29 00:21:18 +04:00
}
}
}
/* _unix_file_watch(): create file tracker (from filesystem)
*/
static void
2014-09-11 04:01:32 +04:00
_unix_file_watch(u3_ufil* fil_u,
u3_udir* dir_u,
2013-09-29 00:21:18 +04:00
c3_c* pax_c,
2015-02-27 09:35:41 +03:00
c3_c* pot_c,
2014-11-20 03:46:16 +03:00
c3_w* mod_w)
2013-09-29 00:21:18 +04:00
{
2014-08-23 01:33:27 +04:00
// (1) build data structure
//
2014-11-05 04:18:47 +03:00
fil_u->non = c3n;
fil_u->dry = c3n;
2013-09-29 00:21:18 +04:00
fil_u->pax_c = pax_c;
2015-02-27 09:35:41 +03:00
fil_u->pot_c = pot_c;
2013-09-29 00:21:18 +04:00
{
c3_c* dot_c = strrchr(pax_c, '.');
c3_c* fas_c = strrchr(pax_c, '/');
fil_u->dot_c = dot_c ? (fas_c ? ((dot_c > fas_c) ? dot_c : 0)
2014-08-23 01:33:27 +04:00
: dot_c)
: 0;
2013-09-29 00:21:18 +04:00
}
fil_u->par_u = dir_u;
2014-11-20 03:46:16 +03:00
fil_u->mod_w[0] = mod_w[0]; fil_u->mod_w[1] = mod_w[1];
fil_u->mod_w[2] = mod_w[2]; fil_u->mod_w[3] = mod_w[3];
2013-09-29 00:21:18 +04:00
fil_u->nex_u = 0;
c3_assert(!fil_u->dot_c || (fil_u->dot_c > fil_u->pax_c));
2014-08-23 01:33:27 +04:00
// (2) stuff data structure into libuv
//
2014-09-11 04:01:32 +04:00
c3_w ret_w = uv_fs_event_init(u3L, // loop
2015-02-27 09:35:41 +03:00
&fil_u->was_u // uv_fs_event_t
2014-08-23 01:33:27 +04:00
);
if (0 != ret_w){
uL(fprintf(uH, "event init: %s\n", strerror(ret_w)));
c3_assert(0);
}
//
ret_w = uv_fs_event_start(&fil_u->was_u, // uv_fs_event_t
_unix_fs_event_cb, // callback
pax_c, // dir as strings
0); // flags
if (0 != ret_w){
uL(fprintf(uH, "event start: %s\n", strerror(ret_w)));
c3_assert(0);
}
2013-09-29 00:21:18 +04:00
}
/* _unix_file_form(): form a filename path downward.
*/
static c3_c*
2015-02-27 09:35:41 +03:00
_unix_file_form(c3_c* pax_c,
2014-09-11 04:01:32 +04:00
u3_noun pre,
2014-11-06 06:10:22 +03:00
u3_noun ket,
2014-09-11 04:01:32 +04:00
u3_noun ext)
2013-09-29 00:21:18 +04:00
{
2014-11-06 03:20:01 +03:00
c3_c* pre_c = u3r_string(pre);
c3_c* ext_c = u3r_string(ext);
2015-02-27 09:35:41 +03:00
c3_w pax_w = strlen(pax_c);
2013-09-29 00:21:18 +04:00
c3_w pre_w = strlen(pre_c);
c3_w ext_w = strlen(ext_c);
2014-11-05 04:18:47 +03:00
c3_w ket_w = (c3y == ket) ? 1 : 0;
2015-02-27 09:35:41 +03:00
c3_c* pox_c = c3_malloc(pax_w + 1 + pre_w + 1 + ket_w + ext_w + 1);
2013-09-29 00:21:18 +04:00
2015-02-27 09:35:41 +03:00
strncpy(pox_c, pax_c, pax_w);
pox_c[pax_w] = '/';
strncpy(pox_c + pax_w + 1, pre_c, pre_w);
pox_c[pax_w + 1 + pre_w] = '.';
2014-11-05 04:18:47 +03:00
if ( c3y == ket ) {
2015-02-27 09:35:41 +03:00
pox_c[pax_w + 1 + pre_w + 1] = '^';
2014-05-20 22:53:49 +04:00
}
2015-02-27 09:35:41 +03:00
strncpy(pox_c + pax_w + 1 + pre_w + 1 + ket_w, ext_c, ext_w);
pox_c[pax_w + 1 + pre_w + 1 + ket_w + ext_w] = '\0';
2013-09-29 00:21:18 +04:00
free(pre_c); free(ext_c);
2014-09-11 04:01:32 +04:00
u3z(pre); u3z(ext);
2013-09-29 00:21:18 +04:00
2015-02-27 09:35:41 +03:00
return pox_c;
2013-09-29 00:21:18 +04:00
}
/* _unix_dir_watch(): instantiate directory tracker.
*/
static void
2015-02-27 09:35:41 +03:00
_unix_dir_watch(u3_udir* dir_u, u3_udir* par_u, c3_c* pax_c, c3_c* pot_c)
2013-09-29 00:21:18 +04:00
{
2014-08-23 01:33:27 +04:00
// (1) build data structure
//
2014-11-05 04:18:47 +03:00
dir_u->yes = c3y;
dir_u->dry = c3n;
2014-08-23 01:33:27 +04:00
dir_u->pax_c = pax_c;
2015-02-27 09:35:41 +03:00
dir_u->pot_c = pot_c;
2014-08-23 01:33:27 +04:00
dir_u->par_u = par_u;
dir_u->dis_u = 0;
dir_u->fil_u = 0;
dir_u->nex_u = 0;
// (2) stuff data structure into libuv
//
2014-09-11 04:01:32 +04:00
c3_w ret_w = uv_fs_event_init(u3L, &dir_u->was_u );
2014-08-21 02:09:51 +04:00
if (0 != ret_w){
uL(fprintf(uH, "event init: %s\n", uv_strerror(ret_w)));
c3_assert(0);
}
2014-08-23 01:33:27 +04:00
// note that we're doing something tricky here; see comment in _unix_fs_event_cb
//
ret_w = uv_fs_event_start(&dir_u->was_u,
2014-08-21 02:09:51 +04:00
_unix_fs_event_cb,
pax_c,
0);
if (0 != ret_w){
uL(fprintf(uH, "event start: %s\n", uv_strerror(ret_w)));
c3_assert(0);
}
2013-09-29 00:21:18 +04:00
}
/* _unix_dir_forge: instantiate directory tracker (and make directory).
*/
static void
2014-09-11 04:01:32 +04:00
_unix_dir_forge(u3_udir* dir_u, u3_udir* par_u, u3_noun tet)
2013-09-29 00:21:18 +04:00
{
2014-11-06 03:20:01 +03:00
c3_c* tet_c = u3r_string(tet);
2014-07-03 22:43:53 +04:00
c3_w pax_w = strlen(par_u->pax_c);
c3_w tet_w = strlen(tet_c);
c3_c* pax_c = c3_malloc(pax_w + 1 + tet_w + 1);
2015-02-27 09:35:41 +03:00
c3_c* pot_c = c3_malloc(pax_w + 1 + 1 + tet_w + 1);
2013-09-29 00:21:18 +04:00
2014-07-03 22:43:53 +04:00
strncpy(pax_c, par_u->pax_c, pax_w + 1);
pax_c[pax_w] = '/';
strncpy(pax_c + pax_w + 1, tet_c, tet_w + 1);
pax_c[pax_w + tet_w + 1] = '\0';
2013-09-29 00:21:18 +04:00
2015-02-27 09:35:41 +03:00
strncpy(pot_c, par_u->pot_c, pax_w + 1 + 1);
pot_c[pax_w + 1] = '/';
strncpy(pot_c + pax_w + 1 + 1, tet_c, tet_w + 1);
pot_c[pax_w + 1 + tet_w + 1] = '\0';
2014-07-03 22:43:53 +04:00
free(tet_c);
2014-09-11 04:01:32 +04:00
u3z(tet);
2013-09-29 00:21:18 +04:00
2014-07-03 22:43:53 +04:00
_unix_mkdir(pax_c);
2015-02-27 09:35:41 +03:00
_unix_mkdir(pot_c);
_unix_dir_watch(dir_u, par_u, pax_c, pot_c);
2013-09-29 00:21:18 +04:00
}
/* _unix_file_done(): finish freeing file.
*/
static void
_unix_file_done(uv_handle_t* was_u)
{
2014-09-11 04:01:32 +04:00
u3_ufil* fil_u = (void*) was_u;
2013-09-29 00:21:18 +04:00
// uL(fprintf(uH, "file: dun: %s\n", fil_u->pax_c));
2013-09-29 00:21:18 +04:00
free(fil_u->pax_c);
free(fil_u);
}
/* _unix_file_free(): free (within) file tracker.
*/
static void
2014-09-11 04:01:32 +04:00
_unix_file_free(u3_ufil* fil_u)
2013-09-29 00:21:18 +04:00
{
// uL(fprintf(uH, "file: free: %s\n", fil_u->pax_c));
2013-09-29 00:21:18 +04:00
uv_close((uv_handle_t*)&fil_u->was_u, _unix_file_done);
}
#if 0
/* _unix_file_sane(): sanity check file.
*/
static void
2014-09-11 04:01:32 +04:00
_unix_file_sane(u3_ufil* fil_u)
2013-09-29 00:21:18 +04:00
{
}
/* _unix_dir_sane(): sanity check directory.
*/
static void
2014-09-11 04:01:32 +04:00
_unix_dir_sane(u3_udir* dir_u)
2013-09-29 00:21:18 +04:00
{
2014-09-11 04:01:32 +04:00
u3_udir* dis_u;
u3_ufil* fil_u;
2013-09-29 00:21:18 +04:00
for ( dis_u = dir_u->dis_u; dis_u; dis_u = dis_u->nex_u ) {
_unix_dir_sane(dis_u);
}
for ( fil_u = dir_u->fil_u; fil_u; fil_u = fil_u->nex_u ) {
_unix_file_sane(fil_u);
}
}
#endif
2014-03-03 02:31:03 +04:00
/* _unix_dir_free(): free (within) directory tracker.
2013-09-29 00:21:18 +04:00
*/
2014-09-11 04:01:32 +04:00
static void _unix_dir_free(u3_udir* dir_u);
2013-09-29 00:21:18 +04:00
static void
_unix_dir_done(uv_handle_t* was_u)
{
2014-09-11 04:01:32 +04:00
u3_udir* dir_u = (void*) was_u;
2013-09-29 00:21:18 +04:00
while ( dir_u->dis_u ) {
2014-09-11 04:01:32 +04:00
u3_udir* nex_u = dir_u->dis_u->nex_u;
2013-09-29 00:21:18 +04:00
_unix_dir_free(dir_u->dis_u);
dir_u->dis_u = nex_u;
}
while ( dir_u->fil_u ) {
2014-09-11 04:01:32 +04:00
u3_ufil* nex_u = dir_u->fil_u->nex_u;
2013-09-29 00:21:18 +04:00
_unix_file_free(dir_u->fil_u);
dir_u->fil_u = nex_u;
}
2014-03-03 02:31:03 +04:00
free(dir_u->pax_c);
free(dir_u);
}
static void
2014-09-11 04:01:32 +04:00
_unix_dir_free(u3_udir* dir_u)
2014-03-03 02:31:03 +04:00
{
uv_close((uv_handle_t*)&dir_u->was_u, _unix_dir_done);
2013-09-29 00:21:18 +04:00
}
/* _unix_dir_update(): update directory.
*/
static void
2014-09-11 04:01:32 +04:00
_unix_dir_update(u3_udir* dir_u, DIR* rid_u)
2013-09-29 00:21:18 +04:00
{
2014-11-05 04:18:47 +03:00
if ( c3y == dir_u->dry ) {
2013-09-29 00:21:18 +04:00
return;
}
2013-09-29 00:21:18 +04:00
else {
// Update all wet subdirectories.
//
2014-09-11 04:01:32 +04:00
u3_udir** dis_u;
u3_ufil** fil_u;
2013-09-29 00:21:18 +04:00
for ( dis_u = &(dir_u->dis_u); *dis_u; ) {
2014-11-05 04:18:47 +03:00
if ( c3y == (*dis_u)->dry ) {
2013-09-29 00:21:18 +04:00
dis_u = &(*dis_u)->nex_u;
}
else {
DIR* red_u = opendir((*dis_u)->pax_c);
if ( 0 == red_u ) {
2014-09-11 04:01:32 +04:00
u3_udir* ded_u = *dis_u;
u3_udir* nex_u = ded_u->nex_u;
2013-09-29 00:21:18 +04:00
// uL(fprintf(uH, "removed directory %s\n", ded_u->pax_c));
_unix_dir_free(ded_u);
*dis_u = nex_u;
}
else {
_unix_dir_update(*dis_u, red_u);
closedir(red_u);
dis_u = &(*dis_u)->nex_u;
}
}
}
// Check all wet files to see if they need deleting.
//
for ( fil_u = &(dir_u->fil_u); *fil_u; ) {
2014-11-05 04:18:47 +03:00
if ( c3y == (*fil_u)->dry ) {
2013-09-29 00:21:18 +04:00
fil_u = &(*fil_u)->nex_u;
}
else {
struct stat buf_u;
if ( -1 == stat((*fil_u)->pax_c, &buf_u) ||
2013-09-29 00:21:18 +04:00
!(S_IFREG & buf_u.st_mode) )
{
2014-09-11 04:01:32 +04:00
u3_ufil* ded_u = *fil_u;
u3_ufil* nex_u = ded_u->nex_u;
2013-09-29 00:21:18 +04:00
// uL(fprintf(uH, "removed file %s\n", ded_u->pax_c));
2013-09-29 00:21:18 +04:00
_unix_file_free(ded_u);
*fil_u = nex_u;
}
else {
fil_u = &(*fil_u)->nex_u;
}
}
}
// Scan for new files/directories. XX - this is O(n^2) brute
// force, and could be done by smarter event processing.
//
while ( 1 ) {
struct dirent ent_u;
struct dirent* out_u;
if ( readdir_r(rid_u, &ent_u, &out_u) != 0 ) {
// uL(fprintf(uH, "%s: %s\n", dir_u->pax_c, strerror(errno)));
c3_assert(0);
}
2013-09-29 00:21:18 +04:00
else if ( !out_u ) {
break;
}
else if ( ('.' == out_u->d_name[0]) ) { // XX screws up some paths
continue;
}
else {
c3_c* pax_c = _unix_down(dir_u->pax_c, out_u->d_name);
2015-02-27 09:35:41 +03:00
c3_c* pot_c = _unix_down(dir_u->pot_c, out_u->d_name);
2013-09-29 00:21:18 +04:00
struct stat buf_u;
2013-09-29 00:21:18 +04:00
// uL(fprintf(uH, " in %s\n", pax_c));
if ( 0 != stat(pax_c, &buf_u) ) {
free(pax_c);
2015-02-27 09:35:41 +03:00
free(pot_c);
2013-09-29 00:21:18 +04:00
continue;
}
2013-09-29 00:21:18 +04:00
else {
if ( !S_ISDIR(buf_u.st_mode) ) {
2014-11-20 03:46:16 +03:00
c3_w mod_w[4];
2014-09-11 04:01:32 +04:00
u3_ufil* fil_u;
2013-09-29 00:21:18 +04:00
2014-03-25 21:39:53 +04:00
if ( ( NULL == strrchr(out_u->d_name, '.')) ||
( '~' == out_u->d_name[strlen(out_u->d_name) - 1] )
) {
2015-05-06 02:42:00 +03:00
free(pax_c);
free(pot_c);
2014-03-19 04:04:44 +04:00
continue;
}
2013-09-29 00:21:18 +04:00
{
2014-09-11 04:01:32 +04:00
u3_noun mod = c3_stat_mtime(&buf_u);
2015-02-27 09:35:41 +03:00
2014-11-20 03:46:16 +03:00
u3r_words(0, 4, mod_w, mod);
2014-09-11 04:01:32 +04:00
u3z(mod);
2013-09-29 00:21:18 +04:00
}
for ( fil_u = dir_u->fil_u; fil_u; fil_u = fil_u->nex_u ) {
if ( !strcmp(pax_c, fil_u->pax_c) ) {
break;
}
}
if ( !fil_u ) {
2014-09-11 04:01:32 +04:00
fil_u = c3_malloc(sizeof(u3_ufil));
2013-09-29 00:21:18 +04:00
// uL(fprintf(uH, "found file %s\n", pax_c));
2015-02-27 09:35:41 +03:00
_unix_file_watch(fil_u, dir_u, pax_c, pot_c, mod_w);
2013-09-29 00:21:18 +04:00
fil_u->nex_u = dir_u->fil_u;
dir_u->fil_u = fil_u;
}
}
else {
2014-09-11 04:01:32 +04:00
u3_udir* dis_u;
2013-09-29 00:21:18 +04:00
for ( dis_u = dir_u->dis_u; dis_u; dis_u = dis_u->nex_u ) {
if ( !strcmp(pax_c, dis_u->pax_c) ) {
break;
}
}
if ( !dis_u ) {
DIR* red_u = _unix_opendir(pax_c);
2014-09-11 04:01:32 +04:00
dis_u = c3_malloc(sizeof(u3_udir));
2013-09-29 00:21:18 +04:00
// uL(fprintf(uH, "found directory %s\n", pax_c));
2015-02-27 09:35:41 +03:00
_unix_dir_watch(dis_u, dir_u, pax_c, pot_c);
2013-09-29 00:21:18 +04:00
_unix_dir_update(dis_u, red_u);
dis_u->nex_u = dir_u->dis_u;
dir_u->dis_u = dis_u;
closedir(red_u);
} else {
free(pax_c);
2015-02-27 09:35:41 +03:00
free(pot_c);
2013-09-29 00:21:18 +04:00
}
}
}
}
}
}
}
2015-02-27 09:35:41 +03:00
/* unix_load(): load a file as a cage
*
2015-05-07 05:25:41 +03:00
* return value of u3_none means delete, u3_nul means no change
2013-09-29 00:21:18 +04:00
*/
2015-05-07 05:25:41 +03:00
static u3_weak
2015-02-27 09:35:41 +03:00
_unix_load(u3_ufil* fil_u)
2013-09-29 00:21:18 +04:00
{
struct stat buf_u;
2015-02-27 09:35:41 +03:00
c3_i fid_i = open(fil_u->pax_c, O_RDONLY, 0644);
2013-09-29 00:21:18 +04:00
c3_w fln_w, red_w;
c3_y* pad_y;
2015-02-27 09:35:41 +03:00
c3_i fit_i = open(fil_u->pot_c, O_RDONLY, 0644);
c3_w flt_w, ret_w;
c3_y* pat_y;
2013-09-29 00:21:18 +04:00
if ( (fid_i < 0) || (fstat(fid_i, &buf_u) < 0) ) {
2014-03-25 21:26:46 +04:00
// ignore if the file disappeared between getting the sync event and now
if ( ENOENT != errno ) {
2015-02-27 09:35:41 +03:00
uL(fprintf(uH, "error loading %s: %s\n", fil_u->pax_c, strerror(errno)));
2014-03-25 21:26:46 +04:00
}
2015-05-07 05:25:41 +03:00
return u3_none;
2013-09-29 00:21:18 +04:00
}
fln_w = buf_u.st_size;
2015-02-27 09:35:41 +03:00
pad_y = c3_malloc(fln_w);
2013-09-29 00:21:18 +04:00
red_w = read(fid_i, pad_y, fln_w);
close(fid_i);
if ( fln_w != red_w ) {
free(pad_y);
c3_assert(0);
return 0;
}
else {
2015-02-27 09:35:41 +03:00
if ( !(fit_i < 0 || fstat(fit_i, &buf_u) < 0) ) {
flt_w = buf_u.st_size;
pat_y = c3_malloc(flt_w);
ret_w = read(fit_i, pat_y, flt_w);
close(fit_i);
if ( flt_w != ret_w ) {
free(pad_y);
c3_assert(0);
return 0;
}
if ( fln_w == flt_w && 0 == memcmp(pad_y, pat_y, fln_w) ) {
2015-03-26 01:35:47 +03:00
free(pad_y);
2015-02-27 09:35:41 +03:00
free(pat_y);
return u3_nul;
}
free(pat_y);
}
// -:!>(*[[@tas @tas ~] @ud @])
u3_noun typ = u3nt(c3__cell,
u3nt(c3__cell,
u3nc(c3__atom, c3__tas),
u3nt(c3__cell,
u3nc(c3__atom, c3__tas),
u3nt(c3__cube,
u3_nul,
u3nc(c3__atom, 'n')))),
u3nt(c3__cell,
u3nc(c3__atom, c3__ud),
u3nc(c3__atom, u3_nul)));
u3_noun nam = u3nt(c3__text, u3i_string("plain"), u3_nul);
u3_noun pad = u3nt(nam, fln_w, u3i_bytes(fln_w, (c3_y *)pad_y));
u3_noun cay = u3nt(c3__mime, typ, pad);
2013-09-29 00:21:18 +04:00
free(pad_y);
2015-02-27 09:35:41 +03:00
return cay;
2013-09-29 00:21:18 +04:00
}
}
/* unix_save(): save a file.
*/
static void
2014-09-11 04:01:32 +04:00
_unix_save(c3_c* pax_c, u3_atom oat)
2013-09-29 00:21:18 +04:00
{
2015-02-27 09:35:41 +03:00
c3_c* dir = strdup(pax_c);
_unix_mkpath(dirname(dir));
free(dir);
2013-09-29 00:21:18 +04:00
c3_i fid_i = open(pax_c, O_WRONLY | O_CREAT | O_TRUNC, 0666);
2015-02-27 09:35:41 +03:00
c3_w fln_w, rit_w, siz_w;
2013-09-29 00:21:18 +04:00
c3_y* oat_y;
if ( fid_i < 0 ) {
2015-02-27 09:35:41 +03:00
uL(fprintf(uH, "error opening %s: %s\n", pax_c, strerror(errno)));
2014-11-06 03:20:01 +03:00
u3m_bail(c3__fail);
2013-09-29 00:21:18 +04:00
}
2015-02-27 09:35:41 +03:00
siz_w = u3h(u3t(oat));
fln_w = u3r_met(3, u3t(u3t(oat)));
oat_y = c3_malloc(siz_w);
memset(oat_y, 0, siz_w);
u3r_bytes(0, fln_w, oat_y, u3t(u3t(oat)));
2014-09-11 04:01:32 +04:00
u3z(oat);
2013-09-29 00:21:18 +04:00
2015-02-27 09:35:41 +03:00
rit_w = write(fid_i, oat_y, siz_w);
if ( rit_w != siz_w ) {
uL(fprintf(uH, "error writing %s: %s\n", pax_c, strerror(errno)));
2013-09-29 00:21:18 +04:00
c3_assert(0);
}
2014-05-17 00:44:40 +04:00
close(fid_i);
free(oat_y);
2013-09-29 00:21:18 +04:00
}
/* _unix_file_load(): load a file by watcher.
*/
2015-05-07 05:25:41 +03:00
static u3_weak
2014-09-11 04:01:32 +04:00
_unix_file_load(u3_ufil* fil_u)
2013-09-29 00:21:18 +04:00
{
2015-02-27 09:35:41 +03:00
return _unix_load(fil_u);
2013-09-29 00:21:18 +04:00
}
/* _unix_dir_name(): directory name.
*/
2014-09-11 04:01:32 +04:00
static u3_noun
_unix_dir_name(u3_udir* dir_u)
2013-09-29 00:21:18 +04:00
{
c3_w pel_w = strlen(dir_u->par_u->pax_c);
c3_c* pax_c = dir_u->pax_c + pel_w + 1;
c3_c* fas_c = strchr(pax_c, '/');
2014-11-06 03:20:01 +03:00
return fas_c ? u3i_bytes((fas_c - pax_c), (c3_y*) pax_c)
: u3i_string(pax_c);
2013-09-29 00:21:18 +04:00
}
2014-05-17 00:44:40 +04:00
/* _unix_file_tame(): file name/extension for toplevel.
*/
2014-09-11 04:01:32 +04:00
static u3_noun
_unix_file_tame(u3_ufil* fil_u)
2014-05-17 00:44:40 +04:00
{
c3_c* fas_c = strrchr(fil_u->pax_c, '/');
c3_c* pax_c = fil_u->pax_c + (fas_c-fil_u->pax_c) + 1;
if ( !fil_u->dot_c ) {
2014-11-06 02:36:30 +03:00
return u3_none;
2014-05-17 00:44:40 +04:00
}
else {
c3_c* ext_c = fil_u->dot_c + 1;
c3_w nam_w = fil_u->dot_c - pax_c;
2014-11-06 03:20:01 +03:00
return u3nc(u3i_bytes(nam_w, (c3_y*)pax_c),
u3i_string(ext_c));
2014-05-17 00:44:40 +04:00
}
}
2013-09-29 00:21:18 +04:00
/* _unix_file_name(): file name/extension.
*/
2014-09-11 04:01:32 +04:00
static u3_noun
_unix_file_name(u3_ufil* fil_u)
2013-09-29 00:21:18 +04:00
{
c3_w pel_w = strlen(fil_u->par_u->pax_c);
c3_c* pax_c = fil_u->pax_c + pel_w + 1;
if ( !fil_u->dot_c ) {
2014-11-06 03:20:01 +03:00
return u3i_string(pax_c);
}
2013-09-29 00:21:18 +04:00
else {
c3_c* ext_c = fil_u->dot_c + 1;
2014-05-20 22:53:49 +04:00
ext_c = (*ext_c == '^') ? (ext_c + 1) : ext_c;
2014-11-06 03:20:01 +03:00
return u3nc(u3i_bytes((fil_u->dot_c - pax_c), (c3_y*)pax_c),
u3i_string(ext_c));
2013-09-29 00:21:18 +04:00
}
}
2015-02-27 09:35:41 +03:00
/* _unix_dir_khan_file(): process a file for khan.
2014-05-17 00:44:40 +04:00
*/
2014-09-11 04:01:32 +04:00
static u3_noun
2015-05-06 02:42:00 +03:00
_unix_dir_khan_file(u3_noun pam, u3_noun wib, u3_noun woz)
2014-05-17 00:44:40 +04:00
{
2014-09-11 04:01:32 +04:00
u3_weak ole;
2014-11-05 04:18:47 +03:00
if ( c3n == u3du(wib) ) {
2014-11-06 22:13:57 +03:00
ole = u3kdb_get(u3k(pam), u3k(wib));
2014-05-17 00:44:40 +04:00
2014-11-06 02:36:30 +03:00
if ( u3_none == ole ) {
2015-02-27 09:35:41 +03:00
ole = u3nc(woz, u3_nul);
2014-05-17 00:44:40 +04:00
} else {
2014-09-11 04:01:32 +04:00
u3_noun elo;
2014-05-17 00:44:40 +04:00
2015-02-27 09:35:41 +03:00
elo = u3nc(woz, u3k(u3t(ole)));
2014-09-11 04:01:32 +04:00
u3z(ole);
2014-05-17 00:44:40 +04:00
ole = elo;
}
2014-11-06 22:13:57 +03:00
pam = u3kdb_put(pam, wib, ole);
2014-05-17 00:44:40 +04:00
}
else {
2014-09-11 04:01:32 +04:00
u3_noun fid = u3h(wib);
u3_noun har = u3t(wib);
2014-05-17 00:44:40 +04:00
2014-11-06 22:13:57 +03:00
ole = u3kdb_get(u3k(pam), u3k(fid));
2014-05-17 00:44:40 +04:00
2014-11-06 02:36:30 +03:00
if ( u3_none == ole ) {
2015-02-27 09:35:41 +03:00
ole = u3nc(u3_nul,
u3kdb_put(u3_nul,
u3k(har),
u3nc(woz, u3_nul)));
2014-05-17 00:44:40 +04:00
}
else {
2015-02-27 09:35:41 +03:00
u3_noun roo = u3t(ole);
2014-11-06 22:13:57 +03:00
u3_weak tup = u3kdb_get(u3k(roo), u3k(har));
2014-09-11 04:01:32 +04:00
u3_noun oor, elo;
2014-05-17 00:44:40 +04:00
2014-11-06 02:36:30 +03:00
if ( u3_none == tup ) {
2015-02-27 09:35:41 +03:00
tup = u3nc(woz, u3_nul);
2014-05-17 00:44:40 +04:00
} else {
2014-09-11 04:01:32 +04:00
u3_noun upt;
2014-05-17 00:44:40 +04:00
2015-02-27 09:35:41 +03:00
upt = u3nc(woz, u3k(u3t(tup)));
2014-09-11 04:01:32 +04:00
u3z(tup);
2014-05-17 00:44:40 +04:00
tup = upt;
}
2014-11-06 22:13:57 +03:00
oor = u3kdb_put(u3k(roo), u3k(har), tup);
2015-02-27 09:35:41 +03:00
elo = u3nc(u3k(u3h(ole)), oor);
2014-05-17 00:44:40 +04:00
2014-09-11 04:01:32 +04:00
u3z(ole); ole = elo;
2014-05-17 00:44:40 +04:00
}
2014-11-06 22:13:57 +03:00
pam = u3kdb_put(pam, u3k(fid), ole);
2014-09-11 04:01:32 +04:00
u3z(wib);
2014-05-17 00:44:40 +04:00
}
return pam;
}
2015-02-27 09:35:41 +03:00
/* _unix_dir_khan(): resolve directory to khan.
2013-09-29 00:21:18 +04:00
*/
2014-09-11 04:01:32 +04:00
static u3_noun
2015-02-27 09:35:41 +03:00
_unix_dir_khan(u3_udir* dir_u)
2013-09-29 00:21:18 +04:00
{
2014-09-11 04:01:32 +04:00
u3_udir* dis_u;
u3_ufil* fil_u;
u3_noun pam = u3_nul;
2013-09-29 00:21:18 +04:00
for ( dis_u = dir_u->dis_u; dis_u; dis_u = dis_u->nex_u ) {
2014-09-11 04:01:32 +04:00
u3_noun pre = _unix_dir_name(dis_u);
2015-02-27 09:35:41 +03:00
u3_noun kan = _unix_dir_khan(dis_u);
2013-09-29 00:21:18 +04:00
2014-11-06 03:20:01 +03:00
// uL(fprintf(uH, "dir %s\n", u3r_string(pre)));
2015-02-27 09:35:41 +03:00
if ( u3_nul != u3h(kan) || u3_nul != u3t(kan) ) {
pam = u3kdb_put(pam, pre, kan);
}
2015-05-06 02:42:00 +03:00
else {
u3z(kan); u3z(pre);
2014-12-09 01:46:46 +03:00
}
2013-09-29 00:21:18 +04:00
}
2014-05-17 00:44:40 +04:00
if ( !dir_u->par_u->par_u ) { // toplevel
for ( fil_u = dir_u->par_u->fil_u; fil_u; fil_u = fil_u->nex_u ) {
// uL(fprintf(uH, "top %s\n", fil_u->pax_c));
2014-09-11 04:01:32 +04:00
u3_noun wib = _unix_file_tame(fil_u);
2014-11-06 02:36:30 +03:00
if ( u3_none == wib ) continue;
2014-09-11 04:01:32 +04:00
u3_noun dur = _unix_dir_name(dir_u);
2014-11-06 03:20:01 +03:00
if ( c3n == u3r_sing(dur, u3h(wib)) ) { // wrong desk
2015-05-06 02:42:00 +03:00
u3z(dur); u3z(wib);
2014-05-17 00:44:40 +04:00
continue;
}
2015-05-06 02:42:00 +03:00
u3_noun woz = u3nt(u3_nul, u3_nul, _unix_file_load(fil_u));
pam = _unix_dir_khan_file(pam, u3k(u3t(wib)), woz);
2014-09-11 04:01:32 +04:00
u3z(dur);
u3z(wib);
2014-05-17 00:44:40 +04:00
}
}
2013-09-29 00:21:18 +04:00
for ( fil_u = dir_u->fil_u; fil_u; fil_u = fil_u->nex_u ) {
2014-09-11 04:01:32 +04:00
u3_noun wib = _unix_file_name(fil_u);
2015-05-07 05:25:41 +03:00
u3_weak baw = _unix_file_load(fil_u);
u3_noun wol = (u3_none == baw ? u3nc(u3_nul, u3_nul) :
2015-02-27 09:35:41 +03:00
u3_nul == baw ? u3_nul :
u3nt(u3_nul, u3_nul, baw));
2015-05-06 02:42:00 +03:00
pam = _unix_dir_khan_file(pam, wib, wol);
2013-09-29 00:21:18 +04:00
}
2015-02-27 09:35:41 +03:00
return u3nc(u3_nul, pam);
2013-09-29 00:21:18 +04:00
}
2014-10-28 20:36:22 +03:00
/* _find_mug(): find a noun with a given mug. retain. DELETEME
*/
#if 0
static u3_weak
_find_mug(u3_noun som, c3_l mug_l)
{
2014-11-06 03:20:01 +03:00
if ( mug_l == u3r_mug(som) ) {
2014-10-28 20:36:22 +03:00
return som;
}
else {
u3_weak pro;
2014-11-05 04:18:47 +03:00
if ( _(u3du(som)) ) {
2014-11-06 02:36:30 +03:00
if ( (pro = _find_mug(u3h(som), mug_l)) != u3_none ) {
2014-10-28 20:36:22 +03:00
return pro;
}
else return _find_mug(u3t(som), mug_l);
}
2014-11-06 02:36:30 +03:00
else return u3_none;
2014-10-28 20:36:22 +03:00
}
}
#endif
2015-02-27 09:35:41 +03:00
#if 0
2013-09-29 00:21:18 +04:00
/* _unix_desk_peek(): peek for ankh.
*/
2014-09-11 04:01:32 +04:00
static u3_noun
_unix_desk_peek(u3_noun hox,
u3_noun syd,
u3_noun lok)
2013-09-29 00:21:18 +04:00
{
2014-10-28 20:36:22 +03:00
u3_noun arg;
2014-09-11 04:01:32 +04:00
u3_noun cay;
2013-09-29 00:21:18 +04:00
2014-10-28 20:36:22 +03:00
arg = u3nc(c3_s2('c','z'), u3nq(hox, syd, lok, u3_nul));
2014-11-06 03:20:01 +03:00
cay = u3v_peek(arg);
2013-09-29 00:21:18 +04:00
2014-09-11 04:01:32 +04:00
if ( u3_nul == cay ) {
return u3nt(0, u3_nul, u3_nul);
2014-10-28 20:36:22 +03:00
}
else {
2014-09-11 04:01:32 +04:00
u3_noun ank = u3k(u3t(cay));
2013-09-29 00:21:18 +04:00
2014-09-11 04:01:32 +04:00
u3z(cay); return ank;
2013-09-29 00:21:18 +04:00
}
}
2015-02-27 09:35:41 +03:00
#endif
2013-09-29 00:21:18 +04:00
2014-08-17 07:57:54 +04:00
#if 0
2014-09-11 04:01:32 +04:00
/* _unix_ankh_sing_map(): compare ankh maps for u3_ankh_sing().
*/
2014-11-06 06:10:22 +03:00
static u3_noun _unix_ankh_sing_in(u3_noun, u3_noun);
2014-11-06 06:10:22 +03:00
static u3_noun
2014-09-11 04:01:32 +04:00
_unix_ankh_sing_map(u3_noun mun, u3_noun mur) // retain
{
2014-09-11 04:01:32 +04:00
u3_noun n_mun, l_mun, r_mun;
u3_noun n_mur, l_mur, r_mur;
2014-11-05 04:18:47 +03:00
if ( (u3_nul == mun) && (u3_nul == mur) ) { return c3y; }
if ( (u3_nul == mun) || (u3_nul == mur) ) { return c3n; }
2014-11-06 03:20:01 +03:00
u3x_trel(mun, &n_mun, &l_mun, &r_mun);
u3x_trel(mur, &n_mur, &l_mur, &r_mur);
2014-11-06 03:20:01 +03:00
if ( (c3n == (u3r_sing(u3h(n_mun), u3h(n_mur)))) ||
2014-11-05 04:18:47 +03:00
(c3n == _unix_ankh_sing_in(u3t(n_mun), u3t(n_mur))) ||
(c3n == _unix_ankh_sing_map(l_mun, l_mur)) ||
(c3n == _unix_ankh_sing_map(r_mun, r_mur)) )
{
2014-11-05 04:18:47 +03:00
return c3n;
} else return c3y;
}
/* _unix_node_sing(): test node equality.
*/
2014-11-06 06:10:22 +03:00
static u3_noun
2014-09-11 04:01:32 +04:00
_unix_node_sing(u3_noun xud, u3_noun bud)
{
2014-11-05 04:18:47 +03:00
if ( (u3_nul == xud) && (u3_nul == bud) ) { return c3y; }
if ( (u3_nul == xud) || (u3_nul == bud) ) { return c3n; }
2014-11-06 03:20:01 +03:00
return u3r_sing(u3t(u3t(xud)), u3t(u3t(bud)));
}
/* _unix_ankh_sing_in(): stupid ankh test which ignores broken hash.
*/
2014-11-06 06:10:22 +03:00
static u3_noun
2014-09-11 04:01:32 +04:00
_unix_ankh_sing_in(u3_noun xun, u3_noun bur) // retain
{
2014-09-11 04:01:32 +04:00
u3_noun p_xun, q_xun, r_xun;
u3_noun p_bur, q_bur, r_bur;
2014-11-06 03:20:01 +03:00
u3x_trel(xun, &p_xun, &q_xun, &r_xun);
u3x_trel(bur, &p_bur, &q_bur, &r_bur);
2014-11-05 04:18:47 +03:00
if ( c3n == _unix_node_sing(q_xun, q_bur) ) {
return c3n;
}
return _unix_ankh_sing_map(r_xun, r_bur);
}
/* _unix_ankh_sing(): full ankh compare.
*/
2014-11-06 06:10:22 +03:00
static u3_noun
2014-09-11 04:01:32 +04:00
_unix_ankh_sing(u3_noun xun, u3_noun bur) // retain
{
2014-11-06 03:20:01 +03:00
if ( c3y == u3r_sing(xun, bur) ) {
2014-11-05 04:18:47 +03:00
return c3y;
} else {
2014-11-05 04:18:47 +03:00
if ( c3n == _unix_ankh_sing_in(xun, bur) ) {
2014-09-11 04:01:32 +04:00
// fprintf(stderr, "uas: no, no (%x, %x)\r\n", u3_mug(xun), u3_mug(bur));
2014-11-05 04:18:47 +03:00
return c3n;
}
else {
// fprintf(stderr, "uas: no, yes\r\n");
2014-11-05 04:18:47 +03:00
return c3y;
}
}
}
2014-08-17 07:57:54 +04:00
#endif
2013-09-29 00:21:18 +04:00
/* _unix_desk_sync_into(): sync external changes to desk.
*/
static void
2014-09-11 04:01:32 +04:00
_unix_desk_sync_into(u3_noun who,
u3_noun hox,
u3_noun syd,
u3_udir* dir_u)
2013-09-29 00:21:18 +04:00
{
2015-02-27 09:35:41 +03:00
u3_noun xun, fav, pax;
2013-09-29 00:21:18 +04:00
2015-02-27 09:35:41 +03:00
xun = _unix_dir_khan(dir_u);
2013-09-29 00:21:18 +04:00
2015-02-27 09:35:41 +03:00
pax = u3nq(u3_blip, c3__sync, u3k(u3A->sen), u3_nul);
fav = u3nq(c3__into, who, syd, xun);
2013-09-29 00:21:18 +04:00
2015-02-27 09:35:41 +03:00
u3v_plan(pax, fav);
2015-05-06 02:42:00 +03:00
u3z(hox);
2013-09-29 00:21:18 +04:00
}
/* _unix_ship_update(): update top level ship.
*/
static void
2014-09-11 04:01:32 +04:00
_unix_ship_update(u3_uhot* hot_u)
2013-09-29 00:21:18 +04:00
{
2014-09-11 04:01:32 +04:00
u3_udir* dir_u = &(hot_u->dir_u);
2013-09-29 00:21:18 +04:00
2014-11-05 04:18:47 +03:00
if ( c3n == dir_u->dry ) {
2013-09-29 00:21:18 +04:00
DIR* rid_u = _unix_opendir(dir_u->pax_c);
2014-09-11 04:01:32 +04:00
u3_udir* dis_u;
2014-11-20 03:46:16 +03:00
u3_noun who = _unix_ship_in(hot_u->who_w);
u3_noun hox = u3dc("scot", 'p', u3k(who));
2013-09-29 00:21:18 +04:00
_unix_dir_update(dir_u, rid_u);
for ( dis_u = dir_u->dis_u; dis_u; dis_u = dis_u->nex_u ) {
2014-09-11 04:01:32 +04:00
u3_noun syd = _unix_dir_name(dis_u);
2013-09-29 00:21:18 +04:00
2014-11-06 03:20:01 +03:00
// uL(fprintf(uH, "sync %s %s\n", u3r_string(hox), u3r_string(syd)));
2014-09-11 04:01:32 +04:00
_unix_desk_sync_into(u3k(who), u3k(hox), syd, dis_u);
2013-09-29 00:21:18 +04:00
}
2014-09-11 04:01:32 +04:00
u3z(hox);
u3z(who);
2013-09-29 00:21:18 +04:00
closedir(rid_u);
_unix_dir_dry(dir_u);
}
}
/* _unix_hot_gain(): gain ship.
2015-03-05 03:36:29 +03:00
*
* I feel like there's some freeing here I missed
2015-03-25 00:46:32 +03:00
* particularly pin_c and/or pot_c
2013-09-29 00:21:18 +04:00
*/
static void
2014-11-06 06:10:22 +03:00
_unix_hot_gain(u3_noun who, u3_noun mek)
2013-09-29 00:21:18 +04:00
{
2014-11-06 06:10:22 +03:00
u3_noun hox = u3dc("scot", 'p', u3k(who));
2014-11-06 03:20:01 +03:00
c3_c* hox_c = u3r_string(hox);
2014-11-16 04:10:15 +03:00
c3_c* pax_c = _unix_down(u3_Host.dir_c, hox_c + 1);
2015-02-27 09:35:41 +03:00
c3_c* pin_c = _unix_down(pax_c, "in");
c3_c* pot_c = _unix_down(pax_c, "out");
2015-03-23 23:40:33 +03:00
// DIR* rox_u = opendir(hox_c);
2013-09-29 00:21:18 +04:00
DIR* rid_u = opendir(pax_c);
2015-02-27 09:35:41 +03:00
DIR* rin_u = opendir(pin_c);
DIR* rot_u = opendir(pot_c);
2015-03-23 23:40:33 +03:00
// if ( !rox_u ) {
// if ( c3y == mek ) {
// _unix_mkdir(hox_c);
// } else {
// u3z(who);
// u3z(hox);
// return;
// }
// } else closedir(rox_u);
2013-09-29 00:21:18 +04:00
if ( !rid_u ) {
2014-11-05 04:18:47 +03:00
if ( c3y == mek ) {
2013-09-29 00:21:18 +04:00
_unix_mkdir(pax_c);
2013-11-09 03:37:38 +04:00
} else {
2014-09-11 04:01:32 +04:00
u3z(who);
u3z(hox);
2013-11-09 03:37:38 +04:00
return;
}
2013-09-29 00:21:18 +04:00
} else closedir(rid_u);
2015-02-27 09:35:41 +03:00
if ( !rin_u ) {
if ( c3y == mek ) {
_unix_mkdir(pin_c);
} else {
u3z(who);
u3z(hox);
return;
}
} else closedir(rin_u);
if ( !rot_u ) {
if ( c3y == mek ) {
_unix_mkdir(pot_c);
} else {
u3z(who);
u3z(hox);
return;
}
} else closedir(rot_u);
2013-09-29 00:21:18 +04:00
// uL(fprintf(uH, "GAIN %s\n", pax_c));
2014-11-20 03:46:16 +03:00
2015-03-05 03:36:29 +03:00
free(pax_c);
2013-09-29 00:21:18 +04:00
free(hox_c);
2014-09-11 04:01:32 +04:00
u3z(hox);
2015-02-27 09:35:41 +03:00
u3_unix_acquire(pin_c);
2013-09-29 00:21:18 +04:00
{
2014-09-11 04:01:32 +04:00
u3_uhot* hot_u = c3_malloc(sizeof(u3_uhot));
2013-09-29 00:21:18 +04:00
2015-02-27 09:35:41 +03:00
_unix_dir_watch(&hot_u->dir_u, 0, pin_c, pot_c);
2014-11-20 03:46:16 +03:00
_unix_ship_out(who, hot_u->who_w);
2014-09-11 04:01:32 +04:00
u3z(who);
2013-11-09 03:37:38 +04:00
2014-09-11 04:01:32 +04:00
hot_u->nex_u = u3_Host.unx_u.hot_u;
u3_Host.unx_u.hot_u = hot_u;
2013-09-29 00:21:18 +04:00
}
}
/* _unix_hot_lose(): release within a host directory.
*/
static void
2014-09-11 04:01:32 +04:00
_unix_hot_lose(u3_uhot* hot_u)
2013-09-29 00:21:18 +04:00
{
// uL(fprintf(uH, "lose: %s\n", hot_u->dir_u.pax_c));
_unix_dir_free(&(hot_u->dir_u));
}
/* _unix_pdir(): find directory reference from text.
*/
2014-09-11 04:01:32 +04:00
static u3_udir**
_unix_pdir(u3_udir* par_u, u3_noun tet)
2013-09-29 00:21:18 +04:00
{
2014-11-06 03:20:01 +03:00
c3_c* tet_c = u3r_string(tet);
2013-09-29 00:21:18 +04:00
c3_w pax_w = strlen(par_u->pax_c);
2014-09-11 04:01:32 +04:00
u3_udir** dir_u;
2013-09-29 00:21:18 +04:00
2015-05-06 02:42:00 +03:00
u3z(tet);
dir_u = &(par_u->dis_u);
while ( 1 ) {
if ( !*dir_u || !strcmp(((*dir_u)->pax_c + pax_w + 1), tet_c) ) {
2013-09-29 00:21:18 +04:00
free(tet_c); return dir_u;
}
else dir_u = &((*dir_u)->nex_u);
}
}
/* _unix_home(): find home directory from identity.
*/
2014-09-11 04:01:32 +04:00
static u3_uhot*
_unix_home(u3_noun who)
2013-09-29 00:21:18 +04:00
{
2014-09-11 04:01:32 +04:00
u3_unix* unx_u = &u3_Host.unx_u;
u3_uhot* hot_u;
2014-11-20 03:46:16 +03:00
c3_w who_w[4];
2013-09-29 00:21:18 +04:00
2014-11-20 03:46:16 +03:00
_unix_ship_out(who, who_w);
for ( hot_u = unx_u->hot_u;
2014-11-21 01:16:57 +03:00
hot_u && !_(_unix_ship_sing(who_w, hot_u->who_w));
hot_u = hot_u->nex_u )
2013-09-29 00:21:18 +04:00
{
// uL(fprintf(uH, "uh: %p, %s\n", hot_u, hot_u->dir_u.pax_c));
}
return hot_u;
}
2015-02-27 09:35:41 +03:00
#if 0
2013-09-29 00:21:18 +04:00
/* _unix_desk_sync_udon(): apply udon to existing value.
*/
2014-09-11 04:01:32 +04:00
static u3_noun
_unix_desk_sync_udon(u3_noun don, u3_noun old)
2013-09-29 00:21:18 +04:00
{
2014-11-06 06:10:22 +03:00
return u3dc("lump", don, old);
2013-09-29 00:21:18 +04:00
}
2015-02-27 09:35:41 +03:00
#endif
2013-09-29 00:21:18 +04:00
/* _unix_desk_sync_tofu(): sync out file install.
*/
static void
2014-09-11 04:01:32 +04:00
_unix_desk_sync_tofu(u3_udir* dir_u,
u3_noun pre,
u3_noun ext,
2015-02-27 09:35:41 +03:00
u3_noun mim)
2013-09-29 00:21:18 +04:00
{
2015-02-27 09:35:41 +03:00
c3_c* pox_c = _unix_file_form(dir_u->pax_c, u3k(pre), c3n, u3k(ext));
c3_c* pot_c = _unix_file_form(dir_u->pot_c, u3k(pre), c3n, u3k(ext));
c3_c* pux_c = _unix_file_form(dir_u->pax_c, u3k(pre), c3y, u3k(ext));
c3_c* put_c = _unix_file_form(dir_u->pot_c, u3k(pre), c3y, u3k(ext));
2014-09-11 04:01:32 +04:00
u3_ufil** fil_u;
2013-09-29 00:21:18 +04:00
2014-11-06 03:20:01 +03:00
// uL(fprintf(uH, "tofu pox_c %s op %s\n", pox_c, u3r_string(u3h(mis))));
2013-09-29 00:21:18 +04:00
#ifdef SYNCLOG
2014-09-11 04:01:32 +04:00
c3_w slot = u3_Host.unx_u.lot_w++ % 1024;
free(u3_Host.unx_u.sylo[slot].pax_c);
u3_Host.unx_u.sylo[slot].pax_c = 0;
#endif
fil_u = &(dir_u->fil_u);
2013-09-29 00:21:18 +04:00
while ( 1 ) { // XX crude!
if ( !*fil_u ||
2015-02-27 09:35:41 +03:00
!strcmp((*fil_u)->pax_c, pox_c) /* ||
!strcmp((*fil_u)->pax_c, pux_c) */ )
2013-09-29 00:21:18 +04:00
{
break;
}
else fil_u = &((*fil_u)->nex_u);
}
2015-02-27 09:35:41 +03:00
if ( *fil_u && u3_nul == mim ) {
uL(fprintf(uH, "file goning: %s\n", pox_c));
2014-09-11 04:01:32 +04:00
u3_ufil* ded_u = *fil_u;
2013-09-29 00:21:18 +04:00
#ifdef SYNCLOG
2014-11-05 04:18:47 +03:00
u3_Host.unx_u.sylo[slot].unx = c3n;
2014-09-11 04:01:32 +04:00
u3_Host.unx_u.sylo[slot].wer_m = c3_s4('t','o','f','u');
u3_Host.unx_u.sylo[slot].wot_m = c3__del;
u3_Host.unx_u.sylo[slot].pax_c = strdup(ded_u->pax_c);
#endif
2013-09-29 00:21:18 +04:00
*fil_u = ded_u->nex_u;
_unix_unlink(ded_u->pax_c);
2015-02-27 09:35:41 +03:00
_unix_unlink(ded_u->pot_c);
2013-09-29 00:21:18 +04:00
_unix_file_free(ded_u);
2015-02-27 09:35:41 +03:00
free(ded_u->pot_c);
2013-09-29 00:21:18 +04:00
free(pox_c);
2015-02-27 09:35:41 +03:00
free(pot_c);
2014-05-20 22:53:49 +04:00
free(pux_c);
2015-02-27 09:35:41 +03:00
free(put_c);
}
else if (u3_nul == mim) {
uL(fprintf(uH, "file already gone: %s\n", pox_c)); // fires too often
free(pox_c);
free(pot_c);
free(pux_c);
free(put_c);
2013-09-29 00:21:18 +04:00
}
else {
c3_c* pax_c;
2015-02-27 09:35:41 +03:00
c3_c* pat_c;
2013-09-29 00:21:18 +04:00
if ( *fil_u ) {
_unix_unlink((*fil_u)->pax_c);
free((*fil_u)->pax_c);
2015-02-27 09:35:41 +03:00
_unix_unlink((*fil_u)->pot_c);
free((*fil_u)->pot_c);
2013-09-29 00:21:18 +04:00
}
2015-02-27 09:35:41 +03:00
#if 0
2014-11-05 04:18:47 +03:00
if ( c3y == u3du(god) ) {
2014-05-20 22:53:49 +04:00
pax_c = pux_c; free(pox_c);
2015-02-27 09:35:41 +03:00
pat_c = put_c; free(pot_c);
2014-05-20 22:53:49 +04:00
} else {
2015-02-27 09:35:41 +03:00
#endif
2014-05-20 22:53:49 +04:00
pax_c = pox_c; free(pux_c);
2015-02-27 09:35:41 +03:00
pat_c = pot_c; free(put_c);
#if 0
2014-05-20 22:53:49 +04:00
}
2015-02-27 09:35:41 +03:00
#endif
2013-09-29 00:21:18 +04:00
#ifdef SYNCLOG
2014-11-05 04:18:47 +03:00
u3_Host.unx_u.sylo[slot].unx = c3n;
2014-09-11 04:01:32 +04:00
u3_Host.unx_u.sylo[slot].wer_m = c3_s4('t','o','f','u');
u3_Host.unx_u.sylo[slot].wot_m = u3h(mis);
u3_Host.unx_u.sylo[slot].pax_c = strdup(pax_c);
#endif
2015-02-27 09:35:41 +03:00
_unix_save(pax_c, u3k(u3t(mim)));
2015-05-06 02:42:00 +03:00
_unix_save(pat_c, u3k(u3t(mim)));
2013-09-29 00:21:18 +04:00
if ( *fil_u ) {
(*fil_u)->dot_c = (pax_c + ((*fil_u)->dot_c - (*fil_u)->pax_c));
(*fil_u)->pax_c = pax_c;
2015-02-27 09:35:41 +03:00
(*fil_u)->pot_c = pat_c;
2013-09-29 00:21:18 +04:00
2014-11-20 03:46:16 +03:00
u3r_words(0, 4, (*fil_u)->mod_w, u3A->now);
}
2013-09-29 00:21:18 +04:00
else {
2014-11-20 03:46:16 +03:00
c3_w mod_w[4];
2013-09-29 00:21:18 +04:00
2014-11-20 03:46:16 +03:00
u3r_words(0, 4, mod_w, u3A->now);
2014-09-11 04:01:32 +04:00
*fil_u = c3_malloc(sizeof(u3_ufil));
2013-09-29 00:21:18 +04:00
2015-02-27 09:35:41 +03:00
_unix_file_watch(*fil_u, dir_u, pax_c, pat_c, mod_w);
2013-09-29 00:21:18 +04:00
}
}
2015-02-27 09:35:41 +03:00
u3z(pre); u3z(ext); u3z(mim);
2013-09-29 00:21:18 +04:00
}
/* _unix_desk_sync_tako(): sync out change.
2013-09-29 00:21:18 +04:00
*/
static void
2015-02-27 09:35:41 +03:00
_unix_desk_sync_tako(u3_udir* dir_u, u3_noun pax, u3_noun mim)
2013-09-29 00:21:18 +04:00
{
2014-11-05 04:18:47 +03:00
if ( (c3n == u3du(pax)) ) {
2014-05-16 04:34:24 +04:00
c3_assert(!"tack");
}
2015-05-06 02:42:00 +03:00
else if ( c3n == u3du(u3t(pax)) ) { // at toplevel
2014-09-11 04:01:32 +04:00
u3_noun i_pax = u3h(pax);
u3_noun t_pax = u3t(pax);
2014-05-17 00:44:40 +04:00
c3_c* par_u = strrchr(dir_u->pax_c, '/') + 1;
2014-11-06 03:20:01 +03:00
u3_noun pem = u3i_string(par_u);
2015-05-06 02:42:00 +03:00
c3_assert( u3_nul == t_pax ); // XX ugly, wrong
2013-09-29 00:21:18 +04:00
2015-02-27 09:35:41 +03:00
_unix_desk_sync_tofu(dir_u->par_u, pem, u3k(i_pax), mim);
2013-09-29 00:21:18 +04:00
}
else {
2014-09-11 04:01:32 +04:00
u3_noun i_pax = u3h(pax);
u3_noun t_pax = u3t(pax);
u3_noun it_pax = u3h(t_pax);
u3_noun tt_pax = u3t(t_pax);
2013-09-29 00:21:18 +04:00
2014-09-11 04:01:32 +04:00
if ( u3_nul == tt_pax ) {
2015-02-27 09:35:41 +03:00
_unix_desk_sync_tofu(dir_u, u3k(i_pax), u3k(it_pax), mim);
}
2013-09-29 00:21:18 +04:00
else {
2014-09-11 04:01:32 +04:00
u3_udir** dis_u = _unix_pdir(dir_u, u3k(i_pax));
2013-09-29 00:21:18 +04:00
if ( !*dis_u ) {
2014-09-11 04:01:32 +04:00
*dis_u = c3_malloc(sizeof(u3_udir));
2013-09-29 00:21:18 +04:00
2014-09-11 04:01:32 +04:00
_unix_dir_forge(*dis_u, dir_u, u3k(i_pax));
2013-09-29 00:21:18 +04:00
}
2015-02-27 09:35:41 +03:00
_unix_desk_sync_tako(*dis_u, u3k(t_pax), mim);
2013-09-29 00:21:18 +04:00
}
}
2014-09-11 04:01:32 +04:00
u3z(pax);
2013-09-29 00:21:18 +04:00
}
2015-05-06 02:42:00 +03:00
/* _unix_desk_sync_list(): sync computed changes.
2013-09-29 00:21:18 +04:00
*/
static void
2015-02-27 09:35:41 +03:00
_unix_desk_sync_list(u3_udir* dir_u, u3_noun can)
2013-09-29 00:21:18 +04:00
{
2015-05-06 02:42:00 +03:00
u3_noun nac = can;
while ( u3_nul != nac ) {
_unix_desk_sync_tako(dir_u, u3k(u3h(u3h(nac))), u3k(u3t(u3h(nac))));
nac = u3t(nac);
2013-09-29 00:21:18 +04:00
}
2015-05-06 02:42:00 +03:00
2015-02-27 09:35:41 +03:00
u3z(can);
2013-09-29 00:21:18 +04:00
}
/* _unix_desk_sync_ergo(): sync desk changes to unix.
*/
static void
2014-09-11 04:01:32 +04:00
_unix_desk_sync_ergo(u3_noun hox,
u3_noun syd,
u3_noun lok,
2015-02-27 09:35:41 +03:00
u3_noun can,
2014-09-11 04:01:32 +04:00
u3_uhot* hot_u)
2013-09-29 00:21:18 +04:00
{
2015-05-06 02:42:00 +03:00
#if 0
u3z(hox); u3z(syd); u3z(lok); u3z(can);
#else
2014-09-11 04:01:32 +04:00
u3_udir** dir_u = _unix_pdir(&(hot_u->dir_u), syd);
2013-09-29 00:21:18 +04:00
2014-08-12 04:12:51 +04:00
#if 0
2014-11-06 03:20:01 +03:00
uL(fprintf(uH, "ergo %s %s %s\n", u3r_string(hox),
u3r_string(syd),
u3r_string(lok)));
2013-09-29 00:21:18 +04:00
#endif
if ( !*dir_u ) {
2014-09-11 04:01:32 +04:00
*dir_u = c3_malloc(sizeof(u3_udir));
2013-09-29 00:21:18 +04:00
2014-09-11 04:01:32 +04:00
_unix_dir_forge(*dir_u, &(hot_u->dir_u), u3k(syd));
2013-09-29 00:21:18 +04:00
}
2015-02-27 09:35:41 +03:00
_unix_desk_sync_list(*dir_u, can);
2015-05-06 02:42:00 +03:00
u3z(lok); u3z(syd); u3z(hox);
#endif
2013-09-29 00:21:18 +04:00
}
2014-09-11 04:01:32 +04:00
/* u3_unix_ef_init(): update filesystem for new acquisition.
2013-09-29 00:21:18 +04:00
*/
void
2014-09-11 04:01:32 +04:00
u3_unix_ef_init(u3_noun who)
2013-09-29 00:21:18 +04:00
{
2014-11-05 04:18:47 +03:00
_unix_hot_gain(u3k(who), c3y);
2013-09-29 00:21:18 +04:00
2014-11-06 03:20:01 +03:00
u3v_plan(u3nq(u3_blip, c3__sync, u3k(u3A->sen), u3_nul),
2014-09-11 04:01:32 +04:00
u3nq(c3__into, who,
u3_blip,
2015-02-27 09:35:41 +03:00
u3nc(u3_nul, u3_nul)));
2013-09-29 00:21:18 +04:00
}
2014-09-11 04:01:32 +04:00
/* u3_unix_ef_ergo(): update filesystem, outbound.
2013-09-29 00:21:18 +04:00
*/
void
2014-09-11 04:01:32 +04:00
u3_unix_ef_ergo(u3_noun who,
u3_noun syd,
2015-02-27 09:35:41 +03:00
u3_noun rel,
u3_noun can)
2013-09-29 00:21:18 +04:00
{
2015-02-27 09:35:41 +03:00
u3_noun hox = u3dc("scot", 'p', u3k(who)); // XXX unnecessary?
u3_noun lok = u3dc("scot", c3__ud, rel); // XXX unnecessary?
2014-09-11 04:01:32 +04:00
u3_uhot* hot_u;
2013-09-29 00:21:18 +04:00
2014-01-06 04:02:34 +04:00
hot_u = _unix_home(who);
2015-05-06 02:42:00 +03:00
u3z(who);
2013-09-29 00:21:18 +04:00
if ( 0 != hot_u ) {
2015-02-27 09:35:41 +03:00
_unix_desk_sync_ergo(hox, syd, lok, can, hot_u);
2013-09-29 00:21:18 +04:00
}
2015-05-06 02:42:00 +03:00
else
{
u3z(syd); u3z(syd); u3z(rel); u3z(can);
}
2013-09-29 00:21:18 +04:00
}
2014-09-11 04:01:32 +04:00
/* u3_unix_ef_look(): update the root.
2013-09-29 00:21:18 +04:00
*/
void
2014-09-11 04:01:32 +04:00
u3_unix_ef_look(void)
2013-09-29 00:21:18 +04:00
{
2014-09-11 04:01:32 +04:00
u3_unix* unx_u = &u3_Host.unx_u;
u3_noun won;
u3_uhot* hot_u;
2013-09-29 00:21:18 +04:00
2014-09-11 04:01:32 +04:00
if ( u3_nul != u3A->roe ) {
// We can't generate a working %into event here because there
// are other events, possibly containing %into, that are queued;
// they will change the state of %clay and cause a patch that
// doesn't work.
//
return;
}
2013-09-29 00:21:18 +04:00
// find owners without directories
{
2014-09-11 04:01:32 +04:00
for ( won = u3A->own; u3_nul != won; won = u3t(won) ) {
u3_noun who = u3h(won);
2014-11-20 03:46:16 +03:00
c3_w who_w[4];
2013-09-29 00:21:18 +04:00
2014-11-20 03:46:16 +03:00
_unix_ship_out(who, who_w);
for ( hot_u = unx_u->hot_u;
2014-11-20 03:46:16 +03:00
hot_u && !_(_unix_ship_sing(who_w, hot_u->who_w));
2013-09-29 00:21:18 +04:00
hot_u = hot_u->nex_u );
if ( 0 == hot_u ) {
2014-11-05 04:18:47 +03:00
_unix_hot_gain(u3k(who), c3n);
2013-09-29 00:21:18 +04:00
}
}
}
// find directories without owners
{
2014-09-11 04:01:32 +04:00
u3_uhot** het_u = &(unx_u->hot_u);
2013-09-29 00:21:18 +04:00
while ( 0 != (hot_u=*het_u) ) {
2014-09-11 04:01:32 +04:00
for ( won = u3A->own; u3_nul != won; won = u3t(won) ) {
u3_noun who = u3h(won);
2014-11-20 03:46:16 +03:00
c3_w who_w[4];
2013-09-29 00:21:18 +04:00
2014-11-20 03:46:16 +03:00
_unix_ship_out(who, who_w);
if ( _(_unix_ship_sing(who_w, hot_u->who_w)) ) {
2013-09-29 00:21:18 +04:00
break;
}
}
2014-09-11 04:01:32 +04:00
if ( u3_nul == won ) {
2013-09-29 00:21:18 +04:00
*het_u = hot_u->nex_u;
2013-09-29 00:21:18 +04:00
// uL(fprintf(uH, "sync: lose %s\n", hot_u->dir_u.pax_c));
_unix_hot_lose(hot_u);
free(hot_u);
continue;
}
else {
het_u = &(hot_u->nex_u);
}
}
}
// update all ships
{
2014-09-11 04:01:32 +04:00
u3_uhot* hot_u;
2013-09-29 00:21:18 +04:00
for ( hot_u = unx_u->hot_u; hot_u; hot_u = hot_u->nex_u ) {
_unix_ship_update(hot_u);
}
}
}
/* _unix_ef_sync(): check for files to sync.
*/
static void
2014-08-21 02:09:51 +04:00
_unix_ef_sync(uv_check_t* han_u)
{
2014-09-11 04:01:32 +04:00
u3_lo_open();
2014-11-05 04:18:47 +03:00
u3_lo_shut(c3y);
}
2013-09-29 00:21:18 +04:00
/* _unix_sign_cb: signal callback.
*/
static void
_unix_sign_cb(uv_signal_t* sil_u, c3_i num_i)
{
2014-09-11 04:01:32 +04:00
u3_lo_open();
2013-09-29 00:21:18 +04:00
{
switch ( num_i ) {
2013-10-14 22:23:55 +04:00
default: fprintf(stderr, "\r\nmysterious signal %d\r\n", num_i); break;
case SIGTERM:
2013-09-29 00:21:18 +04:00
fprintf(stderr, "\r\ncaught signal %d\r\n", num_i);
2014-11-05 04:18:47 +03:00
u3_Host.liv = c3n;
2013-09-29 00:21:18 +04:00
break;
2014-10-05 01:57:40 +04:00
case SIGINT:
fprintf(stderr, "\r\ninterrupt\r\n");
u3_term_ef_ctlc();
break;
2014-09-11 04:01:32 +04:00
case SIGWINCH: u3_term_ef_winc(); break;
// case SIGCHLD: u3_save_ef_chld(); break;
2013-09-29 00:21:18 +04:00
}
}
2014-11-05 04:18:47 +03:00
u3_lo_shut(c3y);
2013-09-29 00:21:18 +04:00
}
2014-09-11 04:01:32 +04:00
/* u3_unix_ef_hold()
2013-09-29 00:21:18 +04:00
*/
void
2014-09-11 04:01:32 +04:00
u3_unix_ef_hold(void)
2013-09-29 00:21:18 +04:00
{
2014-09-11 04:01:32 +04:00
u3_unix* unx_u = &u3_Host.unx_u;
u3_usig* sig_u;
2013-09-29 00:21:18 +04:00
for ( sig_u = unx_u->sig_u; sig_u; sig_u = sig_u->nex_u ) {
uv_signal_stop(&sig_u->sil_u);
2013-09-29 00:21:18 +04:00
}
}
2014-09-11 04:01:32 +04:00
/* u3_unix_ef_move()
2013-09-29 00:21:18 +04:00
*/
void
2014-09-11 04:01:32 +04:00
u3_unix_ef_move(void)
2013-09-29 00:21:18 +04:00
{
2014-09-11 04:01:32 +04:00
u3_unix* unx_u = &u3_Host.unx_u;
u3_usig* sig_u;
2013-09-29 00:21:18 +04:00
for ( sig_u = unx_u->sig_u; sig_u; sig_u = sig_u->nex_u ) {
uv_signal_start(&sig_u->sil_u, _unix_sign_cb, sig_u->num_i);
2013-09-29 00:21:18 +04:00
}
}
2014-09-11 04:01:32 +04:00
/* u3_unix_io_init(): initialize unix sync.
2013-09-29 00:21:18 +04:00
*/
void
2014-09-11 04:01:32 +04:00
u3_unix_io_init(void)
2013-09-29 00:21:18 +04:00
{
2014-09-11 04:01:32 +04:00
u3_unix* unx_u = &u3_Host.unx_u;
2013-09-29 00:21:18 +04:00
{
2014-09-11 04:01:32 +04:00
u3_usig* sig_u;
2013-09-29 00:21:18 +04:00
2014-09-11 04:01:32 +04:00
sig_u = c3_malloc(sizeof(u3_usig));
uv_signal_init(u3L, &sig_u->sil_u);
2013-09-29 00:21:18 +04:00
sig_u->num_i = SIGTERM;
sig_u->nex_u = unx_u->sig_u;
unx_u->sig_u = sig_u;
}
{
2014-09-11 04:01:32 +04:00
u3_usig* sig_u;
2013-09-29 00:21:18 +04:00
2014-09-11 04:01:32 +04:00
sig_u = c3_malloc(sizeof(u3_usig));
uv_signal_init(u3L, &sig_u->sil_u);
2013-09-29 00:21:18 +04:00
sig_u->num_i = SIGINT;
sig_u->nex_u = unx_u->sig_u;
unx_u->sig_u = sig_u;
}
{
2014-09-11 04:01:32 +04:00
u3_usig* sig_u;
2013-09-29 00:21:18 +04:00
2014-09-11 04:01:32 +04:00
sig_u = c3_malloc(sizeof(u3_usig));
uv_signal_init(u3L, &sig_u->sil_u);
2013-09-29 00:21:18 +04:00
sig_u->num_i = SIGWINCH;
sig_u->nex_u = unx_u->sig_u;
unx_u->sig_u = sig_u;
}
#if SYNCLOG
unx_u->lot_w = 0;
memset(unx_u->sylo, 0, sizeof(unx_u->sylo));
#endif
2014-09-11 04:01:32 +04:00
uv_check_init(u3_Host.lup_u, &u3_Host.unx_u.syn_u);
2014-01-17 12:12:05 +04:00
}
2014-09-11 04:01:32 +04:00
/* u3_unix_io_talk(): start listening for fs events.
2014-01-17 12:12:05 +04:00
*/
void
2014-09-11 04:01:32 +04:00
u3_unix_io_talk()
2014-01-17 12:12:05 +04:00
{
2014-11-16 04:10:15 +03:00
u3_unix_acquire(u3_Host.dir_c);
2014-09-11 04:01:32 +04:00
u3_unix_ef_move();
uv_check_start(&u3_Host.unx_u.syn_u, _unix_ef_sync);
2013-09-29 00:21:18 +04:00
}
2014-09-11 04:01:32 +04:00
/* u3_unix_io_exit(): terminate unix I/O.
2013-09-29 00:21:18 +04:00
*/
void
2014-09-11 04:01:32 +04:00
u3_unix_io_exit(void)
2013-09-29 00:21:18 +04:00
{
2014-09-11 04:01:32 +04:00
uv_check_stop(&u3_Host.unx_u.syn_u);
2014-11-16 04:10:15 +03:00
u3_unix_release(u3_Host.dir_c);
2013-09-29 00:21:18 +04:00
{
2014-09-11 04:01:32 +04:00
u3_uhot* hot_u;
2013-09-29 00:21:18 +04:00
2014-09-11 04:01:32 +04:00
for ( hot_u = u3_Host.unx_u.hot_u; hot_u; hot_u = hot_u->nex_u ) {
u3_unix_release(hot_u->dir_u.pax_c);
2013-09-29 00:21:18 +04:00
}
}
#ifdef SYNCLOG
for (int i = 0; i<1024; i++) {
2014-09-11 04:01:32 +04:00
free(u3_Host.unx_u.sylo[i].pax_c);
}
#endif
2013-09-29 00:21:18 +04:00
}
2014-09-11 04:01:32 +04:00
/* u3_unix_io_poll(): update unix IO state.
2013-09-29 00:21:18 +04:00
*/
void
2014-09-11 04:01:32 +04:00
u3_unix_io_poll(void)
2013-09-29 00:21:18 +04:00
{
}