Пример #1
0
int main(void) {
  atexit(cleanup);

  system("rm -fr DIR");

  /* simple case */
  xmkdir("DIR");
  xmkdir("DIR/a");
  xmkdir("DIR/a/b");
  xmkdir("DIR/c");
  xmkdir("DIR/d");
  touch("DIR/0");
  touch("DIR/a/1");
  touch("DIR/a/b/2");
  touch("DIR/c/3");
  touch("DIR/c/4");
  /* DIR/d is empty */

  struct Expected expected[] = {
    {0, "c/4"},
    {0, "c/3"},
    {0, "a/b/2"},
    {0, "a/1"},
    {0, "0"},
  };

  struct DirListEntry * list = make_recursive_dir_list("DIR");
  verify(expected, list, sizeof(expected) / sizeof(expected[0]));
  free_dir_list(list);

  exit(EXIT_SUCCESS);
}
Пример #2
0
void jscoverage_instrument(const char * source,
                           const char * destination,
                           int verbose,
                           char ** exclude,
                           int num_exclude,
                           char ** no_instrument,
                           int num_no_instrument)
{
  assert(source != NULL);
  assert(destination != NULL);

  g_verbose = verbose;

  /* check if they are the same */
  check_same_file(source, destination);

  /* check if source directory is an ancestor of destination directory */
  check_contains_file(source, destination);

  /* check that the source exists and is a directory */
  struct stat buf;
  xstat(source, &buf);
  if (! S_ISDIR(buf.st_mode)) {
    fatal("not a directory: %s", source);
  }

  /* if the destination directory exists, check that it is a jscoverage directory */
  if (stat(destination, &buf) == 0) {
    /* it exists */
    if (! S_ISDIR(buf.st_mode)) {
      fatal("not a directory: %s", destination);
    }
    if (! directory_is_empty(destination)) {
      char * expected_file = NULL;
      if (jscoverage_mode == JSCOVERAGE_MOZILLA) {
        char * modules_directory = make_path(destination, "modules");
        expected_file = make_path(modules_directory, "jscoverage.jsm");
        free(modules_directory);
      }
      else {
        expected_file = make_path(destination, "jscoverage.html");
      }
      if (stat(expected_file, &buf) == -1) {
        fatal("refusing to overwrite directory: %s", destination);
      }
      free(expected_file);
    }
  }
  else if (errno == ENOENT) {
    xmkdir(destination);
  }
  else {
    fatal("cannot stat directory: %s", destination);
  }

  /* copy the resources */
  if (jscoverage_mode == JSCOVERAGE_MOZILLA) {
    char * chrome_directory = make_path(destination, "chrome");
    char * jscoverage_chrome_directory = make_path(chrome_directory, "jscoverage");
    mkdirs(jscoverage_chrome_directory);
    copy_resource("jscoverage.manifest", chrome_directory);
    copy_resource("jscoverage.html", jscoverage_chrome_directory);
    copy_resource("jscoverage.css", jscoverage_chrome_directory);
    copy_resource("jscoverage.js", jscoverage_chrome_directory);
    copy_resource("jscoverage-throbber.gif", jscoverage_chrome_directory);
    copy_resource("jscoverage-highlight.css", jscoverage_chrome_directory);
    copy_resource("jscoverage.xul", jscoverage_chrome_directory);
    copy_resource("jscoverage-overlay.js", jscoverage_chrome_directory);
    free(jscoverage_chrome_directory);
    free(chrome_directory);

    char * modules_directory = make_path(destination, "modules");
    mkdirs(modules_directory);
    copy_resource("jscoverage.jsm", modules_directory);
    free(modules_directory);
  }
  else {
    jscoverage_copy_resources(destination);
  }

  /* finally: copy the directory */
  struct DirListEntry * list = make_recursive_dir_list(source);
  for (struct DirListEntry * p = list; p != NULL; p = p->next) {
    char * s = make_path(source, p->name);
    char * d = make_path(destination, p->name);

    /* check if it's on the exclude list */
    for (int i = 0; i < num_exclude; i++) {
      char * x = make_path(source, exclude[i]);
      if (is_same_file(x, s) || contains_file(x, s)) {
        free(x);
        goto cleanup;
      }
      free(x);
    }

    char * dd = make_dirname(d);
    mkdirs(dd);
    free(dd);

    int instrument_this = 1;

    /* check if it's on the no-instrument list */
    for (int i = 0; i < num_no_instrument; i++) {
      char * ni = make_path(source, no_instrument[i]);
      if (is_same_file(ni, s) || contains_file(ni, s)) {
        instrument_this = 0;
      }
      free(ni);
    }

    instrument_file(s, d, p->name, instrument_this);

  cleanup:
    free(s);
    free(d);
  }

  free_dir_list(list);
}