bool CONFIG::get_config_include_path(POOL_MEM &full_path, const char *config_dir) { bool found = false; if (m_config_include_dir) { /* * Set full_path to the initial part of the include path, * so it can be used as result, even on errors. * On success, full_path will be overwritten with the full path. */ full_path.strcpy(config_dir); path_append(full_path, m_config_include_dir); if (path_is_directory(full_path)) { m_config_dir = bstrdup(config_dir); /* * Set full_path to wildcard path. */ if (get_path_of_resource(full_path, NULL, NULL, NULL, true)) { m_use_config_include_dir = true; found = true; } } } return found; }
bool CONFIG::get_config_file(POOL_MEM &full_path, const char *config_dir, const char *config_filename) { bool found = false; if (!path_is_directory(config_dir)) { return false; } if (config_filename) { full_path.strcpy(config_dir); if (path_append(full_path, config_filename)) { if (path_exists(full_path)) { m_config_dir = bstrdup(config_dir); found = true; } } } return found; }
bool CONFIG::get_path_of_resource(POOL_MEM &path, const char *component, const char *resourcetype, const char *name, bool set_wildcards) { POOL_MEM rel_path(PM_FNAME); POOL_MEM directory(PM_FNAME); POOL_MEM resourcetype_lowercase(resourcetype); resourcetype_lowercase.toLower(); if (!component) { if (m_config_include_dir) { component = m_config_include_dir; } else { return false; } } if (resourcetype_lowercase.strlen() <= 0) { if (set_wildcards) { resourcetype_lowercase.strcpy("*"); } else { return false; } } if (!name) { if (set_wildcards) { name = "*"; } else { return false; } } path.strcpy(m_config_dir); rel_path.bsprintf(m_config_include_naming_format, component, resourcetype_lowercase.c_str(), name); path_append(path, rel_path); return true; }
void OUTPUT_FORMATTER::rewrap(POOL_MEM &string, int wrap) { char *p, *q; int open = 0; int charsinline = 0; POOL_MEM rewrap_string(PM_MESSAGE); /* * wrap < 0: no modification * wrap = 0: single line * wrap > 0: wrap line after x characters (if api==0) */ if (wrap < 0) { return; } /* * Allocate a wrap buffer that is big enough to hold the original * string + WRAP_EXTRA_BYTES. This means we can accommodate enough * line breaks and spaces to wrap the original string. */ rewrap_string.check_size(string.max_size() + WRAP_EXTRA_BYTES); /* * Walk the input buffer and copy the data to the wrap buffer * inserting line breaks as needed. */ q = rewrap_string.c_str(); p = string.c_str(); while (*p) { charsinline++; switch (*p) { case ' ': if (api == 0 && wrap > 0 && charsinline >= wrap && open <= 0 && *(p + 1) != '|') { *q++ = '\n'; *q++ = '\t'; charsinline = 0; } else { if (charsinline > 1) { *q++ = ' '; } } break; case '|': *q++ = '|'; if (api == 0 && wrap > 0 && open <= 0) { *q++ = '\n'; *q++ = '\t'; charsinline = 0; } break; case '[': case '<': open++; *q++ = *p; break; case ']': case '>': open--; *q++ = *p; break; case '\n': case '\t': if (charsinline > 1) { if (*(p + 1) != '\n' && *(p + 1) != '\t' && *(p + 1) != ' ') { *q++ = ' '; } } break; default: *q++ = *p; break; } p++; } *q = '\0'; string.strcpy(rewrap_string); }
/* * Returns false on error * true on OK, with full_path set to where config file should be */ bool CONFIG::find_config_path(POOL_MEM &full_path) { bool found = false; POOL_MEM config_dir; POOL_MEM config_path_file; if (!m_cf) { /* * No path is given, so use the defaults. */ found = get_config_file(full_path, get_default_configdir(), m_config_default_filename); if (!found) { config_path_file.strcpy(full_path); found = get_config_include_path(full_path, get_default_configdir()); } if (!found) { Jmsg2(NULL, M_ERROR, 0, _("Failed to read config file at the default locations " "\"%s\" (config file path) and \"%s\" (config include directory).\n"), config_path_file.c_str(), full_path.c_str()); } } else if (path_exists(m_cf)) { /* * Path is given and exists. */ if (path_is_directory(m_cf)) { found = get_config_file(full_path, m_cf, m_config_default_filename); if (!found) { config_path_file.strcpy(full_path); found = get_config_include_path(full_path, m_cf); } if (!found) { Jmsg3(NULL, M_ERROR, 0, _("Failed to find configuration files under directory \"%s\". " "Did look for \"%s\" (config file path) and \"%s\" (config include directory).\n"), m_cf, config_path_file.c_str(), full_path.c_str()); } } else { full_path.strcpy(m_cf); path_get_directory(config_dir, full_path); m_config_dir = bstrdup(config_dir.c_str()); found = true; } } else if (!m_config_default_filename) { /* * Compatibility with older versions. * If m_config_default_filename is not set, * m_cf may contain what is expected in m_config_default_filename. */ found = get_config_file(full_path, get_default_configdir(), m_cf); if (!found) { Jmsg2(NULL, M_ERROR, 0, _("Failed to find configuration files at \"%s\" and \"%s\".\n"), m_cf, full_path.c_str()); } } else { Jmsg1(NULL, M_ERROR, 0, _("Failed to read config file \"%s\"\n"), m_cf); } if (found) { set_env("BAREOS_CFGDIR", m_config_dir); } return found; }