/* _unix_dir_free(): free (within) directory tracker. */ static void _unix_dir_free(u2_udir* dir_u) { free(dir_u->pax_c); while ( dir_u->dis_u ) { u2_udir* nex_u = dir_u->dis_u->nex_u; _unix_dir_free(dir_u->dis_u); free(dir_u->dis_u); dir_u->dis_u = nex_u; } while ( dir_u->fil_u ) { u2_ufil* nex_u = dir_u->fil_u->nex_u; _unix_file_free(dir_u->fil_u); free(dir_u->fil_u); dir_u->fil_u = nex_u; } }
static void _unix_dir_done(uv_handle_t* was_u) { u2_udir* dir_u = (void*) was_u; while ( dir_u->dis_u ) { u2_udir* nex_u = dir_u->dis_u->nex_u; _unix_dir_free(dir_u->dis_u); dir_u->dis_u = nex_u; } while ( dir_u->fil_u ) { u2_ufil* nex_u = dir_u->fil_u->nex_u; _unix_file_free(dir_u->fil_u); dir_u->fil_u = nex_u; } free(dir_u->pax_c); free(dir_u); }
/* _unix_hot_lose(): release within a host directory. */ static void _unix_hot_lose(u2_uhot* hot_u) { uL(fprintf(uH, "lose: %s\n", hot_u->dir_u.pax_c)); _unix_dir_free(&(hot_u->dir_u)); }
/* _unix_dir_update(): update directory, true if changed. */ static u2_bean _unix_dir_update(u2_udir* dir_u, DIR* rid_u) { u2_bean cha = u2_no; // uL(fprintf(uH, "dir_update ON %s\n", dir_u->pax_c)); /* use dry bits as markers */ { u2_udir* dis_u; u2_ufil* fil_u; for ( dis_u = dir_u->dis_u; dis_u; dis_u = dis_u->nex_u ) { dis_u->dry = u2_yes; } for ( fil_u = dir_u->fil_u; fil_u; fil_u = fil_u->nex_u ) { fil_u->dry = u2_yes; } } /* iterate through directory, opening and updating */ 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); } 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); struct stat buf_u; // uL(fprintf(uH, " in %s\n", pax_c)); if ( 0 != stat(pax_c, &buf_u) ) { free(pax_c); continue; } else { if ( !S_ISDIR(buf_u.st_mode) ) { mpz_t mod_mp; u2_ufil* fil_u; { u2_noun mod = c3_stat_mtime(&buf_u); u2_cr_mp(mod_mp, mod); u2z(mod); } for ( fil_u = dir_u->fil_u; fil_u; fil_u = fil_u->nex_u ) { if ( !strcmp(pax_c, fil_u->pax_c) ) { fil_u->dry = u2_no; cha = u2_or(cha, _unix_file_update(fil_u, mod_mp)); break; } } if ( !fil_u ) { fil_u = malloc(sizeof(u2_ufil)); // uL(fprintf(uH, "found file %s\n", pax_c)); _unix_file_watch(fil_u, dir_u, pax_c, mod_mp); fil_u->nex_u = dir_u->fil_u; dir_u->fil_u = fil_u; cha = u2_yes; } mpz_clear(mod_mp); } else { DIR* red_u = _unix_opendir(pax_c); u2_udir* dis_u; for ( dis_u = dir_u->dis_u; dis_u; dis_u = dis_u->nex_u ) { if ( !strcmp(pax_c, dis_u->pax_c) ) { dis_u->dry = u2_no; cha = u2_or(cha, _unix_dir_update(dis_u, red_u)); break; } } if ( !dis_u ) { dis_u = malloc(sizeof(u2_udir)); // uL(fprintf(uH, "found directory %s\n", pax_c)); _unix_dir_watch(dis_u, dir_u, pax_c); _unix_dir_update(dis_u, red_u); dis_u->nex_u = dir_u->dis_u; dir_u->dis_u = dis_u; cha = u2_yes; } } } } } /* use dry bits as markers */ { u2_udir** dis_u; u2_ufil** fil_u; for ( dis_u = &(dir_u->dis_u); *dis_u; ) { if ( u2_no == (*dis_u)->dry ) { (*dis_u)->dry = u2_yes; dis_u = &(*dis_u)->nex_u; } else { u2_udir* ded_u = *dis_u; u2_udir* nex_u = ded_u->nex_u; // uL(fprintf(uH, "removed directory %s\n", ded_u->pax_c)); _unix_dir_free(ded_u); free(ded_u); *dis_u = nex_u; cha = u2_yes; } } for ( fil_u = &(dir_u->fil_u); *fil_u; ) { if ( u2_no == (*fil_u)->dry ) { fil_u = &(*fil_u)->nex_u; } else { u2_ufil* ded_u = *fil_u; u2_ufil* nex_u = ded_u->nex_u; // uL(fprintf(uH, "removed file %s\n", ded_u->pax_c)); _unix_file_free(ded_u); free(ded_u); *fil_u = nex_u; cha = u2_yes; } } } closedir(rid_u); // uL(fprintf(uH, "dir_update OFF %s\n", dir_u->pax_c)); return cha; }
/* _unix_dir_update(): update directory. */ static void _unix_dir_update(u2_udir* dir_u, DIR* rid_u) { if ( u2_yes == dir_u->dry ) { return; } else { // Update all wet subdirectories. // u2_udir** dis_u; u2_ufil** fil_u; for ( dis_u = &(dir_u->dis_u); *dis_u; ) { if ( u2_yes == (*dis_u)->dry ) { dis_u = &(*dis_u)->nex_u; } else { DIR* red_u = opendir((*dis_u)->pax_c); if ( 0 == red_u ) { u2_udir* ded_u = *dis_u; u2_udir* nex_u = ded_u->nex_u; // 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; ) { if ( u2_yes == (*fil_u)->dry ) { fil_u = &(*fil_u)->nex_u; } else { struct stat buf_u; if ( -1 == stat((*fil_u)->pax_c, &buf_u) || !(S_IFREG & buf_u.st_mode) ) { u2_ufil* ded_u = *fil_u; u2_ufil* nex_u = ded_u->nex_u; // uL(fprintf(uH, "removed file %s\n", ded_u->pax_c)); _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); } 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); struct stat buf_u; // uL(fprintf(uH, " in %s\n", pax_c)); if ( 0 != stat(pax_c, &buf_u) ) { free(pax_c); continue; } else { if ( !S_ISDIR(buf_u.st_mode) ) { mpz_t mod_mp; u2_ufil* fil_u; if ( ( NULL == strrchr(out_u->d_name, '.')) || ( '~' == out_u->d_name[strlen(out_u->d_name) - 1] ) ) { continue; } { u2_noun mod = c3_stat_mtime(&buf_u); u2_cr_mp(mod_mp, mod); u2z(mod); } 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 ) { fil_u = c3_malloc(sizeof(u2_ufil)); // uL(fprintf(uH, "found file %s\n", pax_c)); _unix_file_watch(fil_u, dir_u, pax_c, mod_mp); fil_u->nex_u = dir_u->fil_u; dir_u->fil_u = fil_u; } mpz_clear(mod_mp); } else { u2_udir* dis_u; 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); dis_u = c3_malloc(sizeof(u2_udir)); // uL(fprintf(uH, "found directory %s\n", pax_c)); _unix_dir_watch(dis_u, dir_u, pax_c); _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); } } } } } } }