static int enter_bundle_dir(const char * basepath, const char *start_pobj_sha1, const char * end_pobj_sha1) { if (chdir(basepath) || chdir(BUNDLES_DIR)) return 0; // ensure creation of start_pobj_sha1 if (safe_create_dir(start_pobj_sha1) || chdir(start_pobj_sha1)) return 0; if (safe_create_dir(end_pobj_sha1) || chdir(end_pobj_sha1)) return 0; return 1; }
/** * initialize a given directory */ int gidit_init(const char *path) { int rc = 0; if ((rc = safe_create_dir(path))) return rc; chdir(path); // create these dirs if they don't exist if ((rc = safe_create_dir(BUNDLES_DIR)) == 0 && (rc = safe_create_dir(PUSHOBJ_DIR)) == 0) { return 0; } return rc; }
static void create_object_directory(void) { struct strbuf path = STRBUF_INIT; size_t baselen; strbuf_addstr(&path, get_object_directory()); baselen = path.len; safe_create_dir(path.buf, 1); strbuf_setlen(&path, baselen); strbuf_addstr(&path, "/pack"); safe_create_dir(path.buf, 1); strbuf_setlen(&path, baselen); strbuf_addstr(&path, "/info"); safe_create_dir(path.buf, 1); strbuf_release(&path); }
int init_db(const char *git_dir, const char *real_git_dir, const char *template_dir, unsigned int flags) { int reinit; int exist_ok = flags & INIT_DB_EXIST_OK; char *original_git_dir = real_pathdup(git_dir, 1); if (real_git_dir) { struct stat st; if (!exist_ok && !stat(git_dir, &st)) die(_("%s already exists"), git_dir); if (!exist_ok && !stat(real_git_dir, &st)) die(_("%s already exists"), real_git_dir); set_git_dir(real_path(real_git_dir)); git_dir = get_git_dir(); separate_git_dir(git_dir, original_git_dir); } else { set_git_dir(real_path(git_dir)); git_dir = get_git_dir(); } startup_info->have_repository = 1; /* Just look for `init.templatedir` and `core.hidedotfiles` */ git_config(git_init_db_config, NULL); safe_create_dir(git_dir, 0); init_is_bare_repository = is_bare_repository(); /* Check to see if the repository version is right. * Note that a newly created repository does not have * config file, so this will not fail. What we are catching * is an attempt to reinitialize new repository with an old tool. */ check_repository_format(); reinit = create_default_files(template_dir, original_git_dir); create_object_directory(); if (get_shared_repository()) { char buf[10]; /* We do not spell "group" and such, so that * the configuration can be read by older version * of git. Note, we use octal numbers for new share modes, * and compatibility values for PERM_GROUP and * PERM_EVERYBODY. */ if (get_shared_repository() < 0) /* force to the mode value */ xsnprintf(buf, sizeof(buf), "0%o", -get_shared_repository()); else if (get_shared_repository() == PERM_GROUP) xsnprintf(buf, sizeof(buf), "%d", OLD_PERM_GROUP); else if (get_shared_repository() == PERM_EVERYBODY) xsnprintf(buf, sizeof(buf), "%d", OLD_PERM_EVERYBODY); else BUG("invalid value for shared_repository"); git_config_set("core.sharedrepository", buf); git_config_set("receive.denyNonFastforwards", "true"); } if (!(flags & INIT_DB_QUIET)) { int len = strlen(git_dir); if (reinit) printf(get_shared_repository() ? _("Reinitialized existing shared Git repository in %s%s\n") : _("Reinitialized existing Git repository in %s%s\n"), git_dir, len && git_dir[len-1] != '/' ? "/" : ""); else printf(get_shared_repository() ? _("Initialized empty shared Git repository in %s%s\n") : _("Initialized empty Git repository in %s%s\n"), git_dir, len && git_dir[len-1] != '/' ? "/" : ""); } free(original_git_dir); return 0; }
static void copy_templates_1(struct strbuf *path, struct strbuf *template_path, DIR *dir) { size_t path_baselen = path->len; size_t template_baselen = template_path->len; struct dirent *de; /* Note: if ".git/hooks" file exists in the repository being * re-initialized, /etc/core-git/templates/hooks/update would * cause "git init" to fail here. I think this is sane but * it means that the set of templates we ship by default, along * with the way the namespace under .git/ is organized, should * be really carefully chosen. */ safe_create_dir(path->buf, 1); while ((de = readdir(dir)) != NULL) { struct stat st_git, st_template; int exists = 0; strbuf_setlen(path, path_baselen); strbuf_setlen(template_path, template_baselen); if (de->d_name[0] == '.') continue; strbuf_addstr(path, de->d_name); strbuf_addstr(template_path, de->d_name); if (lstat(path->buf, &st_git)) { if (errno != ENOENT) die_errno(_("cannot stat '%s'"), path->buf); } else exists = 1; if (lstat(template_path->buf, &st_template)) die_errno(_("cannot stat template '%s'"), template_path->buf); if (S_ISDIR(st_template.st_mode)) { DIR *subdir = opendir(template_path->buf); if (!subdir) die_errno(_("cannot opendir '%s'"), template_path->buf); strbuf_addch(path, '/'); strbuf_addch(template_path, '/'); copy_templates_1(path, template_path, subdir); closedir(subdir); } else if (exists) continue; else if (S_ISLNK(st_template.st_mode)) { struct strbuf lnk = STRBUF_INIT; if (strbuf_readlink(&lnk, template_path->buf, st_template.st_size) < 0) die_errno(_("cannot readlink '%s'"), template_path->buf); if (create_symlink(NULL, lnk.buf, path->buf)) die_errno(_("cannot symlink '%s' '%s'"), lnk.buf, path->buf); strbuf_release(&lnk); } else if (S_ISREG(st_template.st_mode)) { if (copy_file(path->buf, template_path->buf, st_template.st_mode)) die_errno(_("cannot copy '%s' to '%s'"), template_path->buf, path->buf); } else error(_("ignoring template %s"), template_path->buf); } }
static int create_default_files(const char *template_path, const char *original_git_dir) { struct stat st1; struct strbuf buf = STRBUF_INIT; char *path; char repo_version_string[10]; char junk[2]; int reinit; int filemode; struct strbuf err = STRBUF_INIT; /* Just look for `init.templatedir` */ git_config(git_init_db_config, NULL); /* * First copy the templates -- we might have the default * config file there, in which case we would want to read * from it after installing. * * Before reading that config, we also need to clear out any cached * values (since we've just potentially changed what's available on * disk). */ copy_templates(template_path); git_config_clear(); reset_shared_repository(); git_config(git_default_config, NULL); /* * We must make sure command-line options continue to override any * values we might have just re-read from the config. */ is_bare_repository_cfg = init_is_bare_repository; if (init_shared_repository != -1) set_shared_repository(init_shared_repository); /* * We would have created the above under user's umask -- under * shared-repository settings, we would need to fix them up. */ if (get_shared_repository()) { adjust_shared_perm(get_git_dir()); } /* * We need to create a "refs" dir in any case so that older * versions of git can tell that this is a repository. */ safe_create_dir(git_path("refs"), 1); adjust_shared_perm(git_path("refs")); if (refs_init_db(&err)) die("failed to set up refs db: %s", err.buf); /* * Create the default symlink from ".git/HEAD" to the "master" * branch, if it does not exist yet. */ path = git_path_buf(&buf, "HEAD"); reinit = (!access(path, R_OK) || readlink(path, junk, sizeof(junk)-1) != -1); if (!reinit) { if (create_symref("HEAD", "refs/heads/master", NULL) < 0) exit(1); } /* This forces creation of new config file */ xsnprintf(repo_version_string, sizeof(repo_version_string), "%d", GIT_REPO_VERSION); git_config_set("core.repositoryformatversion", repo_version_string); /* Check filemode trustability */ path = git_path_buf(&buf, "config"); filemode = TEST_FILEMODE; if (TEST_FILEMODE && !lstat(path, &st1)) { struct stat st2; filemode = (!chmod(path, st1.st_mode ^ S_IXUSR) && !lstat(path, &st2) && st1.st_mode != st2.st_mode && !chmod(path, st1.st_mode)); if (filemode && !reinit && (st1.st_mode & S_IXUSR)) filemode = 0; } git_config_set("core.filemode", filemode ? "true" : "false"); if (is_bare_repository()) git_config_set("core.bare", "true"); else { const char *work_tree = get_git_work_tree(); git_config_set("core.bare", "false"); /* allow template config file to override the default */ if (log_all_ref_updates == LOG_REFS_UNSET) git_config_set("core.logallrefupdates", "true"); if (needs_work_tree_config(original_git_dir, work_tree)) git_config_set("core.worktree", work_tree); } if (!reinit) { /* Check if symlink is supported in the work tree */ path = git_path_buf(&buf, "tXXXXXX"); if (!close(xmkstemp(path)) && !unlink(path) && !create_symlink(NULL, "testing", path) && !lstat(path, &st1) && S_ISLNK(st1.st_mode)) unlink(path); /* good */ else git_config_set("core.symlinks", "false"); /* Check if the filesystem is case-insensitive */ path = git_path_buf(&buf, "CoNfIg"); if (!access(path, F_OK)) git_config_set("core.ignorecase", "true"); probe_utf8_pathname_composition(); } strbuf_release(&buf); return reinit; }
/** * Initialize user directories, takes PGP */ int gidit_proj_init(FILE *fp, const char * basepath, unsigned int flags) { FILE * pgp_fp; struct strbuf pgp_key = STRBUF_INIT; struct strbuf proj_name = STRBUF_INIT; unsigned char sha1[20]; char pgp_sha1[41]; git_SHA_CTX c; strbuf_getline(&proj_name, fp, '\n'); if (proj_name.len == 0) { strbuf_release(&pgp_key); strbuf_release(&proj_name); return error("Error while reading project name\n"); } strbuf_getline(&pgp_key, fp, EOF); if (pgp_key.len == 0) { strbuf_release(&pgp_key); strbuf_release(&proj_name); return error("Error while reading pgp_key"); } // hash the pgp key git_SHA1_Init(&c); git_SHA1_Update(&c, pgp_key.buf, pgp_key.len); git_SHA1_Final(sha1, &c); // change dir to pushobjects dir if (chdir(basepath) || chdir(PUSHOBJ_DIR)) return error("Error going to pushobjects directory\n"); sprintf(pgp_sha1, "%s", sha1_to_hex(sha1)); if (safe_create_dir(pgp_sha1)) exit(1); chdir(pgp_sha1); // now ensure the project directories existence if (safe_create_dir(proj_name.buf)) exit(1); // if pgp key file already exists, not need to resave if (access("PGP", F_OK)) { // save the PGP key in there pgp_fp = fopen("PGP", "w"); if (!pgp_fp) die("Error while saving PGP key"); fwrite(pgp_key.buf, pgp_key.len, 1, pgp_fp); fclose(pgp_fp); } strbuf_release(&pgp_key); strbuf_release(&proj_name); return 0; }
/** * Read in projdir data, create projdir if it doesn't exist */ static int init_projdir(struct projdir* pd) { int len = 0; char * path = NULL; FILE * fp; if (access(pd->userdir, R_OK|W_OK)) return error("User does not exist"); if (safe_create_dir(pd->projdir)) return -1; // first get the pgp stuff path = (char *)malloc(strlen(pd->userdir) + 1 + 3); sprintf(path, "%s/PGP", pd->userdir); fp = fopen(path, "r"); free(path); if (!fp) return error("error while opening pgp file for reading"); fseek(fp, 0, SEEK_END); pd->pgp_len = ftell(fp); fseek(fp, 0, SEEK_SET); pd->pgp = (unsigned char*)malloc(pd->pgp_len); len = fread(pd->pgp, pd->pgp_len, 1, fp); fclose(fp); if (len != 1) return error("error while retrieving PGP info"); if (access(pd->userdir, W_OK|R_OK|X_OK) != 0) return error("Unknown user/pgp key, please initialize user first\n"); path = (char*)malloc(strlen(pd->projdir) + 1 + 4 + 1); sprintf(path, "%s/HEAD", pd->projdir); if (access(path, F_OK) == 0) { fp = fopen(path, "r"); if (!fp) die("Error while looking up head revision"); if (!read_sha1(fp, pd->head)) die("Error while reading sha1"); } else { fp = fopen(path, "w"); if (!fp) die("Error while looking up head revision"); memset(pd->head, '0', 40); pd->head[40] = '\0'; fprintf(fp, "%s\n", pd->head); } free(path); fclose(fp); return 0; }