File: ffindex_get.c

package info (click to toggle)
ffindex 0.9.9.9-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 488 kB
  • sloc: ansic: 2,382; makefile: 163; perl: 86; python: 56; sh: 21; ruby: 6
file content (190 lines) | stat: -rw-r--r-- 5,362 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
 * Ffindex
 * written by Andreas Hauser <andy@splashground.de>.
 * Please add your name here if you distribute modified versions.
 * 
 * Ffindex is provided under the Create Commons license "Attribution-ShareAlike
 * 4.0", which basically captures the spirit of the Gnu Public License (GPL).
 * 
 * See:
 * http://creativecommons.org/licenses/by-sa/4.0/
*/

#define _GNU_SOURCE 1
#define _LARGEFILE64_SOURCE 1
#define _FILE_OFFSET_BITS 64

#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include <libgen.h>

#include "ffindex.h"
#include "ffutil.h"

void usage(char* program_name)
{
    fprintf(stderr, "USAGE: %s data_filename index_filename entry name(s)\n"
                    "\t-f FILE\t\tfile containing a list of file names, one per line\n"
                    "\t-h\t\tdisplay this help message\n"
                    "\t-n\t\tuse index of entry instead of entry name\n"
                    "ENVIRONMENT\n"
                    "\tFFINDEX_MAX_INDEX_ENTRIES - allocate memory for this number of entries\n"
                    FFINDEX_COPYRIGHT,
                    basename(program_name));
}

int main(int argn, char **argv)
{
  int by_index = 0;
  int opt;
  char* list_filenames[4096];
  size_t list_filenames_index = 0;
  char* program_name = basename(argv[0]);
  while ((opt = getopt(argn, argv, "f:hn")) != -1)
  {
    switch (opt)
    {
      case 'f':
        list_filenames[list_filenames_index++] = optarg;
        break;
      case 'n':
        by_index = 1;
        break;
      case 'h':
        usage(program_name);
        return EXIT_SUCCESS;
      default:
        usage(program_name);
        return EXIT_FAILURE;
    }
  }
  if(argn < 3)
  {
    usage(argv[0]);
    return EXIT_FAILURE;
  }
  char *data_filename  = argv[optind++];
  char *index_filename = argv[optind++];

  ffindex_db_t * ffindex_db = calloc(1, sizeof(ffindex_db_t));
  ffindex_db->ffdata_filename = data_filename;
  ffindex_db->ffindex_filename = index_filename;
  ffindex_db->mode[0] = 'r';
  ffindex_db->num_max_entries = ffcount_lines(index_filename);

  ffindex_db = ffindex_index_db_open(ffindex_db);
  if(!ffindex_db)
  {
    fferror_print(__FILE__, __LINE__, "ffindex_index_db_open failed", argv[0]);
    return EXIT_FAILURE;
  }

  if(by_index)
  {
    for(int i = optind; i < argn; i++)
    {
      size_t index_n = atol(argv[i]) - 1; // offset from 0 but specify from 1

      ffindex_entry_t* entry = ffindex_get_entry_by_index(ffindex_db->ffindex, index_n);
      if(entry == NULL)
      {
        errno = ENOENT; 
        fferror_print(__FILE__, __LINE__, "ffindex_get entry index out of range", argv[i]);
      }
      else
      {
        char *filedata = ffindex_get_data_by_entry(ffindex_db->ffdata, entry);
        if(filedata == NULL)
        {
          errno = ENOENT; 
          fferror_print(__FILE__, __LINE__, "ffindex_get entry index out of range", argv[i]);
        }
        else
          fwrite(filedata, entry->length - 1, 1, stdout);
      }
    }
  }
  else // by name
  {
    // get names given by file
    if(list_filenames_index > 0)
    {
      for(int i = 0; i < list_filenames_index; i++)
      {
        FILE *list_file = fopen(list_filenames[i], "r");
        if( list_file == NULL) { perror(list_filenames[i]); return EXIT_FAILURE; }

        char name[PATH_MAX];
        while(fgets(name, PATH_MAX, list_file) != NULL)
        {
          ffindex_entry_t* entry = ffindex_get_entry_by_name(ffindex_db->ffindex, ffnchomp(name, strnlen(name, PATH_MAX)));
          if(entry == NULL)
          {
            errno = ENOENT; 
            fferror_print(__FILE__, __LINE__, "ffindex_get key not found in index", name);
          }
          else
          {
            char *filedata = ffindex_get_data_by_entry(ffindex_db->ffdata, entry);
            if(filedata == NULL)
            {
              errno = ENOENT; 
              fferror_print(__FILE__, __LINE__, "ffindex_get key not found in index", name);
            }
            else
              fwrite(filedata, entry->length - 1, 1, stdout);
          }
        }
      }
    }

    // get files given on command line
    for(int i = optind; i < argn; i++)
    {
      char *filename = argv[i];

      ffindex_entry_t* entry = ffindex_get_entry_by_name(ffindex_db->ffindex, filename);
      if(entry == NULL)
      {
        errno = ENOENT; 
        fferror_print(__FILE__, __LINE__, "ffindex_get key not found in index", filename);
      }
      else
      {
        char *filedata = ffindex_get_data_by_entry(ffindex_db->ffdata, entry);
        if(filedata == NULL)
        {
          errno = ENOENT; 
          fferror_print(__FILE__, __LINE__, "ffindex_get key not found in index", filename);
        }
        else
          fwrite(filedata, entry->length - 1, 1, stdout);
      }
    }

      /* Alternative code using (slower) ffindex_fopen */
      /*
         FILE *file = ffindex_fopen(data, index, filename);
         if(file == NULL)
         {
         errno = ENOENT; 
         fferror_print(__FILE__, __LINE__, "ffindex_fopen file not found in index", filename);
         }
         else
         {
         char line[LINE_MAX];
         while(fgets(line, LINE_MAX, file) != NULL)
         printf("%s", line);
         }
         */
  }

  ffindex_index_db_close(ffindex_db);

  return 0;
}

/* vim: ts=2 sw=2 et
 */