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, md5_list_t **md5_list_head)
42 md5_list_t *md5_list_entry = *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, md5_list_t **md5_list_head)
56 md5_list_t *new = NULL;
57 static md5_list_t *md5_list_tail = NULL;
59 new = calloc(1, sizeof(*new));
64 new->full_path = path;
65 new->file_size = size;
74 md5_list_tail->next = new;
81 static int parse_md5_entry(char *md5_entry, md5_list_t **md5_list_head)
84 char *file_name = NULL;
92 file_name = &md5_entry[24];
94 if ('\n' == file_name[strlen(file_name) -1])
95 file_name[strlen(file_name) - 1] = 0;
97 file_size = get_file_size(file_name);
99 error = add_md5_entry(file_size, md5, file_name, md5_list_head);
103 int parse_md5_file(char *filename, md5_list_t **md5_list_head)
110 file = fopen(filename, "rb");
113 jd_log(0, "Failed to open MD5 file %s, error %d!\n", filename, errno);
119 ret = fgets(buf, sizeof(buf), file);
122 error = parse_md5_entry(strdup(buf), md5_list_head);
127 static int file_exists(char *path, INT64 *size)
132 error = stat(path, &sb);
133 if (!error && S_ISREG(sb.st_mode))
143 static int find_file_in_mirror(match_list_t *match_list_head, char *jigdo_match, char *jigdo_name,
144 char *match, INT64 *file_size, char **mirror_path)
146 match_list_t *entry = match_list_head;
151 if (!strcmp(entry->match, match))
153 sprintf(path, "%s/%s", entry->mirror_path, jigdo_name);
154 if (file_exists(path, file_size))
156 *mirror_path = strdup(path);
163 *mirror_path = jigdo_name;
168 /* DELIBERATELY do not sort these, or do anything clever with
169 insertion. The entries in the jigdo file should be in the same
170 order as the ones we'll want from the template. Simply add to the
171 end of the singly-linked list each time! */
172 static int add_file_entry(char *jigdo_entry, match_list_t *match_list_head,
173 md5_list_t **md5_list_head, char *missing_filename)
176 char *file_name = NULL;
178 char *ptr = jigdo_entry;
179 char *base64_md5 = NULL;
181 char *jigdo_name = NULL;
183 /* Grab out the component strings from the entry in the jigdo file */
184 base64_md5 = jigdo_entry;
193 else if (':' == *ptr)
199 else if ('\n' == *ptr)
205 if (find_file_in_md5_list(base64_md5, md5_list_head))
206 return 0; /* We already have an entry for this file; don't
207 * waste any more time on it */
209 /* else look for the file in the filesystem */
210 if (NULL == match || NULL == jigdo_name)
212 jd_log(0, "Could not parse malformed jigdo entry \"%s\"\n", jigdo_entry);
215 error = find_file_in_mirror(match_list_head, match, jigdo_name, match, &file_size, &file_name);
219 if (missing_filename)
220 add_md5_entry(MISSING, base64_md5, file_name, md5_list_head);
223 jd_log(0, "Unable to find a file to match %s\n", file_name);
224 jd_log(0, "Abort!\n");
229 add_md5_entry(file_size, base64_md5, file_name, md5_list_head);
233 int parse_jigdo_file(char *filename, md5_list_t **md5_list_head, match_list_t *match_list_head, char *missing_filename)
240 file = gzopen(filename, "rb");
243 jd_log(0, "Failed to open jigdo file %s, error %d!\n", filename, errno);
247 /* Find the [Parts] section of the jigdo file */
250 ret = gzgets(file, buf, sizeof(buf));
253 if (!strncmp(buf, "[Parts]", 7))
257 /* Now grab the individual file entries and build a list */
260 ret = gzgets(file, buf, sizeof(buf));
261 if (NULL == ret || !strcmp(buf, "\n"))
263 if (!strcmp(buf, "[") || !strcmp(buf, "#"))
265 error = add_file_entry(strdup(buf), match_list_head, md5_list_head, missing_filename);