Example #1
0
/* Insert all file from directory into ffindex */
int ffindex_insert_list_file(FILE *data_file, FILE *index_file, size_t *start_offset, FILE *list_file)
{
  size_t offset = *start_offset;
  char path[PATH_MAX];
  while(fgets(path, PATH_MAX, list_file) != NULL)
    ffindex_insert_file(data_file, index_file, &offset, ffnchomp(path, strlen(path)), basename(path));

  /* update return value */
  *start_offset = offset;
  return 0;
}
Example #2
0
int main(int argn, char **argv)
{
  int sort = 0, unlink = 0, version = 0;
  int err = EXIT_SUCCESS;
  char* list_filenames[MAX_FILENAME_LIST_FILES];
  size_t list_filenames_index = 0;

  static struct option long_options[] =
          {
                  { "file",    required_argument, NULL, 'f' },
                  { "sort",    no_argument, NULL, 's' },
                  { "unlink",  no_argument, NULL, 'u' },
                  { "version", no_argument, NULL, 'v' },
                  { NULL,      0,           NULL,  0  }
          };

  int opt;
  while (1)
  {
    int option_index = 0;
    opt = getopt_long(argn, argv, "suvf:", long_options, &option_index);
    if (opt == -1)
      break;

    switch (opt)
    {
      case 'f':
        list_filenames[list_filenames_index++] = optarg;
            break;
      case 's':
        sort = 1;
            break;
      case 'u':
        unlink = 1;
            break;
      case 'v':
        version = 1;
            break;
      default:
        usage(argv[0]);
            return EXIT_FAILURE;
    }
  }

  if(version == 1)
  {
    /* Don't you dare running it on a platform where byte != 8 bits */
    printf("%s version %.2f, off_t = %zd bits\n", argv[0], FFINDEX_VERSION, sizeof(off_t) * 8);
    return EXIT_SUCCESS;
  }

  if(optind >= argn)
  {
    usage(argv[0]);
    return EXIT_FAILURE;
  }

  char *index_filename = argv[optind++];
  FILE *index_file;

  index_file = fopen(index_filename, "r+");
  if(index_file == NULL) { perror(index_filename); return EXIT_FAILURE; }

  size_t entries = ffcount_lines(index_filename);
  ffindex_index_t* index = ffindex_index_parse(index_file, entries);
  if(index == NULL) { perror("ffindex_index_parse failed"); return (EXIT_FAILURE); }
  fclose(index_file);

  /* Unlink entries */
  if(unlink)
  {
    /* For each list_file unlink all entries */
    for(int i = 0; i < list_filenames_index; i++) {
      printf("Unlinking entries from '%s'\n", list_filenames[i]);
      FILE *list_file = fopen(list_filenames[i], "r");
      if (list_file == NULL) {
        perror(list_filenames[i]);
        return EXIT_FAILURE;
      }

      /* unlink entries in file, one per line */
      char path[PATH_MAX];
      while(fgets(path, PATH_MAX, list_file) != NULL) {
        index = ffindex_unlink(index, ffnchomp(path, strlen(path)));
      }
    }

    /* unlink entries specified by args */
    for(int i = optind; i < argn; i++) {
      index = ffindex_unlink(index, argv[i]);
    }

    /* Sort the index entries and write back */
    if(sort) {
      ffindex_sort_index_file(index);
      index_file = fopen(index_filename, "w");
      if(index_file == NULL) {
        perror(index_filename);
        return EXIT_FAILURE;
      }
      err += ffindex_write(index, index_file);
    }
  }

  /* Write index back */
  index_file = fopen(index_filename, "w");
  if(index_file == NULL) {
    perror(index_filename);
    return EXIT_FAILURE;
  }
  err += ffindex_write(index, index_file);
  return err;
}
Example #3
0
int main(int argn, char **argv)
{
  int sort = 0, unlink = 0, version = 0, use_tree = 1;
  int opt, err = EXIT_SUCCESS;
  char* list_filenames[MAX_FILENAME_LIST_FILES];
  size_t list_filenames_index = 0;
  while ((opt = getopt(argn, argv, "stuvf:")) != -1)
  {
    switch (opt)
    {
      case 'f':
        list_filenames[list_filenames_index++] = optarg;
        break;
      case 's':
        sort = 1;
        break;
      case 't':
        use_tree = 1;
        break;
      case 'u':
        unlink = 1;
        break;
      case 'v':
        version = 1;
        break;
      default:
        fprintf(stderr, "Option %c not recognized\n", opt);
        usage(argv[0]);
        return EXIT_FAILURE;
    }
  }

  if(version == 1)
  {
    /* Don't you dare running it on a platform where byte != 8 bits */
    printf("%s version %.2f, off_t = %zd bits\n", argv[0], FFINDEX_VERSION, sizeof(off_t) * 8);
    return EXIT_SUCCESS;
  }

  if(optind >= argn)
  {
    usage(argv[0]);
    return EXIT_FAILURE;
  }

  char *index_filename = argv[optind++];
  FILE *index_file;

  index_file = fopen(index_filename, "r+");
  if(index_file == NULL) { perror(index_filename); return EXIT_FAILURE; }

  ffindex_index_t* index = ffindex_index_parse(index_file, 0);
  if(index == NULL) { perror("ffindex_index_parse failed"); return (EXIT_FAILURE); }

  fclose(index_file);

  /* Unlink entries */
  if(unlink)
  {
    if(use_tree)
    {
      /* Build tree */
      index = ffindex_index_as_tree(index);

      /* For each list_file unlink all entries */
      if(list_filenames_index > 0)
        for(int i = 0; i < list_filenames_index; i++)
        {
          printf("Unlinking entries from '%s'\n", list_filenames[i]);
          FILE *list_file = fopen(list_filenames[i], "r");
          if( list_file == NULL) { perror(list_filenames[i]); return EXIT_FAILURE; }

          /* unlink entries in file, one per line */
          char path[PATH_MAX];
          while(fgets(path, PATH_MAX, list_file) != NULL)
            index = ffindex_unlink(index, ffnchomp(path, strlen(path)));
        }

      /* unlink entries specified by args */
      for(int i = optind; i < argn; i++)
        index = ffindex_unlink(index, argv[i]);
    }
    else
    {
      char** sorted_names_to_unlink = malloc(FFINDEX_MAX_INDEX_ENTRIES_DEFAULT * sizeof(char *));
      if(sorted_names_to_unlink == NULL)
        fferror_print(__FILE__, __LINE__, __func__, "malloc failed");
      /* For each list_file unlink all entries */
      if(list_filenames_index > 0)
        for(int i = 0; i < list_filenames_index; i++)
        {
          printf("Unlinking entries from '%s'\n", list_filenames[i]);
          FILE *list_file = fopen(list_filenames[i], "r");
          if( list_file == NULL) { perror(list_filenames[i]); return EXIT_FAILURE; }

          /* unlink entries in file, one per line */
          char path[PATH_MAX];
          while(fgets(path, PATH_MAX, list_file) != NULL)
            sorted_names_to_unlink[i++] = ffnchomp(strdup(path), strlen(path));
          ffindex_unlink_entries(index, sorted_names_to_unlink, i);
        }

      /* unlink entries specified by args */
      int y = 0;
      for(int i = optind; i < argn; i++, y++)
        sorted_names_to_unlink[y] = argv[i];
      ffindex_unlink_entries(index, sorted_names_to_unlink, y);

      /* Sort the index entries and write back */
      if(sort)
      {
        ffindex_sort_index_file(index);
        index_file = fopen(index_filename, "w");
        if(index_file == NULL) { perror(index_filename); return EXIT_FAILURE; }
        err += ffindex_write(index, index_file);
      }
    }
  }

  /* Write index back */
  index_file = fopen(index_filename, "w");
  if(index_file == NULL) { perror(index_filename); return EXIT_FAILURE; }
  err += ffindex_write(index, index_file);
  return err;
}