diff --git a/zdev/include/misc.h b/zdev/include/misc.h index 2a97f426..ffdb2c11 100644 --- a/zdev/include/misc.h +++ b/zdev/include/misc.h @@ -157,10 +157,6 @@ char *misc_asprintf(const char *, ...); int misc_system(err_t, const char *, ...); bool misc_read_dir(const char *, struct util_list *, bool (*)(const char *, void *), void *); -bool path_exists(const char *); -bool file_exists(const char *); -bool file_writable(const char *); -bool dir_exists(const char *); bool file_is_devnode(const char *); exit_code_t remove_file(const char *); char *misc_read_text_file(const char *, int, err_t); diff --git a/zdev/src/ccw.c b/zdev/src/ccw.c index a4d56948..0de27333 100644 --- a/zdev/src/ccw.c +++ b/zdev/src/ccw.c @@ -14,6 +14,7 @@ #include #include "lib/util_base.h" +#include "lib/util_path.h" #include "attrib.h" #include "ccw.h" @@ -1165,15 +1166,10 @@ out: static void read_grouped(struct ccw_devinfo *info, const char *path) { - char *file_path; - - file_path = misc_asprintf("%s/group_device", path); - if (path_exists(file_path)) + if (util_path_exists("%s/group_device", path)) info->grouped = 1; else info->grouped = 0; - - free(file_path); } static void read_cutype(struct ccw_devinfo *info, const char *path) @@ -1229,7 +1225,7 @@ static struct ccw_devinfo *ccw_devinfo_read(struct ccw_devid *devid) id = ccw_devid_to_str(devid); path = path_get_ccw_device(NULL, id); - if (!dir_exists(path)) + if (!util_path_is_dir(path)) goto out; info->exists = 1; @@ -1354,7 +1350,7 @@ bool ccw_exists(const char *drv, const char *mod, const char *id) if (!path) return false; - rc = dir_exists(path); + rc = util_path_is_dir(path); free(path); return rc; @@ -1428,7 +1424,7 @@ static exit_code_t ccw_st_read_active(struct subtype *st, struct device *dev, state->definable = 0; path = path_get_ccw_device(drv, id); - if (path_exists(path)) { + if (util_path_exists(path)) { state->exists = 1; device_read_active_settings(dev, scope); } else @@ -1641,7 +1637,7 @@ static void ccw_st_add_errors(struct subtype *st, const char *id, if (!path) return; - if (!path_exists(path)) { + if (!util_path_exists(path)) { strlist_add(errors, "CCW device %s does not exist", id); goto out; } @@ -1662,9 +1658,7 @@ static void ccw_st_add_errors(struct subtype *st, const char *id, "paths", id); goto out; } - free(apath); - apath = misc_asprintf("%s/driver", path); - if (!path_exists(apath)) { + if (!util_path_exists("%s/driver", path)) { strlist_add(errors, "CCW device %s is not bound to a driver", id); goto out; diff --git a/zdev/src/ccwgroup.c b/zdev/src/ccwgroup.c index 94854f76..53d7eb09 100644 --- a/zdev/src/ccwgroup.c +++ b/zdev/src/ccwgroup.c @@ -12,6 +12,7 @@ #include #include "lib/util_base.h" +#include "lib/util_path.h" #include "attrib.h" #include "ccw.h" @@ -557,7 +558,7 @@ static bool read_full_id(struct ccwgroup_devid *devid_ptr, const char *drv, bool result = false; path = path_get_ccwgroup_device(drv, id); - if (!dir_exists(path)) + if (!util_path_is_dir(path)) goto out; memset(&devid, 0, sizeof(struct ccwgroup_devid)); @@ -625,7 +626,7 @@ static void ccwgroup_add_ids(const char *drv, const char *mod, path = path_get_ccwgroup_devices(drv); if (mod) module_try_load_once(mod, path); - if (dir_exists(path)) + if (util_path_is_dir(path)) path_for_each(path, get_ids_cb, &cb_data); free(path); } @@ -663,7 +664,7 @@ static exit_code_t ccwgroup_st_read_active(struct subtype *st, state->definable = 0; path = ccwgroup_get_dev_path_by_devid(drv, devid); - if (path_exists(path)) { + if (util_path_exists(path)) { state->exists = 1; device_read_active_settings(dev, scope); } else diff --git a/zdev/src/chzdev.c b/zdev/src/chzdev.c index 87b51134..f7fe3858 100644 --- a/zdev/src/chzdev.c +++ b/zdev/src/chzdev.c @@ -16,6 +16,7 @@ #include #include +#include "lib/util_path.h" #include "lib/zt_common.h" #include "attrib.h" @@ -2502,7 +2503,7 @@ static exit_code_t do_export(struct options *opts) info("Exporting configuration data to standard output\n"); } else { info("Exporting configuration data to %s\n", opts->export); - if (!path_exists(opts->export)) { + if (!util_path_exists(opts->export)) { rc = path_create(opts->export); if (rc) return rc; diff --git a/zdev/src/ctc_auto.c b/zdev/src/ctc_auto.c index f4e314da..b39a9dcd 100644 --- a/zdev/src/ctc_auto.c +++ b/zdev/src/ctc_auto.c @@ -9,6 +9,8 @@ #include +#include "lib/util_path.h" + #include "ccw.h" #include "ccwgroup.h" #include "ctc.h" @@ -125,13 +127,13 @@ static struct util_list *read_sorted_ctc_devinfos(void) /* Add CCW devices bound to the CTC CCW device driver. */ module_try_load_once(CTC_MOD_NAME, NULL); path = path_get_sys_bus_drv(CCW_BUS_NAME, CTC_CCWDRV_NAME); - if (dir_exists(path)) + if (util_path_is_dir(path)) path_for_each(path, add_cb, infos); free(path); /* Add CCW devices bound to the LCS CCW device driver. */ path = path_get_sys_bus_drv(CCW_BUS_NAME, LCS_CCWDRV_NAME); - if (dir_exists(path)) + if (util_path_is_dir(path)) path_for_each(path, add_cb, infos); free(path); diff --git a/zdev/src/device.c b/zdev/src/device.c index f7af6e17..9fa636fc 100644 --- a/zdev/src/device.c +++ b/zdev/src/device.c @@ -11,6 +11,8 @@ #include #include +#include "lib/util_path.h" + #include "attrib.h" #include "device.h" #include "devtype.h" @@ -480,7 +482,8 @@ void device_read_active_settings(struct device *dev, read_scope_t scope) a = attrib_find(st->dev_attribs, name); s = setting_list_apply_actual(dev->active.settings, a, name, value); - if (link || (scope == scope_all && !file_writable(path))) + if (link || (scope == scope_all && + !util_path_is_writable(path))) s->readonly = 1; if (link) free(link); diff --git a/zdev/src/devnode.c b/zdev/src/devnode.c index 6a1381c1..0169921e 100644 --- a/zdev/src/devnode.c +++ b/zdev/src/devnode.c @@ -15,6 +15,8 @@ #include #include +#include "lib/util_path.h" + #include "devnode.h" #include "misc.h" #include "path.h" @@ -230,7 +232,7 @@ static exit_code_t add_block_cb(const char *path, const char *filename, /* Add additional nodes. */ cb_data->prefix = filename; - if (dir_exists(path)) + if (util_path_is_dir(path)) path_for_each(path, add_part_cb, cb_data); return EXIT_OK; @@ -249,7 +251,7 @@ int devnode_add_block_from_sysfs(struct util_list *list, const char *path) cb_data.prefix = NULL; blkpath = misc_asprintf("%s/block", path); - if (dir_exists(blkpath)) + if (util_path_is_dir(blkpath)) path_for_each(blkpath, add_block_cb, &cb_data); free(blkpath); @@ -283,7 +285,7 @@ int devnode_add_net_from_sysfs(struct util_list *list, const char *path) cb_data.prefix = NULL; netpath = misc_asprintf("%s/net", path); - if (dir_exists(netpath)) + if (util_path_is_dir(netpath)) path_for_each(netpath, add_net_cb, &cb_data); free(netpath); diff --git a/zdev/src/generic_ccw.c b/zdev/src/generic_ccw.c index 8d5cbe48..9132d4a7 100644 --- a/zdev/src/generic_ccw.c +++ b/zdev/src/generic_ccw.c @@ -9,6 +9,8 @@ #include +#include "lib/util_path.h" + #include "attrib.h" #include "ccw.h" #include "ccwgroup.h" @@ -162,7 +164,7 @@ static void generic_ccw_st_add_devnodes(struct subtype *st, const char *id, cb_data.devnodes = devnodes; cb_data.id = id; path = path_get_sys_dev_char_devices(); - if (dir_exists(path)) + if (util_path_is_dir(path)) path_for_each(path, add_cb, &cb_data); free(path); } diff --git a/zdev/src/lcs_auto.c b/zdev/src/lcs_auto.c index 9a1b976a..0d0b87ea 100644 --- a/zdev/src/lcs_auto.c +++ b/zdev/src/lcs_auto.c @@ -9,6 +9,8 @@ #include +#include "lib/util_path.h" + #include "ccw.h" #include "ccwgroup.h" #include "ctc.h" @@ -129,13 +131,13 @@ static struct util_list *read_sorted_lcs_devinfos(void) /* Add CCW devices bound to the LCS CCW device driver. */ module_try_load_once(LCS_MOD_NAME, NULL); path = path_get_sys_bus_drv(CCW_BUS_NAME, LCS_CCWDRV_NAME); - if (dir_exists(path)) + if (util_path_is_dir(path)) path_for_each(path, add_cb, infos); free(path); /* Add CCW devices bound to the CTC CCW device driver. */ path = path_get_sys_bus_drv(CCW_BUS_NAME, CTC_CCWDRV_NAME); - if (dir_exists(path)) + if (util_path_is_dir(path)) path_for_each(path, add_cb, infos); free(path); diff --git a/zdev/src/misc.c b/zdev/src/misc.c index f815d6c6..f01acaec 100644 --- a/zdev/src/misc.c +++ b/zdev/src/misc.c @@ -889,69 +889,6 @@ void ptrlist_move(struct util_list *to, struct util_list *from, util_list_add_tail(to, node); } -/* Check if a path exists. */ -bool path_exists(const char *path) -{ - struct stat s; - - debug("Checking if file exists: %s\n", path); - if (stat(path, &s) != 0) - return false; - - return true; -} - -/* Check if a file exists. */ -bool file_exists(const char *path) -{ - struct stat s; - - debug("Checking if regular file exists: %s\n", path); - if (stat(path, &s) != 0) - return false; - - if (!S_ISREG(s.st_mode)) - return false; - - return true; -} - -/* Check if a file is writable. */ -bool file_writable(const char *path) -{ - struct stat s; - - debug("Checking if regular file is writable: %s\n", path); - if (stat(path, &s) != 0) - return false; - - if (!S_ISREG(s.st_mode)) - return false; - - if (!(s.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH))) - return false; - - return true; -} - -/* Check if a directory exists. */ -bool dir_exists(const char *path) -{ - bool result = false; - struct stat s; - - debug("Checking if directory exists: %s\n", path); - if (stat(path, &s) != 0) - goto out; - if (!S_ISDIR(s.st_mode)) - goto out; - result = true; -out: - debug("Result: %d\n", result); - - return result; -} - /* Check if file is a block or character special file. */ bool file_is_devnode(const char *path) { diff --git a/zdev/src/modprobe.c b/zdev/src/modprobe.c index b35d8812..45ffb14e 100644 --- a/zdev/src/modprobe.c +++ b/zdev/src/modprobe.c @@ -14,6 +14,8 @@ #include #include +#include "lib/util_path.h" + #include "attrib.h" #include "misc.h" #include "modprobe.h" @@ -376,7 +378,7 @@ exit_code_t modprobe_read_settings(const char *path, const char *mod, struct modprobe_file *mf; exit_code_t rc; - if (!file_exists(path)) { + if (!util_path_is_reg_file(path)) { *settings = NULL; return EXIT_OK; } @@ -398,7 +400,7 @@ exit_code_t modprobe_write_settings(const char *path, const char *mod, exit_code_t rc; unsigned long lines; - if (file_exists(path)) { + if (util_path_is_reg_file(path)) { rc = modprobe_read(path, &mf); if (rc) return rc; @@ -414,7 +416,7 @@ exit_code_t modprobe_write_settings(const char *path, const char *mod, lines = util_list_len(&mf->lines); if (lines == 0 || (lines == 1 && find_chzdev_comment(mf))) { /* Do not write empty files. */ - if (file_exists(path)) + if (util_path_is_reg_file(path)) rc = remove_file(path); } else rc = modprobe_write(mf); diff --git a/zdev/src/module.c b/zdev/src/module.c index 3dbdb7d7..09acb86e 100644 --- a/zdev/src/module.c +++ b/zdev/src/module.c @@ -12,6 +12,8 @@ #include #include +#include "lib/util_path.h" + #include "attrib.h" #include "misc.h" #include "module.h" @@ -34,7 +36,7 @@ bool module_loaded(const char *mod) char *path = path_get_sys_module(mod); bool rc; - rc = dir_exists(path); + rc = util_path_is_dir(path); free(path); return rc; @@ -229,7 +231,7 @@ void module_try_load_once(const char *mod, const char *path) } else tried_loading = strlist_new(); strlist_add(tried_loading, mod); - if (path && path_exists(path)) + if (path && util_path_exists(path)) return; if (module_loaded(mod)) return; @@ -275,7 +277,7 @@ bool module_set_params(const char *mod, struct setting_list *settings) return false; } path = path_get_sys_module_param(mod, s->name); - result = file_writable(path); + result = util_path_is_writable(path); free(path); if (!result) { /* Sysfs file is not writable. */ diff --git a/zdev/src/qeth_auto.c b/zdev/src/qeth_auto.c index 0b278e93..d5b47a11 100644 --- a/zdev/src/qeth_auto.c +++ b/zdev/src/qeth_auto.c @@ -11,6 +11,8 @@ #include #include +#include "lib/util_path.h" + #include "ccw.h" #include "ccwgroup.h" #include "device.h" @@ -277,7 +279,7 @@ static struct util_list *read_sorted_qeth_devinfos(void) /* Get CHPID information for all devices bound to the QETH driver. */ infos = ptrlist_new(); path = path_get_sys_bus_drv(CCW_BUS_NAME, QETH_CCWDRV_NAME); - if (dir_exists(path)) + if (util_path_is_dir(path)) path_for_each(path, add_cb, infos); free(path); diff --git a/zdev/src/root.c b/zdev/src/root.c index 6f85a2ba..7f0d39ff 100644 --- a/zdev/src/root.c +++ b/zdev/src/root.c @@ -9,6 +9,8 @@ #include +#include "lib/util_path.h" + #include "device.h" #include "devtype.h" #include "misc.h" @@ -69,7 +71,7 @@ exit_code_t root_check(void) "required.\n"); /* Check if script is available. */ - if (!file_exists(PATH_ROOT_SCRIPT)) + if (!util_path_is_reg_file(PATH_ROOT_SCRIPT)) goto out; /* Ask for confirmation. */ diff --git a/zdev/src/scsi.c b/zdev/src/scsi.c index 55a130bf..38f69a3b 100644 --- a/zdev/src/scsi.c +++ b/zdev/src/scsi.c @@ -12,6 +12,8 @@ #include #include +#include "lib/util_path.h" + #include "misc.h" #include "path.h" #include "scsi.h" @@ -253,7 +255,7 @@ static struct util_list *read_scsi_zfcp_list(void) list = ptrlist_new(); path = path_get_sys_bus_dev("scsi", NULL); - if (dir_exists(path)) + if (util_path_is_dir(path)) path_for_each(path, add_ids_cb, list); free(path); diff --git a/zdev/src/select.c b/zdev/src/select.c index a4a8cd56..3fe3b3d8 100644 --- a/zdev/src/select.c +++ b/zdev/src/select.c @@ -10,6 +10,8 @@ #include #include +#include "lib/util_path.h" + #include "blkinfo.h" #include "ccw.h" #include "device.h" @@ -622,7 +624,7 @@ exit_code_t select_by_path(struct select_opts *select, struct ptrlist_node *p; exit_code_t rc; - if (!path_exists(path)) { + if (!util_path_exists(path)) { err_t_print(err, "Path not found: %s\n", path); return EXIT_DEVICE_NOT_FOUND; } diff --git a/zdev/src/udev.c b/zdev/src/udev.c index ee70becd..b58717e6 100644 --- a/zdev/src/udev.c +++ b/zdev/src/udev.c @@ -13,6 +13,8 @@ #include #include +#include "lib/util_path.h" + #include "attrib.h" #include "ccw.h" #include "device.h" @@ -391,7 +393,7 @@ exit_code_t udev_remove_rule(const char *type, const char *id) exit_code_t rc = EXIT_OK; path = path_get_udev_rule(type, id); - if (file_exists(path)) + if (util_path_is_reg_file(path)) rc = remove_file(path); free(path); diff --git a/zdev/src/udev_ccw.c b/zdev/src/udev_ccw.c index 3f745611..2aedf825 100644 --- a/zdev/src/udev_ccw.c +++ b/zdev/src/udev_ccw.c @@ -13,6 +13,8 @@ #include #include +#include "lib/util_path.h" + #include "attrib.h" #include "ccw.h" #include "device.h" @@ -33,7 +35,7 @@ bool udev_ccw_exists(const char *type, const char *id) return false; path = path_get_udev_rule(type, normid); - rc = file_exists(path); + rc = util_path_is_reg_file(path); free(path); free(normid); @@ -150,7 +152,7 @@ exit_code_t udev_ccw_write_device(struct device *dev) path = path_get_udev_rule(type, id); debug("Writing %s udev rule file %s\n", type, path); - if (!path_exists(path)) { + if (!util_path_exists(path)) { rc = path_create(path); if (rc) goto out; @@ -243,7 +245,7 @@ exit_code_t udev_ccw_write_cio_ignore(const char *id_list) if (!*id_list) { /* Empty id_list string - remove file. */ - if (!file_exists(path)) { + if (!util_path_is_reg_file(path)) { /* Already removed. */ goto out; } @@ -256,7 +258,7 @@ exit_code_t udev_ccw_write_cio_ignore(const char *id_list) goto out; debug("Writing cio-ignore udev rule file %s\n", path); - if (!path_exists(path)) { + if (!util_path_exists(path)) { rc = path_create(path); if (rc) goto out; diff --git a/zdev/src/udev_ccwgroup.c b/zdev/src/udev_ccwgroup.c index 2b426690..7b433e7b 100644 --- a/zdev/src/udev_ccwgroup.c +++ b/zdev/src/udev_ccwgroup.c @@ -13,6 +13,8 @@ #include #include +#include "lib/util_path.h" + #include "attrib.h" #include "ccwgroup.h" #include "device.h" @@ -53,7 +55,7 @@ bool udev_ccwgroup_exists(const char *type, const char *id) path = get_rule_path(type, id); if (!path) return false; - rc = file_exists(path); + rc = util_path_is_reg_file(path); free(path); return rc; @@ -215,7 +217,7 @@ exit_code_t udev_ccwgroup_write_device(struct device *dev) list = setting_list_get_sorted(dev->persistent.settings); debug("Writing %s udev rule file %s\n", type, path); - if (!path_exists(path)) { + if (!util_path_exists(path)) { rc = path_create(path); if (rc) goto out; @@ -364,7 +366,7 @@ void udev_ccwgroup_add_device_ids(const char *type, struct util_list *list) cb_data.prefix = misc_asprintf("%s-%s-", UDEV_PREFIX, type); cb_data.ids = list; - if (dir_exists(path)) + if (util_path_is_dir(path)) path_for_each(path, get_ids_cb, &cb_data); free(cb_data.prefix); @@ -382,7 +384,7 @@ exit_code_t udev_ccwgroup_remove_rule(const char *type, const char *id) return EXIT_INVALID_ID; path = path_get_udev_rule(type, partial_id); - if (file_exists(path)) + if (util_path_is_reg_file(path)) rc = remove_file(path); free(path); free(partial_id); diff --git a/zdev/src/udev_zfcp_lun.c b/zdev/src/udev_zfcp_lun.c index 99b2eabc..f34d750b 100644 --- a/zdev/src/udev_zfcp_lun.c +++ b/zdev/src/udev_zfcp_lun.c @@ -14,6 +14,8 @@ #include #include +#include "lib/util_path.h" + #include "attrib.h" #include "device.h" #include "misc.h" @@ -376,7 +378,7 @@ void udev_zfcp_lun_add_device_ids(struct util_list *list) cb_data.list = list; path = path_get_udev_rules(); - if (dir_exists(path)) + if (util_path_is_dir(path)) path_for_each(path, lun_cb, &cb_data); free(path); @@ -497,7 +499,7 @@ static exit_code_t write_luns_rule(const char *path, struct util_list *list) return EXIT_INTERNAL_ERROR; hba_id = ccw_devid_to_str(&node->id.fcp_dev); debug("Writing FCP LUN udev rule file %s\n", path); - if (!path_exists(path)) { + if (!util_path_exists(path)) { rc = path_create(path); if (rc) goto out; @@ -614,7 +616,7 @@ static exit_code_t update_lun_rule(const char *id, struct device_state *state) /* Get previous rule data. */ luns = zfcp_lun_node_list_new(); - exists = file_exists(path); + exists = util_path_is_reg_file(path); if (exists) udev_read_zfcp_lun_rule(path, luns); diff --git a/zdev/src/zfcp_lun.c b/zdev/src/zfcp_lun.c index ed7a02b1..0c266821 100644 --- a/zdev/src/zfcp_lun.c +++ b/zdev/src/zfcp_lun.c @@ -13,6 +13,8 @@ #include #include +#include "lib/util_path.h" + #include "attrib.h" #include "ccw.h" #include "device.h" @@ -430,7 +432,7 @@ static exit_code_t zfcp_lun_st_read_active(struct subtype *st, /* Check for FC unit. */ fc_path = path_get_zfcp_lun_dev(dev->devid); - fc_exists = path_exists(fc_path); + fc_exists = util_path_exists(fc_path); free(fc_path); /* Check for SCSI device. */ @@ -478,7 +480,7 @@ static exit_code_t zfcp_lun_add(struct device *dev) /* Check if LUN already exists. */ fcp_dev_id = ccw_devid_to_str(&devid->fcp_dev); lunpath = path_get_zfcp_lun_dev(devid); - if (dir_exists(lunpath)) { + if (util_path_is_dir(lunpath)) { hctl = scsi_hctl_from_zfcp_lun_devid(devid); if (!hctl) goto check_failed; @@ -486,7 +488,7 @@ static exit_code_t zfcp_lun_add(struct device *dev) } portpath = path_get_zfcp_port_dev(devid); - if (!dir_exists(portpath)) { + if (!util_path_is_dir(portpath)) { delayed_err("Target port not found\n"); rc = EXIT_ZFCP_WWPN_NOT_FOUND; goto out; @@ -681,14 +683,14 @@ static exit_code_t zfcp_lun_st_device_undefine(struct subtype *st, remove_lun: path = path_get_zfcp_lun_dev(devid); - if (!path_exists(path)) + if (!util_path_exists(path)) goto out; free(path); path = NULL; /* Remove FCP LUN. */ devpath = path_get_zfcp_port_dev(devid); - if (!dir_exists(devpath)) { + if (!util_path_is_dir(devpath)) { rc = EXIT_ZFCP_WWPN_NOT_FOUND; goto out; } @@ -779,7 +781,7 @@ static bool zfcp_lun_fc_lun_exists(const char *id) if (zfcp_lun_parse_devid(&devid, id, err_ignore) != EXIT_OK) return false; path = path_get_zfcp_lun_dev(&devid); - result = path_exists(path); + result = util_path_exists(path); free(path); return result; @@ -910,7 +912,7 @@ static void add_sg_from_sysfs(struct util_list *list, const char *path) char *sgpath; sgpath = misc_asprintf("%s/scsi_generic", path); - if (dir_exists(sgpath)) + if (util_path_is_dir(sgpath)) path_for_each(sgpath, add_sg_cb, list); free(sgpath); }