16 INT64 get_file_size(char *filename)
21 error = stat(filename, &sb);
28 time_t get_file_mtime(char *filename)
33 error = stat(filename, &sb);
40 md5_list_t *find_file_in_md5_list(unsigned char *base64_md5)
42 md5_list_t *md5_list_entry = G_md5_list_head;
44 while (md5_list_entry)
46 if (!memcmp(md5_list_entry->md5, base64_md5, 16))
47 return md5_list_entry;
49 md5_list_entry = md5_list_entry->next;
51 return NULL; /* Not found */
54 static int add_md5_entry(INT64 size, char *md5, char *path)
56 md5_list_t *new = NULL;
57 new = calloc(1, sizeof(*new));
62 new->full_path = path;
63 new->file_size = size;
67 G_md5_list_head = new;
68 G_md5_list_tail = new;
72 G_md5_list_tail->next = new;
73 G_md5_list_tail = new;
79 static int parse_md5_entry(char *md5_entry)
82 char *file_name = NULL;
90 file_name = &md5_entry[24];
92 if ('\n' == file_name[strlen(file_name) -1])
93 file_name[strlen(file_name) - 1] = 0;
95 file_size = get_file_size(file_name);
97 error = add_md5_entry(file_size, md5, file_name);
101 int parse_md5_file(char *filename)
108 file = fopen(filename, "rb");
111 fprintf(G_logfile, "Failed to open MD5 file %s, error %d!\n", filename, errno);
117 ret = fgets(buf, sizeof(buf), file);
120 error = parse_md5_entry(strdup(buf));
125 static int file_exists(char *path, INT64 *size)
130 error = stat(path, &sb);
131 if (!error && S_ISREG(sb.st_mode))
141 static int find_file_in_mirror(char *jigdo_match, char *jigdo_name,
142 char *match, INT64 *file_size, char **mirror_path)
144 match_list_t *entry = G_match_list_head;
149 if (!strcmp(entry->match, match))
151 sprintf(path, "%s/%s", entry->mirror_path, jigdo_name);
152 if (file_exists(path, file_size))
154 *mirror_path = strdup(path);
161 *mirror_path = jigdo_name;
166 /* DELIBERATELY do not sort these, or do anything clever with
167 insertion. The entries in the jigdo file should be in the same
168 order as the ones we'll want from the template. Simply add to the
169 end of the singly-linked list each time! */
170 static int add_file_entry(char *jigdo_entry)
173 char *file_name = NULL;
175 char *ptr = jigdo_entry;
176 char *base64_md5 = NULL;
178 char *jigdo_name = NULL;
180 /* Grab out the component strings from the entry in the jigdo file */
181 base64_md5 = jigdo_entry;
190 else if (':' == *ptr)
196 else if ('\n' == *ptr)
202 if (find_file_in_md5_list(base64_md5))
203 return 0; /* We already have an entry for this file; don't
204 * waste any more time on it */
206 /* else look for the file in the filesystem */
207 if (NULL == match || NULL == jigdo_name)
209 fprintf(G_logfile, "Could not parse malformed jigdo entry \"%s\"\n", jigdo_entry);
212 error = find_file_in_mirror(match, jigdo_name, match, &file_size, &file_name);
216 if (G_missing_filename)
217 add_md5_entry(MISSING, base64_md5, file_name);
220 fprintf(G_logfile, "Unable to find a file to match %s\n", file_name);
221 fprintf(G_logfile, "Abort!\n");
226 add_md5_entry(file_size, base64_md5, file_name);
232 int parse_jigdo_file(char *filename)
239 file = gzopen(filename, "rb");
242 fprintf(G_logfile, "Failed to open jigdo file %s, error %d!\n", filename, errno);
246 /* Find the [Parts] section of the jigdo file */
249 ret = gzgets(file, buf, sizeof(buf));
252 if (!strncmp(buf, "[Parts]", 7))
256 /* Now grab the individual file entries and build a list */
259 ret = gzgets(file, buf, sizeof(buf));
260 if (NULL == ret || !strcmp(buf, "\n"))
262 if (!strcmp(buf, "[") || !strcmp(buf, "#"))
264 error = add_file_entry(strdup(buf));