Exemplo n.º 1
0
void Destroy::exec(const Options &opts)
{
    m_tag = opts.get_arg("tag");
    if (m_tag.empty()) {
        throw Exception("Tag must be specified");
    }

    std::string age_str = opts.get_arg("age");
    if (age_str.empty()) {
        throw Exception("Age must be specified");
    }

    system_clock::duration age_dur;
    if (!parse_age(age_dur, age_str)) {
        throw Exception("Invalid age format: " + age_str);
    }
    m_age = duration_cast<seconds>(age_dur).count();

    m_recursive = opts.get("recursive");
    m_defer = opts.get("defer");
    m_dry_run = opts.get("dry_run");
    m_verbose = opts.get_count("verbose");

    system_clock::time_point now = system_clock::now();
    m_now = duration_cast<seconds>(now.time_since_epoch()).count();

    if (m_verbose) {
        std::cout << format_time(now, "%Y.%m.%d %H:%M:%S", false)
            << " cleaning up\n"
            << "    tag           : " << m_tag << "\n"
            << "    age           : " << age_str << "\n";
    }

    m_datasets.clear();
    for (auto &i : opts.ops()) {
        find(i);
    }

    // Sort found datasets by timestamp.
    m_datasets.sort([] (const Dataset &a, const Dataset &b) {
        if (a.name < b.name)
            return true;
        if (a.name > b.name)
            return false;

        if (a.timestamp < b.timestamp)
            return true;

        return false;
    });

    for (auto &i : m_datasets) {
        destroy(i.name);
    }

    if (m_verbose) {
        std::cout << std::endl;
    }
}
Exemplo n.º 2
0
Arquivo: list.c Projeto: mniip/atheme
static void
process_parvarray(struct list_option *opts, size_t optsize, int parc, char *parv[])
{
	int i;
	size_t j;

	for (i = 0; i < parc; i++)
	{
		for (j = 0; j < optsize; j++)
		{
			if (!strcasecmp(opts[j].option, parv[i]))
			{
				switch(opts[j].opttype)
				{
				case OPT_BOOL:
					*opts[j].optval.boolval = true;
					break;
				case OPT_INT:
					if (i + 1 < parc)
					{
						*opts[j].optval.intval = atoi(parv[i + 1]);
						i++;
					}
					break;
				case OPT_STRING:
					if (i + 1 < parc)
					{
						*opts[j].optval.strval = parv[i + 1];
						i++;
					}
					break;
				case OPT_FLAG:
					*opts[j].optval.flagval |= opts[j].flag;
					break;
				case OPT_AGE:
					if (i + 1 < parc)
					{
						*opts[j].optval.ageval = parse_age(parv[i + 1]);
						i++;
					}
					break;
				default:
					break;
				}
			}
		}
	}
}
Exemplo n.º 3
0
static bool process_parvarray(sourceinfo_t *si, list_option_t *opts, size_t optsize, int parc, char *parv[])
{
	int i;
	size_t j;
	bool found;

	for (i = 0; i < parc; i++)
	{
		found = false;
		for (j = 0; j < optsize; j++)
		{
			if (!strcasecmp(opts[j].option, parv[i]))
			{
				switch(opts[j].opttype)
				{
				case OPT_BOOL:
					*opts[j].optval.boolval = true;
					break;
				case OPT_INT:
					if (i + 1 < parc)
					{
						*opts[j].optval.intval = atoi(parv[i + 1]);
						i++;
					} else {
						command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, opts[j].option);
						return false;
					}
					break;
				case OPT_STRING:
					if (i + 1 < parc)
					{
						*opts[j].optval.strval = parv[i + 1];
						i++;
					} else {
						command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, opts[j].option);
						return false;
					}
					break;
				case OPT_FLAG:
					*opts[j].optval.flagval |= opts[j].flag;
					break;
				case OPT_AGE:
					if (i + 1 < parc)
					{
						*opts[j].optval.ageval = parse_age(parv[i + 1]);
						i++;
					} else {
						command_fail(si, fault_needmoreparams, STR_INSUFFICIENT_PARAMS, opts[j].option);
						return false;
					}
					break;
				default:
					break;
				}
				found = true;
				break;
			}
		}
		if (!found) {
			command_fail(si, fault_badparams, _("\2%s\2 is not a recognized LIST criterion"), parv[i]);
			return false;
		}
	}

	return true;
}
Exemplo n.º 4
0
void parse_options(int argc, char* argv[]) { // {{{

	int c;
	int digit_optind = 0;

	struct config_t* cfg = config_init(config());

	int _did_reset_discc = 0;

	while (1) {
		int this_option_optind = optind ? optind : 1;
		int option_index = 0;
		static struct option long_options[] = {
			{"0",               no_argument,       0, '0' },
			{"minage",          required_argument, 0, 'm' },
			{"eval",            required_argument, 0, 'e' },
			{"hard-link",       no_argument,       0, 'H' },
			{"symbolic-link",   no_argument,       0, 'L' },
			{"show-merge-0",    required_argument, 0, 'O' },
			{"show-merge",      required_argument, 0, 'o' },
			{"dry-run",         no_argument,       0, 'n' },
			{"nice",            required_argument, 0, 'N' },
			{"ionice",          required_argument, 0, 'i' },
			{"cgroup",          required_argument, 0, 'c' },
			{"verbose",         no_argument,       0, 'v' },
			{"jobs",            required_argument, 0, 'j' },
			{"read",            required_argument, 0, 'R' },
			{"help",            no_argument,       0, '?' },
			{0,                 0,                 0,  0  }
		};

		c = getopt_long(argc, argv, "0m:e:HLO:o:nN:i:c:t:vj:R:h",
				long_options, &option_index);
		if (c == -1)
			break;

		switch (c) {
			case '0':
				path_source_set(&cfg->flags, PATHSOURCE_STDIN0);
				break;

			case 'm':
				cfg->minage = parse_age(optarg);
				break;

			case 'e':
				if (!_did_reset_discc) {
					_did_reset_discc = 1;
					cfg->discriminantc = 0;
				}
				discriminantv_parse(optarg, cfg->discriminantv + cfg->discriminantc++);
				break;

			case 'H':
				link_type_set_hard(cfg->flags);
				break;

			case 'L':
				link_type_set_symb(cfg->flags);
				break;

			case 'O':
				cfg->report_file = strdup(optarg);
				cfg->report_separator = '\0';
				break;

			case 'o':
				cfg->report_file = strdup(optarg);
				cfg->report_separator = '\n';
				break;

			case 'n':
				cfg->flags |= CONFIG_DRYRUN;
				break;

			case 'N':
				if (sscanf(optarg, "%d", &cfg->nice) != 1)
					fatal("Invalid nice level (expecting an positive integer value).\n");

				if (cfg->nice < 0)
					fatal("Only positive integers are allowed in nice value.\n");

				break;

			case 'i':
				cfg->ionice = ionice_parse(optarg);
				break;

			case 'c':
				cfg->cgroups = realloc(cfg->cgroups, ++cfg->cgroupc * sizeof(char*));
				cfg->cgroups[cfg->cgroupc-1] = strdup(optarg);
				break;

			case 'v':
				cfg->verbose++;
				break;

			case 'R':
				parse_read(optarg, &cfg->read_policy, &cfg->bufsize);
				break;

			case '?':
			case 'h':
				help();
				exit(0);

			default:
				fatal("Unknown switch %s\n.", argv[option_index]);
		}
	}

	discriminantv_post_parse(cfg->discriminantv, cfg->discriminantc);

	if (cfg->report_file) {
		if (!strcmp(cfg->report_file, "-"))
			cfg->report_fd = 0;
		else
			cfg->report_fd = open(cfg->report_file, O_CREAT | O_RDWR | O_TRUNC, 0640);

		if (cfg->report_fd < 0)
			fatal("Could not open file %s: %s.\n", cfg->report_file, strerror(errno));
	}

	if (optind < argc) {
		if ((cfg->flags & PATHSOURCE_ARGS) == 0)
			fatal("Extra arguments found.\n");

		while (optind < argc)
			add_path(argv[optind++]);

	} else if ((cfg->flags & PATHSOURCE_STDIN) == 0) {
		path_source_set(&cfg->flags, PATHSOURCE_STDIN);

	}
} // }}}