static int read_loose_refs(struct strbuf *path, int name_offset, struct ref **tail) { DIR *dir = opendir(path->buf); struct dirent *de; struct { char **entries; int nr, alloc; } list; int i, pathlen; if (!dir) return -1; memset (&list, 0, sizeof(list)); while ((de = readdir(dir))) { if (is_dot_or_dotdot(de->d_name)) continue; ALLOC_GROW(list.entries, list.nr + 1, list.alloc); list.entries[list.nr++] = xstrdup(de->d_name); } closedir(dir); /* sort the list */ qsort(list.entries, list.nr, sizeof(char *), str_cmp); pathlen = path->len; strbuf_addch(path, '/'); for (i = 0; i < list.nr; i++, strbuf_setlen(path, pathlen + 1)) { strbuf_addstr(path, list.entries[i]); if (read_loose_refs(path, name_offset, tail)) { int fd = open(path->buf, O_RDONLY); char buffer[40]; struct ref *next; if (fd < 0) continue; next = alloc_ref(path->buf + name_offset); if (read_in_full(fd, buffer, 40) != 40 || get_sha1_hex(buffer, next->old_sha1)) { close(fd); free(next); continue; } close(fd); (*tail)->next = next; *tail = next; } } strbuf_setlen(path, pathlen); for (i = 0; i < list.nr; i++) free(list.entries[i]); free(list.entries); return 0; }
static struct ref *get_refs_via_rsync(struct transport *transport) { struct strbuf buf = STRBUF_INIT, temp_dir = STRBUF_INIT; struct ref dummy, *tail = &dummy; struct child_process rsync; const char *args[5]; int temp_dir_len; /* copy the refs to the temporary directory */ strbuf_addstr(&temp_dir, git_path("rsync-refs-XXXXXX")); if (!mkdtemp(temp_dir.buf)) die ("Could not make temporary directory"); temp_dir_len = temp_dir.len; strbuf_addstr(&buf, transport->url); strbuf_addstr(&buf, "/refs"); memset(&rsync, 0, sizeof(rsync)); rsync.argv = args; rsync.stdout_to_stderr = 1; args[0] = "rsync"; args[1] = (transport->verbose > 0) ? "-rv" : "-r"; args[2] = buf.buf; args[3] = temp_dir.buf; args[4] = NULL; if (run_command(&rsync)) die ("Could not run rsync to get refs"); strbuf_reset(&buf); strbuf_addstr(&buf, transport->url); strbuf_addstr(&buf, "/packed-refs"); args[2] = buf.buf; if (run_command(&rsync)) die ("Could not run rsync to get refs"); /* read the copied refs */ strbuf_addstr(&temp_dir, "/refs"); read_loose_refs(&temp_dir, temp_dir_len + 1, &tail); strbuf_setlen(&temp_dir, temp_dir_len); tail = &dummy; strbuf_addstr(&temp_dir, "/packed-refs"); insert_packed_refs(temp_dir.buf, &tail); strbuf_setlen(&temp_dir, temp_dir_len); if (remove_dir_recursively(&temp_dir, 0)) warning ("Error removing temporary directory %s.", temp_dir.buf); strbuf_release(&buf); strbuf_release(&temp_dir); return dummy.next; }