Create file in virFileWriteStr() if it doesn't exist

This patch adds a mode_t parameter to virFileWriteStr().
If mode is different from 0, virFileWriteStr() will try
to create the file if it doesn't exist.

* src/util/util.h (virFileWriteStr): Alter signature.
* src/util/util.c (virFileWriteStr): Allow file creation.
* src/network/bridge_driver.c (networkEnableIpForwarding)
(networkDisableIPV6): Adjust clients.
* src/node_device/node_device_driver.c
(nodeDeviceVportCreateDelete): Likewise.
* src/util/cgroup.c (virCgroupSetValueStr): Likewise.
* src/util/pci.c (pciBindDeviceToStub, pciUnBindDeviceFromStub):
Likewise.
This commit is contained in:
Jean-Baptiste Rouault 2010-12-03 10:47:08 +01:00 committed by Eric Blake
parent 4fa617e6eb
commit 966a1bfe22
6 changed files with 24 additions and 20 deletions

View File

@ -1024,7 +1024,7 @@ networkReloadIptablesRules(struct network_driver *driver)
static int static int
networkEnableIpForwarding(void) networkEnableIpForwarding(void)
{ {
return virFileWriteStr("/proc/sys/net/ipv4/ip_forward", "1\n"); return virFileWriteStr("/proc/sys/net/ipv4/ip_forward", "1\n", 0);
} }
#define SYSCTL_PATH "/proc/sys" #define SYSCTL_PATH "/proc/sys"
@ -1045,7 +1045,7 @@ static int networkDisableIPV6(virNetworkObjPtr network)
goto cleanup; goto cleanup;
} }
if (virFileWriteStr(field, "1") < 0) { if (virFileWriteStr(field, "1", 0) < 0) {
virReportSystemError(errno, virReportSystemError(errno,
_("cannot enable %s"), field); _("cannot enable %s"), field);
goto cleanup; goto cleanup;
@ -1057,7 +1057,7 @@ static int networkDisableIPV6(virNetworkObjPtr network)
goto cleanup; goto cleanup;
} }
if (virFileWriteStr(field, "0") < 0) { if (virFileWriteStr(field, "0", 0) < 0) {
virReportSystemError(errno, virReportSystemError(errno,
_("cannot disable %s"), field); _("cannot disable %s"), field);
goto cleanup; goto cleanup;
@ -1069,7 +1069,7 @@ static int networkDisableIPV6(virNetworkObjPtr network)
goto cleanup; goto cleanup;
} }
if (virFileWriteStr(field, "1") < 0) { if (virFileWriteStr(field, "1", 0) < 0) {
virReportSystemError(errno, virReportSystemError(errno,
_("cannot enable %s"), field); _("cannot enable %s"), field);
goto cleanup; goto cleanup;

View File

@ -454,7 +454,7 @@ nodeDeviceVportCreateDelete(const int parent_host,
goto cleanup; goto cleanup;
} }
if (virFileWriteStr(operation_path, vport_name) == -1) { if (virFileWriteStr(operation_path, vport_name, 0) == -1) {
virReportSystemError(errno, virReportSystemError(errno,
_("Write of '%s' to '%s' during " _("Write of '%s' to '%s' during "
"vport create/delete failed"), "vport create/delete failed"),

View File

@ -288,7 +288,7 @@ static int virCgroupSetValueStr(virCgroupPtr group,
return rc; return rc;
VIR_DEBUG("Set value '%s' to '%s'", keypath, value); VIR_DEBUG("Set value '%s' to '%s'", keypath, value);
rc = virFileWriteStr(keypath, value); rc = virFileWriteStr(keypath, value, 0);
if (rc < 0) { if (rc < 0) {
DEBUG("Failed to write value '%s': %m", value); DEBUG("Failed to write value '%s': %m", value);
rc = -errno; rc = -errno;

View File

@ -849,7 +849,7 @@ pciBindDeviceToStub(pciDevice *dev, const char *driver)
* bound by the stub. * bound by the stub.
*/ */
pciDriverFile(path, sizeof(path), driver, "new_id"); pciDriverFile(path, sizeof(path), driver, "new_id");
if (virFileWriteStr(path, dev->id) < 0) { if (virFileWriteStr(path, dev->id, 0) < 0) {
virReportSystemError(errno, virReportSystemError(errno,
_("Failed to add PCI device ID '%s' to %s"), _("Failed to add PCI device ID '%s' to %s"),
dev->id, driver); dev->id, driver);
@ -862,7 +862,7 @@ pciBindDeviceToStub(pciDevice *dev, const char *driver)
* your root filesystem. * your root filesystem.
*/ */
pciDeviceFile(path, sizeof(path), dev->name, "driver/unbind"); pciDeviceFile(path, sizeof(path), dev->name, "driver/unbind");
if (virFileExists(path) && virFileWriteStr(path, dev->name) < 0) { if (virFileExists(path) && virFileWriteStr(path, dev->name, 0) < 0) {
virReportSystemError(errno, virReportSystemError(errno,
_("Failed to unbind PCI device '%s'"), dev->name); _("Failed to unbind PCI device '%s'"), dev->name);
return -1; return -1;
@ -875,7 +875,7 @@ pciBindDeviceToStub(pciDevice *dev, const char *driver)
if (!virFileLinkPointsTo(path, drvdir)) { if (!virFileLinkPointsTo(path, drvdir)) {
/* Xen's pciback.ko wants you to use new_slot first */ /* Xen's pciback.ko wants you to use new_slot first */
pciDriverFile(path, sizeof(path), driver, "new_slot"); pciDriverFile(path, sizeof(path), driver, "new_slot");
if (virFileExists(path) && virFileWriteStr(path, dev->name) < 0) { if (virFileExists(path) && virFileWriteStr(path, dev->name, 0) < 0) {
virReportSystemError(errno, virReportSystemError(errno,
_("Failed to add slot for PCI device '%s' to %s"), _("Failed to add slot for PCI device '%s' to %s"),
dev->name, driver); dev->name, driver);
@ -883,7 +883,7 @@ pciBindDeviceToStub(pciDevice *dev, const char *driver)
} }
pciDriverFile(path, sizeof(path), driver, "bind"); pciDriverFile(path, sizeof(path), driver, "bind");
if (virFileWriteStr(path, dev->name) < 0) { if (virFileWriteStr(path, dev->name, 0) < 0) {
virReportSystemError(errno, virReportSystemError(errno,
_("Failed to bind PCI device '%s' to %s"), _("Failed to bind PCI device '%s' to %s"),
dev->name, driver); dev->name, driver);
@ -895,7 +895,7 @@ pciBindDeviceToStub(pciDevice *dev, const char *driver)
* ID table so that 'drivers_probe' works below. * ID table so that 'drivers_probe' works below.
*/ */
pciDriverFile(path, sizeof(path), driver, "remove_id"); pciDriverFile(path, sizeof(path), driver, "remove_id");
if (virFileExists(path) && virFileWriteStr(path, dev->id) < 0) { if (virFileExists(path) && virFileWriteStr(path, dev->id, 0) < 0) {
virReportSystemError(errno, virReportSystemError(errno,
_("Failed to remove PCI ID '%s' from %s"), _("Failed to remove PCI ID '%s' from %s"),
dev->id, driver); dev->id, driver);
@ -936,7 +936,7 @@ pciUnBindDeviceFromStub(pciDevice *dev, const char *driver)
pciDeviceFile(path, sizeof(path), dev->name, "driver"); pciDeviceFile(path, sizeof(path), dev->name, "driver");
if (virFileExists(drvdir) && virFileLinkPointsTo(path, drvdir)) { if (virFileExists(drvdir) && virFileLinkPointsTo(path, drvdir)) {
pciDriverFile(path, sizeof(path), driver, "unbind"); pciDriverFile(path, sizeof(path), driver, "unbind");
if (virFileWriteStr(path, dev->name) < 0) { if (virFileWriteStr(path, dev->name, 0) < 0) {
virReportSystemError(errno, virReportSystemError(errno,
_("Failed to bind PCI device '%s' to %s"), _("Failed to bind PCI device '%s' to %s"),
dev->name, driver); dev->name, driver);
@ -946,7 +946,7 @@ pciUnBindDeviceFromStub(pciDevice *dev, const char *driver)
/* Xen's pciback.ko wants you to use remove_slot on the specific device */ /* Xen's pciback.ko wants you to use remove_slot on the specific device */
pciDriverFile(path, sizeof(path), driver, "remove_slot"); pciDriverFile(path, sizeof(path), driver, "remove_slot");
if (virFileExists(path) && virFileWriteStr(path, dev->name) < 0) { if (virFileExists(path) && virFileWriteStr(path, dev->name, 0) < 0) {
virReportSystemError(errno, virReportSystemError(errno,
_("Failed to remove slot for PCI device '%s' to %s"), _("Failed to remove slot for PCI device '%s' to %s"),
dev->name, driver); dev->name, driver);
@ -961,7 +961,7 @@ pciUnBindDeviceFromStub(pciDevice *dev, const char *driver)
*/ */
pciDriverFile(path, sizeof(path), driver, "remove_id"); pciDriverFile(path, sizeof(path), driver, "remove_id");
if (!virFileExists(drvdir) || virFileExists(path)) { if (!virFileExists(drvdir) || virFileExists(path)) {
if (virFileWriteStr(PCI_SYSFS "drivers_probe", dev->name) < 0) { if (virFileWriteStr(PCI_SYSFS "drivers_probe", dev->name, 0) < 0) {
virReportSystemError(errno, virReportSystemError(errno,
_("Failed to trigger a re-probe for PCI device '%s'"), _("Failed to trigger a re-probe for PCI device '%s'"),
dev->name); dev->name);

View File

@ -1125,20 +1125,23 @@ int virFileReadAll(const char *path, int maxlen, char **buf)
return len; return len;
} }
/* Truncate @path and write @str to it. /* Truncate @path and write @str to it. If @mode is 0, ensure that
@path exists; otherwise, use @mode if @path must be created.
Return 0 for success, nonzero for failure. Return 0 for success, nonzero for failure.
Be careful to preserve any errno value upon failure. */ Be careful to preserve any errno value upon failure. */
int virFileWriteStr(const char *path, const char *str) int virFileWriteStr(const char *path, const char *str, mode_t mode)
{ {
int fd; int fd;
if ((fd = open(path, O_WRONLY|O_TRUNC)) == -1) if (mode)
fd = open(path, O_WRONLY|O_TRUNC|O_CREAT, mode);
else
fd = open(path, O_WRONLY|O_TRUNC);
if (fd == -1)
return -1; return -1;
if (safewrite(fd, str, strlen(str)) < 0) { if (safewrite(fd, str, strlen(str)) < 0) {
int saved_errno = errno;
VIR_FORCE_CLOSE(fd); VIR_FORCE_CLOSE(fd);
errno = saved_errno;
return -1; return -1;
} }

View File

@ -97,7 +97,8 @@ int virFileReadLimFD(int fd, int maxlen, char **buf) ATTRIBUTE_RETURN_CHECK;
int virFileReadAll(const char *path, int maxlen, char **buf) ATTRIBUTE_RETURN_CHECK; int virFileReadAll(const char *path, int maxlen, char **buf) ATTRIBUTE_RETURN_CHECK;
int virFileWriteStr(const char *path, const char *str) ATTRIBUTE_RETURN_CHECK; int virFileWriteStr(const char *path, const char *str, mode_t mode)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
int virFileMatchesNameSuffix(const char *file, int virFileMatchesNameSuffix(const char *file,
const char *name, const char *name,