static void bz_git_make_slug(struct cork_buffer *dest, const char *url, const char *commit) { struct cork_path *path; cork_hash hash; /* First grab the basename of the URL. */ path = cork_path_new(url); cork_path_set_basename(path); cork_buffer_append_string(dest, cork_path_get(path)); cork_path_free(path); /* Then remove any trailing ".git" extension. */ if (dest->size >= 4) { const char *extension = &cork_buffer_char(dest, dest->size - 4); if (strcmp(extension, ".git") == 0) { cork_buffer_truncate(dest, dest->size - 4); } } /* Then calculate the hash of the full URL and commit, and append that to * ensure we have a unique slug. */ hash = 0x48f2a642; /* hash of "git" */ hash = cork_stable_hash_buffer(hash, url, strlen(url)); hash = cork_stable_hash_buffer(hash, commit, strlen(commit)); cork_buffer_append_printf(dest, "-%08" PRIx32, hash); }
static int cork_walk_one_directory(struct cork_dir_walker *w, struct cork_buffer *path, size_t root_path_size) { DIR *dir = NULL; struct dirent *entry; size_t dir_path_size; rip_check_posix(dir = opendir(path->buf)); cork_buffer_append(path, "/", 1); dir_path_size = path->size; errno = 0; while ((entry = readdir(dir)) != NULL) { struct stat info; /* Skip the "." and ".." entries */ if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) { continue; } /* Stat the directory entry */ cork_buffer_append_string(path, entry->d_name); ei_check_posix(stat(path->buf, &info)); /* If the entry is a subdirectory, recurse into it. */ if (S_ISDIR(info.st_mode)) { int rc = cork_dir_walker_enter_directory (w, path->buf, path->buf + root_path_size, path->buf + dir_path_size); if (rc != CORK_SKIP_DIRECTORY) { ei_check(cork_walk_one_directory(w, path, root_path_size)); ei_check(cork_dir_walker_leave_directory (w, path->buf, path->buf + root_path_size, path->buf + dir_path_size)); } } else if (S_ISREG(info.st_mode)) { ei_check(cork_dir_walker_file (w, path->buf, path->buf + root_path_size, path->buf + dir_path_size)); } /* Remove this entry name from the path buffer. */ cork_buffer_truncate(path, dir_path_size); /* We have to reset errno to 0 because of the ambiguous way * readdir uses a return value of NULL. Other functions may * return normally yet set errno to a non-zero value. dlopen * on Mac OS X is an ogreish example. Since an error readdir * is indicated by returning NULL and setting errno to indicate * the error, then we need to reset it to zero before each call. * We shall assume, perhaps to our great misery, that functions * within this loop do proper error checking and act accordingly. */ errno = 0; } /* Check errno immediately after the while loop terminates */ if (CORK_UNLIKELY(errno != 0)) { cork_system_error_set(); goto error; } /* Remove the trailing '/' from the path buffer. */ cork_buffer_truncate(path, dir_path_size - 1); rii_check_posix(closedir(dir)); return 0; error: if (dir != NULL) { rii_check_posix(closedir(dir)); } return -1; }