static int openvzWriteConfigParam(const char * conf_file, const char *param, const char *value) { char * temp_file = NULL; int temp_fd = -1; FILE *fp; char *line = NULL; size_t line_size = 0; if (virAsprintf(&temp_file, "%s.tmp", conf_file)<0) { virReportOOMError(); return -1; } fp = fopen(conf_file, "r"); if (fp == NULL) goto error; temp_fd = open(temp_file, O_WRONLY | O_CREAT | O_TRUNC, 0644); if (temp_fd == -1) { goto error; } while (1) { if (getline(&line, &line_size, fp) <= 0) break; if (!(STRPREFIX(line, param) && line[strlen(param)] == '=')) { if (safewrite(temp_fd, line, strlen(line)) != strlen(line)) goto error; } } if (safewrite(temp_fd, param, strlen(param)) < 0 || safewrite(temp_fd, "=\"", 2) < 0 || safewrite(temp_fd, value, strlen(value)) < 0 || safewrite(temp_fd, "\"\n", 2) < 0) goto error; if (VIR_FCLOSE(fp) < 0) goto error; if (VIR_CLOSE(temp_fd) < 0) goto error; if (rename(temp_file, conf_file) < 0) goto error; VIR_FREE(line); return 0; error: VIR_FREE(line); VIR_FORCE_FCLOSE(fp); VIR_FORCE_CLOSE(temp_fd); if (temp_file) unlink(temp_file); VIR_FREE(temp_file); return -1; }
static int hostsfileWrite(const char *path, dnsmasqDhcpHost *hosts, unsigned int nhosts) { char *tmp; FILE *f; bool istmp = true; unsigned int i; int rc = 0; if (nhosts == 0) return rc; if (virAsprintf(&tmp, "%s.new", path) < 0) return ENOMEM; if (!(f = fopen(tmp, "w"))) { istmp = false; if (!(f = fopen(path, "w"))) { rc = errno; goto cleanup; } } for (i = 0; i < nhosts; i++) { if (fputs(hosts[i].host, f) == EOF || fputc('\n', f) == EOF) { rc = errno; VIR_FORCE_FCLOSE(f); if (istmp) unlink(tmp); goto cleanup; } } if (VIR_FCLOSE(f) == EOF) { rc = errno; goto cleanup; } if (istmp) { if (rename(tmp, path) < 0) { rc = errno; unlink(tmp); goto cleanup; } if (unlink(tmp) < 0) { rc = errno; goto cleanup; } } cleanup: VIR_FREE(tmp); return rc; }
static int64_t read_stat (const char *path) { char str[64]; int64_t r; int i; FILE *fp; fp = fopen (path, "r"); if (!fp) return -1; /* read, but don't bail out before closing */ i = fread (str, 1, sizeof str - 1, fp); if (VIR_FCLOSE(fp) != 0 /* disk error */ || i < 1) /* ensure we read at least one byte */ return -1; str[i] = '\0'; /* make sure the string is nul-terminated */ if (xstrtoint64 (str, 10, &r) == -1) return -1; return r; }
static int64_t read_stat(const char *path) { char str[64]; int64_t r; size_t i; FILE *fp; fp = fopen(path, "r"); if (!fp) return -1; /* read, but don't bail out before closing */ i = fread(str, 1, sizeof(str) - 1, fp); if (VIR_FCLOSE(fp) != 0 /* disk error */ || i < 1) /* ensure we read at least one byte */ return -1; str[i] = '\0'; /* make sure the string is nul-terminated */ if (virStrToLong_ll(str, NULL, 10, (long long *) &r) < 0) return -1; return r; }
static int hostsfileWrite(const char *path, dnsmasqDhcpHost *hosts, unsigned int nhosts) { char *tmp, *content = NULL; FILE *f; bool istmp = true; int rc = 0; /* even if there are 0 hosts, create a 0 length file, to allow * for runtime addition. */ if (virAsprintf(&tmp, "%s.new", path) < 0) return -ENOMEM; if (!(f = fopen(tmp, "w"))) { istmp = false; if (!(f = fopen(path, "w"))) { rc = -errno; goto cleanup; } } if (!(content = dnsmasqDhcpHostsToString(hosts, nhosts))) { rc = -ENOMEM; goto cleanup; } if (fputs(content, f) == EOF) { rc = -errno; VIR_FORCE_FCLOSE(f); if (istmp) unlink(tmp); goto cleanup; } if (VIR_FCLOSE(f) == EOF) { rc = -errno; goto cleanup; } if (istmp && rename(tmp, path) < 0) { rc = -errno; unlink(tmp); goto cleanup; } cleanup: VIR_FREE(content); VIR_FREE(tmp); return rc; }
static int openvz_copyfile(char* from_path, char* to_path) { char *line = NULL; size_t line_size = 0; FILE *fp; int copy_fd; int bytes_read; fp = fopen(from_path, "r"); if (fp == NULL) return -1; copy_fd = open(to_path, O_WRONLY | O_CREAT | O_TRUNC, 0644); if (copy_fd == -1) { VIR_FORCE_FCLOSE(fp); return -1; } while (1) { if (getline(&line, &line_size, fp) <= 0) break; bytes_read = strlen(line); if (safewrite(copy_fd, line, bytes_read) != bytes_read) goto error; } if (VIR_FCLOSE(fp) < 0) goto error; if (VIR_CLOSE(copy_fd) < 0) goto error; VIR_FREE(line); return 0; error: VIR_FREE(line); VIR_FORCE_FCLOSE(fp); VIR_FORCE_CLOSE(copy_fd); return -1; }
/* Do actual checking for UUID presence in conf file, * assign if not present. */ int openvzSetDefinedUUID(int vpsid, unsigned char *uuid) { char *conf_file; char uuidstr[VIR_UUID_STRING_BUFLEN]; FILE *fp = NULL; int ret = -1; if (uuid == NULL) return -1; if (openvzLocateConfFile(vpsid, &conf_file, "conf") < 0) return -1; if (openvzGetVPSUUID(vpsid, uuidstr, sizeof(uuidstr))) goto cleanup; if (uuidstr[0] == 0) { fp = fopen(conf_file, "a"); /* append */ if (fp == NULL) goto cleanup; virUUIDFormat(uuid, uuidstr); /* Record failure if fprintf or VIR_FCLOSE fails, and be careful always to close the stream. */ if ((fprintf(fp, "\n#UUID: %s\n", uuidstr) < 0) || (VIR_FCLOSE(fp) == EOF)) goto cleanup; } ret = 0; cleanup: VIR_FORCE_FCLOSE(fp); VIR_FREE(conf_file); return ret; }
static int addnhostsWrite(const char *path, dnsmasqAddnHost *hosts, unsigned int nhosts) { char *tmp; FILE *f; bool istmp = true; size_t i, j; int rc = 0; /* even if there are 0 hosts, create a 0 length file, to allow * for runtime addition. */ if (virAsprintf(&tmp, "%s.new", path) < 0) return -ENOMEM; if (!(f = fopen(tmp, "w"))) { istmp = false; if (!(f = fopen(path, "w"))) { rc = -errno; goto cleanup; } } for (i = 0; i < nhosts; i++) { if (fputs(hosts[i].ip, f) == EOF || fputc('\t', f) == EOF) { rc = -errno; VIR_FORCE_FCLOSE(f); if (istmp) unlink(tmp); goto cleanup; } for (j = 0; j < hosts[i].nhostnames; j++) { if (fputs(hosts[i].hostnames[j], f) == EOF || fputc('\t', f) == EOF) { rc = -errno; VIR_FORCE_FCLOSE(f); if (istmp) unlink(tmp); goto cleanup; } } if (fputc('\n', f) == EOF) { rc = -errno; VIR_FORCE_FCLOSE(f); if (istmp) unlink(tmp); goto cleanup; } } if (VIR_FCLOSE(f) == EOF) { rc = -errno; goto cleanup; } if (istmp && rename(tmp, path) < 0) { rc = -errno; unlink(tmp); goto cleanup; } cleanup: VIR_FREE(tmp); return rc; }
static int addnhostsWrite(const char *path, dnsmasqAddnHost *hosts, unsigned int nhosts) { char *tmp; FILE *f; bool istmp = true; unsigned int i, ii; int rc = 0; if (nhosts == 0) return rc; if (virAsprintf(&tmp, "%s.new", path) < 0) return -ENOMEM; if (!(f = fopen(tmp, "w"))) { istmp = false; if (!(f = fopen(path, "w"))) { rc = -errno; goto cleanup; } } for (i = 0; i < nhosts; i++) { if (fputs(hosts[i].ip, f) == EOF || fputc('\t', f) == EOF) { rc = -errno; VIR_FORCE_FCLOSE(f); if (istmp) unlink(tmp); goto cleanup; } for (ii = 0; ii < hosts[i].nhostnames; ii++) { if (fputs(hosts[i].hostnames[ii], f) == EOF || fputc('\t', f) == EOF) { rc = -errno; VIR_FORCE_FCLOSE(f); if (istmp) unlink(tmp); goto cleanup; } } if (fputc('\n', f) == EOF) { rc = -errno; VIR_FORCE_FCLOSE(f); if (istmp) unlink(tmp); goto cleanup; } } if (VIR_FCLOSE(f) == EOF) { rc = -errno; goto cleanup; } if (istmp && rename(tmp, path) < 0) { rc = -errno; unlink(tmp); goto cleanup; } cleanup: VIR_FREE(tmp); return rc; }