Exemplo n.º 1
0
Arquivo: walk.c Projeto: Gruelty/urbit
/* _walk_in(): inner loop of _walk(), producing map.
*/
static u2_noun
_walk_in(u2_reck* rec_u, const c3_c* dir_c, c3_w len_w)
{
  DIR*    dir_d = opendir(dir_c);
  u2_noun map = u2_nul;

  if ( !dir_d ) {
    return u2_nul;
  }
  else while ( 1 ) {
    struct dirent  ent_n;
    struct dirent* out_n;

    if ( readdir_r(dir_d, &ent_n, &out_n) != 0 ) {
      uL(fprintf(uH, "%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 = malloc(lef_w + 1);
      struct stat buf_b;
  
      strcpy(pat_c, dir_c);
      pat_c[len_w] = '/';
      strcpy(pat_c + len_w + 1, fil_c);

      if ( 0 != stat(pat_c, &buf_b) ) {
        free(pat_c);
      } else {
        u2_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;
          {
            u2_noun nam = u2_ci_string(nam_c);
            u2_noun ext = u2_ci_string(ext_c);
            u2_noun get = u2_ckd_by_get(u2k(map), u2k(nam));
            u2_noun dat = u2_walk_load(pat_c);
            u2_noun hax;

            if ( !strcmp("noun", ext_c) ) {
              dat = u2_cke_cue(dat);
            }
            hax = u2_do("sham", u2k(dat));
            if ( u2_none == get ) { get = u2_nul; }
          
            get = u2_ckd_by_put(get, ext, u2nt(u2_yes, hax, dat));
            map = u2_ckd_by_put(map, nam, u2nc(u2_no, get));
          }
          free(nam_c);
          free(ext_c);
        }
        else {
          u2_noun dir = _walk_in(rec_u, pat_c, lef_w);

          if ( u2_nul != dir ) {
            map = u2_ckd_by_put
              (map, u2_ci_string(fil_c), u2nc(u2_no, dir));
          }
          else u2z(tim);
        }
        free(pat_c);
      }
    }
  }
  closedir(dir_d);
  return map;
}
Exemplo n.º 2
0
/* _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;
}
Exemplo n.º 3
0
Arquivo: unix.c Projeto: esaul/urbit
/* _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);
            }
          }
        }
      }
    }
  }
}