16 INT64 get_file_size(char *filename)
21 error = stat(filename, &sb);
28 md5_list_t *find_file_in_md5_list(unsigned char *base64_md5)
30 md5_list_t *md5_list_entry = G_md5_list_head;
32 while (md5_list_entry)
34 if (!memcmp(md5_list_entry->md5, base64_md5, 16))
35 return md5_list_entry;
37 md5_list_entry = md5_list_entry->next;
39 return NULL; /* Not found */
42 static int add_md5_entry(INT64 size, char *md5, char *path)
44 md5_list_t *new = NULL;
45 new = calloc(1, sizeof(*new));
50 new->full_path = path;
51 new->file_size = size;
55 G_md5_list_head = new;
56 G_md5_list_tail = new;
60 G_md5_list_tail->next = new;
61 G_md5_list_tail = new;
67 static int parse_md5_entry(char *md5_entry)
70 char *file_name = NULL;
78 file_name = &md5_entry[24];
80 if ('\n' == file_name[strlen(file_name) -1])
81 file_name[strlen(file_name) - 1] = 0;
83 file_size = get_file_size(file_name);
85 error = add_md5_entry(file_size, md5, file_name);
89 int parse_md5_file(char *filename)
96 file = fopen(filename, "rb");
99 fprintf(G_logfile, "Failed to open MD5 file %s, error %d!\n", filename, errno);
105 ret = fgets(buf, sizeof(buf), file);
108 error = parse_md5_entry(strdup(buf));
113 static int file_exists(char *path, INT64 *size)
118 error = stat(path, &sb);
119 if (!error && S_ISREG(sb.st_mode))
129 static int find_file_in_mirror(char *jigdo_match, char *jigdo_name,
130 char *match, INT64 *file_size, char **mirror_path)
132 match_list_t *entry = G_match_list_head;
137 if (!strcmp(entry->match, match))
139 sprintf(path, "%s/%s", entry->mirror_path, jigdo_name);
140 if (file_exists(path, file_size))
142 *mirror_path = strdup(path);
149 *mirror_path = jigdo_name;
154 /* DELIBERATELY do not sort these, or do anything clever with
155 insertion. The entries in the jigdo file should be in the same
156 order as the ones we'll want from the template. Simply add to the
157 end of the singly-linked list each time! */
158 static int add_file_entry(char *jigdo_entry)
161 char *file_name = NULL;
163 char *ptr = jigdo_entry;
164 char *base64_md5 = NULL;
166 char *jigdo_name = NULL;
168 /* Grab out the component strings from the entry in the jigdo file */
169 base64_md5 = jigdo_entry;
178 else if (':' == *ptr)
184 else if ('\n' == *ptr)
190 if (find_file_in_md5_list(base64_md5))
191 return 0; /* We already have an entry for this file; don't
192 * waste any more time on it */
194 /* else look for the file in the filesystem */
195 if (NULL == match || NULL == jigdo_name)
197 fprintf(G_logfile, "Could not parse malformed jigdo entry \"%s\"\n", jigdo_entry);
200 error = find_file_in_mirror(match, jigdo_name, match, &file_size, &file_name);
204 if (G_missing_filename)
205 add_md5_entry(MISSING, base64_md5, file_name);
208 fprintf(G_logfile, "Unable to find a file to match %s\n", file_name);
209 fprintf(G_logfile, "Abort!\n");
214 add_md5_entry(file_size, base64_md5, file_name);
220 int parse_jigdo_file(char *filename)
227 file = gzopen(filename, "rb");
230 fprintf(G_logfile, "Failed to open jigdo file %s, error %d!\n", filename, errno);
234 /* Find the [Parts] section of the jigdo file */
237 ret = gzgets(file, buf, sizeof(buf));
240 if (!strncmp(buf, "[Parts]", 7))
244 /* Now grab the individual file entries and build a list */
247 ret = gzgets(file, buf, sizeof(buf));
248 if (NULL == ret || !strcmp(buf, "\n"))
250 if (!strcmp(buf, "[") || !strcmp(buf, "#"))
252 error = add_file_entry(strdup(buf));