/* * Scan directory and return all file names matching the given prefix. */ int ni_scandir(const char *dirname, const char *pattern, ni_string_array_t *res) { struct dirent *dp; char *copy = NULL; const char *match_prefix = NULL; const char *match_suffix = NULL; unsigned int pfxlen, sfxlen; unsigned int count, rv = 0; DIR *dir; dir = opendir(dirname); if (dir == NULL) { ni_debug_readwrite("Unable to open directory '%s': %m", dirname); return 0; } if (pattern) { char *s; copy = xstrdup(pattern); if ((s = strchr(copy, '*')) == NULL) { ni_error("%s: bad pattern \"%s\"", __func__, pattern); goto out; } if (s != copy) match_prefix = copy; *s++ = '\0'; if (*s != '\0') match_suffix = s; } count = res->count; pfxlen = match_prefix? strlen(match_prefix) : 0; sfxlen = match_suffix? strlen(match_suffix) : 0; while ((dp = readdir(dir)) != NULL) { const char *name = dp->d_name; if (name[0] == '.') continue; if (pfxlen && strncmp(name, match_prefix, pfxlen)) continue; if (sfxlen != 0) { unsigned int namelen = strlen(name); if (namelen < pfxlen + sfxlen) continue; if (strcmp(name + namelen - sfxlen, match_suffix)) continue; } ni_string_array_append(res, name); } rv = res->count - count; out: closedir(dir); free(copy); return rv; }
/* * Copy file for backup */ int ni_backup_file_to(const char *srcpath, const char *backupdir) { const char *dstpath; if (!(dstpath = __ni_build_backup_path(srcpath, backupdir))) return -1; if (ni_mkdir_maybe(backupdir, 0700) < 0) return -1; if (access(dstpath, F_OK) == 0) { ni_debug_readwrite("%s(%s, %s): backup copy already exists", __FUNCTION__, srcpath, backupdir); return 0; } ni_debug_readwrite("%s(%s, %s)", __FUNCTION__, srcpath, backupdir); return ni_copy_file_path(srcpath, dstpath); }
ni_bool_t ni_ifsysctl_file_load(ni_var_array_t *vars, const char *filename) { if (!vars || ni_string_empty(filename)) return FALSE; ni_debug_readwrite("Reading sysctl file '%s'", filename); return __ni_sysctl_file_load(vars, filename, __ni_ifsysctl_vars_map); }
/* * Restore file from backup */ int ni_restore_file_from(const char *dstpath, const char *backupdir) { const char *srcpath; if (!(srcpath = __ni_build_backup_path(dstpath, backupdir))) return -1; if (access(srcpath, R_OK) < 0) { if (errno == ENOENT) { ni_debug_readwrite("%s(%s, %s): no backup copy to restore", __FUNCTION__, dstpath, backupdir); return 0; } ni_error("cannot restore %s from %s: %m", dstpath, srcpath); return -1; } ni_debug_readwrite("%s(%s, %s)", __FUNCTION__, dstpath, backupdir); if (ni_copy_file_path(srcpath, dstpath) < 0) return -1; unlink(srcpath); return 0; }
/* * This specifies sources of client configuration. * * The ifconfig source specifies the type, location and the * priority / load order of the interface configurations. * * <sources> * <ifconfig location="firmware:" /> * <ifconfig location="compat:" /> * <ifconfig location="wicked:/etc/wicked/ifconfig" /> * </sources> * */ static ni_bool_t __ni_config_parse_ifconfig_source(ni_string_array_t *sources, xml_node_t *node) { const char *attrval = NULL; unsigned int i; if ((attrval = xml_node_get_attr(node, "location")) != NULL && *attrval) { const char **p = __ni_ifconfig_source_types; for (i = 0; p[i]; i++) { if (!strncasecmp(attrval, p[i], ni_string_len(p[i]))) { ni_debug_readwrite("%s: Adding ifconfig %s", __func__, attrval); ni_string_array_append(sources, attrval); return TRUE; } } } ni_error("Unknown ifconfig location: %s", attrval); return FALSE; }
ni_iaid_map_t * ni_iaid_map_load(const char *filename) { ni_iaid_map_t *map; const char *type; ni_buffer_t buff; struct stat stb; ssize_t len; if (!(map = ni_iaid_map_new())) { ni_error("unable to allocate memory for iaid map: %m"); return NULL; } if (filename) { type = "given"; if (!ni_string_dup(&map->file, filename)) { ni_error("unable to copy %s iaid map file name (%s): %m", type, filename); goto failure; } if (!ni_iaid_map_open(map)) { ni_error("unable to open %s iaid map file name (%s): %m", type, map->file); goto failure; } } else { type = "default"; if (!ni_iaid_map_set_default_file(&map->file)) { ni_error("unable to construct %s iaid map file name: %m", type); goto failure; } if (!ni_iaid_map_open(map)) { ni_debug_readwrite("unable to open %s iaid map file name (%s): %m", type, map->file); type = "fallback"; if (!ni_iaid_map_set_fallback_file(&map->file)) { ni_error("unable to construct %s iaid map file name: %m", type); goto failure; } if (!ni_iaid_map_open(map)) { ni_error("unable to open iaid map file name (%s): %m", map->file); goto failure; } } } if (!ni_iaid_map_lock(map)) { ni_error("unable to lock %s iaid map file name (%s): %m", type, map->file); goto failure; } if (fstat(map->fd, &stb) < 0) stb.st_size = BUFSIZ; ni_buffer_init_dynamic(&buff, stb.st_size + 1); do { if (!ni_buffer_tailroom(&buff)) ni_buffer_ensure_tailroom(&buff, BUFSIZ); do { len = read(map->fd, ni_buffer_tail(&buff), ni_buffer_tailroom(&buff)); if (len > 0) ni_buffer_push_tail(&buff, len); } while (len < 0 && errno == EINTR); } while (len > 0); if (len < 0) { ni_error("unable to read %s iaid map file name (%s): %m", type, map->file); } else { map->doc = xml_document_from_buffer(&buff, map->file); ni_buffer_destroy(&buff); if (!map->doc) { map->doc = xml_document_new(); ni_warn("unable to parse %s iaid map file name (%s): %m", type, map->file); } return map; } failure: ni_iaid_map_free(map); return NULL; }