path.c revision b5efdb8af40ea759a1ea584c1bc44ecc81dd00ce
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen/***
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen This file is part of systemd.
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen Copyright 2014 Lennart Poettering
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen systemd is free software; you can redistribute it and/or modify it
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen under the terms of the GNU Lesser General Public License as published by
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen the Free Software Foundation; either version 2.1 of the License, or
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen (at your option) any later version.
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen systemd is distributed in the hope that it will be useful, but
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen WITHOUT ANY WARRANTY; without even the implied warranty of
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen Lesser General Public License for more details.
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen You should have received a copy of the GNU Lesser General Public License
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen along with systemd; If not, see <http://www.gnu.org/licenses/>.
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen***/
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen#include <errno.h>
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen#include <getopt.h>
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen#include <stdio.h>
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen#include <stdlib.h>
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen#include "sd-path.h"
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen#include "alloc-util.h"
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen#include "log.h"
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen#include "macro.h"
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen#include "string-util.h"
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen#include "util.h"
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersenstatic const char *arg_suffix = NULL;
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersenstatic const char* const path_table[_SD_PATH_MAX] = {
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen [SD_PATH_TEMPORARY] = "temporary",
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen [SD_PATH_TEMPORARY_LARGE] = "temporary-large",
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen [SD_PATH_SYSTEM_BINARIES] = "system-binaries",
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen [SD_PATH_SYSTEM_INCLUDE] = "system-include",
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen [SD_PATH_SYSTEM_LIBRARY_PRIVATE] = "system-library-private",
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen [SD_PATH_SYSTEM_LIBRARY_ARCH] = "system-library-arch",
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen [SD_PATH_SYSTEM_SHARED] = "system-shared",
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen [SD_PATH_SYSTEM_CONFIGURATION_FACTORY] = "system-configuration-factory",
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen [SD_PATH_SYSTEM_STATE_FACTORY] = "system-state-factory",
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen [SD_PATH_SYSTEM_CONFIGURATION] = "system-configuration",
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen [SD_PATH_SYSTEM_RUNTIME] = "system-runtime",
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen [SD_PATH_SYSTEM_RUNTIME_LOGS] = "system-runtime-logs",
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen [SD_PATH_SYSTEM_STATE_PRIVATE] = "system-state-private",
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen [SD_PATH_SYSTEM_STATE_LOGS] = "system-state-logs",
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen [SD_PATH_SYSTEM_STATE_CACHE] = "system-state-cache",
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen [SD_PATH_SYSTEM_STATE_SPOOL] = "system-state-spool",
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen [SD_PATH_USER_BINARIES] = "user-binaries",
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen [SD_PATH_USER_LIBRARY_PRIVATE] = "user-library-private",
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen [SD_PATH_USER_LIBRARY_ARCH] = "user-library-arch",
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen [SD_PATH_USER_SHARED] = "user-shared",
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen [SD_PATH_USER_CONFIGURATION] = "user-configuration",
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen [SD_PATH_USER_RUNTIME] = "user-runtime",
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen [SD_PATH_USER_STATE_CACHE] = "user-state-cache",
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen [SD_PATH_USER] = "user",
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen [SD_PATH_USER_DOCUMENTS] = "user-documents",
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen [SD_PATH_USER_MUSIC] = "user-music",
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen [SD_PATH_USER_PICTURES] = "user-pictures",
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen [SD_PATH_USER_VIDEOS] = "user-videos",
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen [SD_PATH_USER_DOWNLOAD] = "user-download",
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen [SD_PATH_USER_PUBLIC] = "user-public",
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen [SD_PATH_USER_TEMPLATES] = "user-templates",
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen [SD_PATH_USER_DESKTOP] = "user-desktop",
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen [SD_PATH_SEARCH_BINARIES] = "search-binaries",
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen [SD_PATH_SEARCH_LIBRARY_PRIVATE] = "search-library-private",
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen [SD_PATH_SEARCH_LIBRARY_ARCH] = "search-library-arch",
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen [SD_PATH_SEARCH_SHARED] = "search-shared",
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen [SD_PATH_SEARCH_CONFIGURATION_FACTORY] = "search-configuration-factory",
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen [SD_PATH_SEARCH_STATE_FACTORY] = "search-state-factory",
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen [SD_PATH_SEARCH_CONFIGURATION] = "search-configuration",
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen};
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersenstatic int list_homes(void) {
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen uint64_t i = 0;
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen int r = 0;
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen for (i = 0; i < ELEMENTSOF(path_table); i++) {
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen _cleanup_free_ char *p = NULL;
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen int q;
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen q = sd_path_home(i, arg_suffix, &p);
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen if (q == -ENXIO)
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen continue;
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen if (q < 0) {
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen log_error_errno(r, "Failed to query %s: %m", path_table[i]);
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen r = q;
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen continue;
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen }
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen printf("%s: %s\n", path_table[i], p);
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen }
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen return r;
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen}
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersenstatic int print_home(const char *n) {
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen uint64_t i = 0;
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen int r;
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen for (i = 0; i < ELEMENTSOF(path_table); i++) {
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen if (streq(path_table[i], n)) {
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen _cleanup_free_ char *p = NULL;
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen r = sd_path_home(i, arg_suffix, &p);
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen if (r < 0)
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen return log_error_errno(r, "Failed to query %s: %m", n);
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen printf("%s\n", p);
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen return 0;
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen }
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen }
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen log_error("Path %s not known.", n);
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen return -EOPNOTSUPP;
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen}
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersenstatic void help(void) {
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen printf("%s [OPTIONS...] [NAME...]\n\n"
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen "Show system and user paths.\n\n"
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen " -h --help Show this help\n"
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen " --version Show package version\n"
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen " --suffix=SUFFIX Suffix to append to paths\n",
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen program_invocation_short_name);
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen}
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersenstatic int parse_argv(int argc, char *argv[]) {
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen enum {
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen ARG_VERSION = 0x100,
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen ARG_SUFFIX,
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen };
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen static const struct option options[] = {
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen { "help", no_argument, NULL, 'h' },
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen { "version", no_argument, NULL, ARG_VERSION },
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen { "suffix", required_argument, NULL, ARG_SUFFIX },
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen {}
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen };
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen int c;
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen assert(argc >= 0);
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen assert(argv);
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen while ((c = getopt_long(argc, argv, "h", options, NULL)) >= 0)
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen switch (c) {
case 'h':
help();
return 0;
case ARG_VERSION:
return version();
case ARG_SUFFIX:
arg_suffix = optarg;
break;
case '?':
return -EINVAL;
default:
assert_not_reached("Unhandled option");
}
return 1;
}
int main(int argc, char* argv[]) {
int r;
log_parse_environment();
log_open();
r = parse_argv(argc, argv);
if (r <= 0)
goto finish;
if (argc > optind) {
int i, q;
for (i = optind; i < argc; i++) {
q = print_home(argv[i]);
if (q < 0)
r = q;
}
} else
r = list_homes();
finish:
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}