void init_config (const char *myname) { config.myname = mbasename(myname); config.handle = NULL; config.aur = 0; config.aur_foreign = 0; config.aur_upgrades = 0; config.colors = isatty(1); config.custom_out = 0; config.db_local = 0; config.db_sync = 0; config.escape = 0; config.filter = 0; config.get_res = 0; config.insecure = 0; config.is_file = 0; config.just_one = 0; config.list = 0; config.numbering = 0; config.op = 0; config.quiet = 0; config.query=ALL; config.show_size = 0; config.arch = NULL; config.aur_url = strdup (AUR_BASE_URL); config.configfile = strndup (CONFFILE, PATH_MAX); strcpy (config.delimiter, " "); config.dbpath = NULL; strcpy (config.format_out, ""); config.rootdir = NULL; config.sort = 0; }
int have_lock_dir(void) { struct stat stt; char buf[128]; if ( stat(P_LOCK, &stt) == 0) { snprintf(lockfile, sizeof(lockfile), "%s/LCK..%s", P_LOCK, mbasename(dial_tty, buf, sizeof(buf))); } else { fprintf(stderr, "Lock directory %s does not exist\n", P_LOCK); exit(-1); } return 1; }
/** Parse command-line arguments for each operation. * @param argc argc * @param argv argv * @return 0 on success, 1 on error */ static int parseargs(int argc, char *argv[]) { int opt; int option_index = 0; int result; const char *optstring = "DQRSTUVb:cdefghiklmnopqr:stuvwy"; static const struct option opts[] = { {"database", no_argument, 0, 'D'}, {"query", no_argument, 0, 'Q'}, {"remove", no_argument, 0, 'R'}, {"sync", no_argument, 0, 'S'}, {"deptest", no_argument, 0, 'T'}, /* used by makepkg */ {"upgrade", no_argument, 0, 'U'}, {"version", no_argument, 0, 'V'}, {"help", no_argument, 0, 'h'}, {"dbpath", required_argument, 0, OP_DBPATH}, {"cascade", no_argument, 0, OP_CASCADE}, {"changelog", no_argument, 0, OP_CHANGELOG}, {"clean", no_argument, 0, OP_CLEAN}, {"nodeps", no_argument, 0, OP_NODEPS}, {"deps", no_argument, 0, OP_DEPS}, {"explicit", no_argument, 0, OP_EXPLICIT}, {"groups", no_argument, 0, OP_GROUPS}, {"info", no_argument, 0, OP_INFO}, {"check", no_argument, 0, OP_CHECK}, {"list", no_argument, 0, OP_LIST}, {"foreign", no_argument, 0, OP_FOREIGN}, {"native", no_argument, 0, OP_NATIVE}, {"nosave", no_argument, 0, OP_NOSAVE}, {"owns", no_argument, 0, OP_OWNS}, {"file", no_argument, 0, OP_FILE}, {"print", no_argument, 0, OP_PRINT}, {"quiet", no_argument, 0, OP_QUIET}, {"root", required_argument, 0, OP_ROOT}, {"recursive", no_argument, 0, OP_RECURSIVE}, {"search", no_argument, 0, OP_SEARCH}, {"unrequired", no_argument, 0, OP_UNREQUIRED}, {"upgrades", no_argument, 0, OP_UPGRADES}, {"sysupgrade", no_argument, 0, OP_SYSUPGRADE}, {"unneeded", no_argument, 0, OP_UNNEEDED}, {"verbose", no_argument, 0, OP_VERBOSE}, {"downloadonly", no_argument, 0, OP_DOWNLOADONLY}, {"refresh", no_argument, 0, OP_REFRESH}, {"noconfirm", no_argument, 0, OP_NOCONFIRM}, {"confirm", no_argument, 0, OP_CONFIRM}, {"config", required_argument, 0, OP_CONFIG}, {"ignore", required_argument, 0, OP_IGNORE}, {"assume-installed", required_argument, 0, OP_ASSUMEINSTALLED}, {"debug", optional_argument, 0, OP_DEBUG}, {"force", no_argument, 0, OP_FORCE}, {"noprogressbar", no_argument, 0, OP_NOPROGRESSBAR}, {"noscriptlet", no_argument, 0, OP_NOSCRIPTLET}, {"ask", required_argument, 0, OP_ASK}, {"cachedir", required_argument, 0, OP_CACHEDIR}, {"asdeps", no_argument, 0, OP_ASDEPS}, {"logfile", required_argument, 0, OP_LOGFILE}, {"ignoregroup", required_argument, 0, OP_IGNOREGROUP}, {"needed", no_argument, 0, OP_NEEDED}, {"asexplicit", no_argument, 0, OP_ASEXPLICIT}, {"arch", required_argument, 0, OP_ARCH}, {"print-format", required_argument, 0, OP_PRINTFORMAT}, {"gpgdir", required_argument, 0, OP_GPGDIR}, {"dbonly", no_argument, 0, OP_DBONLY}, {"color", required_argument, 0, OP_COLOR}, {0, 0, 0, 0} }; /* parse operation */ while((opt = getopt_long(argc, argv, optstring, opts, &option_index)) != -1) { if(opt == 0) { continue; } else if(opt == '?') { /* unknown option, getopt printed an error */ return 1; } parsearg_op(opt, 0); } if(config->op == 0) { pm_printf(ALPM_LOG_ERROR, _("only one operation may be used at a time\n")); return 1; } if(config->help) { usage(config->op, mbasename(argv[0])); cleanup(0); } if(config->version) { version(); cleanup(0); } /* parse all other options */ optind = 1; while((opt = getopt_long(argc, argv, optstring, opts, &option_index)) != -1) { if(opt == 0) { continue; } else if(opt == '?') { /* this should have failed during first pass already */ return 1; } else if(parsearg_op(opt, 1) == 0) { /* opt is an operation */ continue; } switch(config->op) { case PM_OP_DATABASE: result = parsearg_database(opt); break; case PM_OP_QUERY: result = parsearg_query(opt); break; case PM_OP_REMOVE: result = parsearg_remove(opt); break; case PM_OP_SYNC: result = parsearg_sync(opt); break; case PM_OP_UPGRADE: result = parsearg_upgrade(opt); break; case PM_OP_DEPTEST: default: result = 1; break; } if(result == 0) { continue; } /* fall back to global options */ result = parsearg_global(opt); if(result != 0) { /* global option parsing failed, abort */ if(opt < OP_LONG_FLAG_MIN) { pm_printf(ALPM_LOG_ERROR, _("invalid option '-%c'\n"), opt); } else { pm_printf(ALPM_LOG_ERROR, _("invalid option '--%s'\n"), opts[option_index].name); } return result; } } while(optind < argc) { /* add the target to our target array */ pm_targets = alpm_list_add(pm_targets, strdup(argv[optind])); optind++; } switch(config->op) { case PM_OP_DATABASE: checkargs_database(); break; case PM_OP_DEPTEST: /* no conflicting options */ break; case PM_OP_SYNC: checkargs_sync(); break; case PM_OP_QUERY: checkargs_query(); break; case PM_OP_REMOVE: checkargs_remove(); break; case PM_OP_UPGRADE: checkargs_upgrade(); break; default: break; } return 0; }
static void shift_pacsave(alpm_handle_t *handle, const char *file) { DIR *dir = NULL; struct dirent *ent; struct stat st; regex_t reg; const char *basename; char *dirname; char oldfile[PATH_MAX]; char newfile[PATH_MAX]; char regstr[PATH_MAX]; unsigned long log_max = 0; size_t basename_len; dirname = mdirname(file); if(!dirname) { return; } basename = mbasename(file); basename_len = strlen(basename); snprintf(regstr, PATH_MAX, "^%s\\.pacsave\\.([[:digit:]]+)$", basename); if(regcomp(®, regstr, REG_EXTENDED | REG_NEWLINE) != 0) { goto cleanup; } dir = opendir(dirname); if(dir == NULL) { _alpm_log(handle, ALPM_LOG_ERROR, _("could not open directory: %s: %s\n"), dirname, strerror(errno)); goto cleanup; } while((ent = readdir(dir)) != NULL) { if(strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) { continue; } if(regexec(®, ent->d_name, 0, 0, 0) == 0) { unsigned long cur_log; cur_log = strtoul(ent->d_name + basename_len + strlen(".pacsave."), NULL, 10); if(cur_log > log_max) { log_max = cur_log; } } } /* Shift pacsaves */ unsigned long i; for(i = log_max + 1; i > 1; i--) { snprintf(oldfile, PATH_MAX, "%s.pacsave.%lu", file, i-1); snprintf(newfile, PATH_MAX, "%s.pacsave.%lu", file, i); rename(oldfile, newfile); } snprintf(oldfile, PATH_MAX, "%s.pacsave", file); if(stat(oldfile, &st) == 0) { snprintf(newfile, PATH_MAX, "%s.1", oldfile); rename(oldfile, newfile); } regfree(®); cleanup: free(dirname); closedir(dir); }
static int query_fileowner(alpm_list_t *targets) { int ret = 0; char path[PATH_MAX]; const char *root; size_t rootlen; alpm_list_t *t; alpm_db_t *db_local; /* This code is here for safety only */ if(targets == NULL) { pm_fprintf(stderr, ALPM_LOG_ERROR, _("no file was specified for --owns\n")); return 1; } /* Set up our root path buffer. We only need to copy the location of root in * once, then we can just overwrite whatever file was there on the previous * iteration. */ root = alpm_option_get_root(config->handle); rootlen = strlen(root); if(rootlen + 1 > PATH_MAX) { /* we are in trouble here */ pm_fprintf(stderr, ALPM_LOG_ERROR, _("path too long: %s%s\n"), root, ""); return 1; } strcpy(path, root); db_local = alpm_option_get_localdb(config->handle); for(t = targets; t; t = alpm_list_next(t)) { char *filename, *dname, *rpath; const char *bname; struct stat buf; alpm_list_t *i; int found = 0; filename = strdup(alpm_list_getdata(t)); if(lstat(filename, &buf) == -1) { /* if it is not a path but a program name, then check in PATH */ if(strchr(filename, '/') == NULL) { if(search_path(&filename, &buf) == -1) { pm_fprintf(stderr, ALPM_LOG_ERROR, _("failed to find '%s' in PATH: %s\n"), filename, strerror(errno)); ret++; free(filename); continue; } } else { pm_fprintf(stderr, ALPM_LOG_ERROR, _("failed to read file '%s': %s\n"), filename, strerror(errno)); ret++; free(filename); continue; } } if(S_ISDIR(buf.st_mode)) { pm_fprintf(stderr, ALPM_LOG_ERROR, _("cannot determine ownership of directory '%s'\n"), filename); ret++; free(filename); continue; } bname = mbasename(filename); dname = mdirname(filename); /* for files in '/', there is no directory name to match */ if(strcmp(dname, "") == 0) { rpath = NULL; } else { rpath = resolve_path(dname); if(!rpath) { pm_fprintf(stderr, ALPM_LOG_ERROR, _("cannot determine real path for '%s': %s\n"), filename, strerror(errno)); free(filename); free(dname); free(rpath); ret++; continue; } } free(dname); for(i = alpm_db_get_pkgcache(db_local); i && !found; i = alpm_list_next(i)) { alpm_pkg_t *info = alpm_list_getdata(i); alpm_filelist_t *filelist = alpm_pkg_get_files(info); size_t j; for(j = 0; j < filelist->count; j++) { const alpm_file_t *file = filelist->files + j; char *ppath, *pdname; const char *pkgfile = file->name; /* avoid the costly resolve_path usage if the basenames don't match */ if(strcmp(mbasename(pkgfile), bname) != 0) { continue; } /* for files in '/', there is no directory name to match */ if(!rpath) { print_query_fileowner(filename, info); found = 1; continue; } if(rootlen + 1 + strlen(pkgfile) > PATH_MAX) { pm_fprintf(stderr, ALPM_LOG_ERROR, _("path too long: %s%s\n"), root, pkgfile); } /* concatenate our file and the root path */ strcpy(path + rootlen, pkgfile); pdname = mdirname(path); ppath = resolve_path(pdname); free(pdname); if(ppath && strcmp(ppath, rpath) == 0) { print_query_fileowner(filename, info); found = 1; } free(ppath); } } if(!found) { pm_fprintf(stderr, ALPM_LOG_ERROR, _("No package owns %s\n"), filename); ret++; } free(filename); free(rpath); } return ret; }