mirror of
https://git.proxmox.com/git/mirror_lxc
synced 2025-08-09 11:20:59 +00:00
lxc-user-nic: improve cull_entries
Signed-off-by: Serge Hallyn <serge.hallyn@ubuntu.com> Acked-by: Stéphane Graber <stgraber@ubuntu.com>
This commit is contained in:
parent
82160e6f08
commit
03c2eb1563
@ -622,11 +622,19 @@ static bool get_nic_from_line(char *p, char **nic)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct entry_line {
|
||||||
|
char *start;
|
||||||
|
int len;
|
||||||
|
bool keep;
|
||||||
|
};
|
||||||
|
|
||||||
static bool cull_entries(int fd, char *me, char *t, char *br)
|
static bool cull_entries(int fd, char *me, char *t, char *br)
|
||||||
{
|
{
|
||||||
struct stat sb;
|
struct stat sb;
|
||||||
char *buf, *p, *e, *nic;
|
char *buf, *p, *e, *nic;
|
||||||
off_t len;
|
off_t len;
|
||||||
|
struct entry_line *entry_lines = NULL;
|
||||||
|
int i, n = 0;
|
||||||
|
|
||||||
nic = alloca(100);
|
nic = alloca(100);
|
||||||
|
|
||||||
@ -643,22 +651,36 @@ static bool cull_entries(int fd, char *me, char *t, char *br)
|
|||||||
p = buf;
|
p = buf;
|
||||||
e = buf + len;
|
e = buf + len;
|
||||||
while ((p = find_line(p, e, me, t, br)) != NULL) {
|
while ((p = find_line(p, e, me, t, br)) != NULL) {
|
||||||
|
struct entry_line *newe = realloc(entry_lines, n+1);
|
||||||
|
if (!newe) {
|
||||||
|
free(entry_lines);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
entry_lines = newe;
|
||||||
|
entry_lines[n].start = p;
|
||||||
|
entry_lines[n].len = get_eol(p) - entry_lines[n].start;
|
||||||
|
entry_lines[n].keep = true;
|
||||||
|
n++;
|
||||||
if (!get_nic_from_line(p, &nic))
|
if (!get_nic_from_line(p, &nic))
|
||||||
continue;
|
continue;
|
||||||
if (nic && !nic_exists(nic)) {
|
if (nic && !nic_exists(nic))
|
||||||
// copy from eol(p)+1..e to p
|
entry_lines[n-1].keep = false;
|
||||||
char *src = get_eol(p) + 1, *dest = p;
|
p += entry_lines[n-1].len + 1;
|
||||||
int diff = src - p;
|
|
||||||
while (src < e)
|
|
||||||
*(dest++) = *(src)++;
|
|
||||||
e -= diff;
|
|
||||||
} else
|
|
||||||
p = get_eol(p) + 1;
|
|
||||||
if (p >= e)
|
if (p >= e)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
p = buf;
|
||||||
|
for (i=0; i<n; i++) {
|
||||||
|
if (!entry_lines[i].keep)
|
||||||
|
continue;
|
||||||
|
memcpy(p, entry_lines[i].start, entry_lines[i].len);
|
||||||
|
p += entry_lines[i].len;
|
||||||
|
*p = '\n';
|
||||||
|
p++;
|
||||||
|
}
|
||||||
|
free(entry_lines);
|
||||||
munmap(buf, sb.st_size);
|
munmap(buf, sb.st_size);
|
||||||
if (ftruncate(fd, e-buf))
|
if (ftruncate(fd, p-buf))
|
||||||
fprintf(stderr, "Failed to set new file size\n");
|
fprintf(stderr, "Failed to set new file size\n");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user