/* * Creates dotdir (in homedir) if it doesn't exist * * returns 0 on success, -1 otherwise */ int touch_dotdir(void) { char *homedir; char *filename; if (!(homedir = get_homedir())) { fprintf(stderr, "Warning: Couldn't get home dir.\n"); return -1; } if (asprintf(&filename, "%s/." PACKAGE, homedir) < 0) { fprintf(stderr, "Warning: Couldn't allocate memory for configuration directory.\n"); return -1; } if (touch_dir(filename) < 0) { if (debug) fprintf(stderr, "Warning: Can't reach configuration directory.\n"); return -1; } free(filename); return 0; }
/* * Loads a filelist from a dir. * * Also chowns all files in the dir if chown is != 0. */ filelist_t * filelist_find_by_dir(filelist_t *l, char *base, char *path, chowninfo_t *chown) { DIR *dh; char buf[1024], *group; struct dirent *dent; struct stat st; filelist_t *tmp; group = ht_get(get_context(), PROPERTY_GROUP); sprintf(buf, "%s/%s", base, path); dh = opendir(buf); if (!dh) return l; while ((dent = readdir(dh))) { if ((!strcmp(dent->d_name, ".")) || (!strcmp(dent->d_name, ".."))) continue; sprintf(buf, "%s/%s/%s", base, path, dent->d_name); if (stat(buf, &st) == -1) continue; // chown file chowninfo_apply_to_file(buf, chown); if (S_ISDIR(st.st_mode)) { // touch dir touch_dir(buf); if (strlen(path) > 0) sprintf(buf, "%s/%s", path, dent->d_name); else strcpy(buf, dent->d_name); // find recursively. l = filelist_find_by_dir(l, base, buf, chown); } else { // touch file touch_file(buf); tmp = (filelist_t*)malloc(sizeof(filelist_t)); if (strlen(path) > 0) sprintf(tmp->file, "%s/%s", path, dent->d_name); else strcpy(tmp->file, dent->d_name); memcpy(&tmp->st, &st, sizeof(st)); tmp->next = l; l = tmp; } } closedir(dh); return l; }
int pre_handler(int argc, char *argv[]) { hashtable_t *env, *cfg; strlist_t *groups; char *dest_section, *destpath; struct stat st; char source[1024], destination[1024], *tmp, *group; char buf[1024]; int rc; env = get_context(); cfg = get_config(); pre_log("START", "%s %s %s", ht_get(env, PROPERTY_USER), argv[1], argv[2]); // set etcdir for the pwd functions if (tmp = ht_get(cfg, PROPERTY_ETCDIR)) pwd_set_etcdir(tmp); if (tmp = ht_get(cfg, PROPERTY_TEXT_HEAD)) printf(tmp); groups = user_find_groups(ht_get(env, PROPERTY_USER)); if (!groups) quit(" * Error finding your groups, go bug sysop!\n"); if (argc < 2) { printf(USAGE); show_groupdirs(groups); quit(0); } // check if someone are trying to fool us. if (strchr(argv[1], '/')) quit(" * You cant give paths in releasename ('/' not allowed)!\n"); char *sourcebis = getcwd(NULL, 0); // check if we are in a position to pre. group = group_find_by_dir(groups, sourcebis); if (!group) { printf(" * You are not in the group-dir of any of your groups.\n\n"); show_groupdirs(groups); quit(0); } pre_log("GROUP", "%s %s", sourcebis, group); printf(" * Looks like this is going to be a %s pre..\n", group); ht_put(env, PROPERTY_GROUP, group); // check if we have chosen a valid destination for our pre. dest_section = section_find_by_name(group, argc > 2 ? argv[2] : 0); if (!dest_section) { show_groupdirs(groups); quit(0); } printf(" * Destination for pre will be the %s section..\n", dest_section); ht_put(env, "section", dest_section); destpath = section_expand_path(dest_section); ht_put(env, "RESOLVEDDESTINATION", destpath); strcpy(source, sourcebis); strcat(source, "/"); strcat(source, argv[1]); // check if source dir is okay. if ((stat(source, &st) == -1) || !S_ISDIR(st.st_mode)) { sprintf(source, " * Hm, '%s' doesnt exist or isnt a dir ?\n", argv[1]); quit(source); } // touch the source. touch_dir(source); // check if destination dir exists. sprintf(destination, "%s/%s", destpath, argv[1]); rc = stat(destination, &st); // try rename if requested if ((rc == 0) && (argc > 3) && (!strcasecmp(argv[3], "force"))) { sprintf(buf, "%s_TRADING", destination); rc = rename(destination, buf); if (rc == 0) printf(" + Renamed existing to %s_TRADING ..\n", argv[1]); else printf(" ! Failed rename existing to %s_TRADING ..\n", argv[1]); } // check if destination exists. if (stat(destination, &st) == -1) pre(dest_section, destination, source, argv[1], group, argv); else { sprintf(source, " * Hm destination already exists. You're too late with pre!\n + Use SITE PRE %s %s FORCE to force pre.\n (this will rename the existing dir, which you can then nuke or wipe afterwards!)\n"); quit(source); } // log DONE: "<preuser>" "<pregroup>" "<release>" "<destinationdir>" pre_log("DONE", "\"%s\" \"%s\" \"%s\" \"%s\"", ht_get(env, PROPERTY_USER), group, argv[1], destpath); return 0; }