1
0
mirror of git://sourceware.org/git/lvm2.git synced 2025-01-02 01:18:26 +03:00

systemid: Define file content more precisely.

In a file containing a system ID:
  Any whitespace at the start of a line is ignored;
  Blank lines are ignored;
  Any characters after a # are ignored along with the #.
  The system ID is obtained by processing the first line with
non-ignored characters.
  If further lines with non-ignored characters follow, a warning is
issued.
This commit is contained in:
Alasdair G Kergon 2015-02-23 20:49:15 +00:00
parent c2ed5feee5
commit e15b439bf3

View File

@ -84,31 +84,52 @@ char *system_id_from_string(struct cmd_context *cmd, const char *str)
static char *_read_system_id_from_file(struct cmd_context *cmd, const char *file)
{
char line[NAME_LEN + 1];
char *line = NULL;
size_t line_size;
char *start, *end;
char *system_id = NULL;
FILE *fp;
if (!file || !strlen(file) || !file[0])
return NULL;
return_NULL;
if (!(fp = fopen(file, "r"))) {
log_warn("WARNING: Error %d opening system_id_file %s", errno, file);
log_warn("WARNING: %s: fopen failed: %s", file, strerror(errno));
return NULL;
}
memset(line, 0, sizeof(line));
while (getline(&line, &line_size, fp) > 0) {
start = line;
while (fgets(line, NAME_LEN, fp)) {
if (line[0] == '#' || line[0] == '\n')
/* Ignore leading whitespace */
while (*start && isspace(*start))
start++;
/* Ignore rest of line after # */
if (!*start || *start == '#')
continue;
if (fclose(fp))
stack;
return system_id_from_string(cmd, line);
if (system_id) {
log_warn("WARNING: Ignoring extra line(s) in system ID file %s.", file);
break;
}
/* Remove any comments from end of line */
for (end = start; *end; start++)
if (*end == '#') {
*end = '\0';
break;
}
system_id = system_id_from_string(cmd, start);
}
free(line);
if (fclose(fp))
stack;
return NULL;
return system_id;
}
static char *_system_id_from_source(struct cmd_context *cmd, const char *source)