discover: Use pxeconffile for udhcpc option name
[petitboot] / discover / file.c
index 269421cb8d02c727fb33fd607506d9c67d83710f..1bde9fb805ba906c95ca4ec24df9d1dc921994af 100644 (file)
@@ -17,6 +17,8 @@
 
 #include <fcntl.h>
 #include <unistd.h>
+#include <stdlib.h>
+#include <errno.h>
 #include <sys/stat.h>
 #include <sys/types.h>
 
@@ -75,3 +77,47 @@ err_close:
        close(fd);
        return -1;
 }
+
+static int write_fd(int fd, char *buf, int len)
+{
+       int i, rc;
+
+       for (i = 0; i < len; i += rc) {
+               rc = write(fd, buf + i, len - i);
+               if (rc < 0 && errno != -EINTR)
+                       return rc;
+       }
+
+       return 0;
+}
+
+int replace_file(const char *filename, char *buf, int len)
+{
+       char *tempfile;
+       mode_t oldmask;
+       int rc, fd;
+
+       tempfile = talloc_asprintf(NULL, "%s.XXXXXX", filename);
+
+       oldmask = umask(0644);
+       fd = mkstemp(tempfile);
+       umask(oldmask);
+       if (fd < 0) {
+               talloc_free(tempfile);
+               return fd;
+       }
+
+       rc = write_fd(fd, buf, len);
+       if (rc) {
+               unlink(tempfile);
+       } else {
+               rc = rename(tempfile, filename);
+       }
+
+       talloc_free(tempfile);
+
+       fchmod(fd, 0644);
+
+       close(fd);
+       return rc;
+}