]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
make a "copy file" function
authorDaniel Lezcano <dlezcano@fr.ibm.com>
Thu, 28 May 2009 10:10:50 +0000 (12:10 +0200)
committerDaniel Lezcano <dlezcano@fr.ibm.com>
Thu, 28 May 2009 10:10:50 +0000 (12:10 +0200)
Create a specific function to copy a file from a location to
another location.

Signed-off-by: Daniel Lezcano <dlezcano@fr.ibm.com>
src/lxc/Makefile.am
src/lxc/utils.c [new file with mode: 0644]
src/lxc/utils.h

index 57b3caeb62fb14bba8536b88829f0d20bbad0c13..58c8ef3849e9c5ad67cfcac944942d8127277837 100644 (file)
@@ -34,7 +34,7 @@ liblxc_la_SOURCES = \
        parse.c parse.h \
        cgroup.c cgroup.h \
        lxc.h \
-       lxc_utils.h \
+       utils.c utils.h \
        lock.c lock.h \
        namespace.h \
        conf.c conf.h \
diff --git a/src/lxc/utils.c b/src/lxc/utils.c
new file mode 100644 (file)
index 0000000..9a6b263
--- /dev/null
@@ -0,0 +1,104 @@
+/*
+ * lxc: linux Container library
+ *
+ * (C) Copyright IBM Corp. 2007, 2008
+ *
+ * Authors:
+ * Daniel Lezcano <dlezcano at fr.ibm.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include <fcntl.h>
+#include <errno.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/mman.h>
+
+#include "log.h"
+
+lxc_log_define(lxc_utils, lxc);
+
+int lxc_copy_file(const char *srcfile, const char *dstfile)
+{
+       void *srcaddr = NULL, *dstaddr;
+       struct stat stat;
+       int srcfd, dstfd, ret = -1;
+       char c = '\0';
+
+       dstfd = open(dstfile, O_CREAT | O_EXCL | O_RDWR, 0600);
+       if (dstfd < 0) {
+               SYSERROR("failed to creat '%s'", dstfile);
+               goto out;
+       }
+
+       srcfd = open(srcfile, O_RDONLY);
+       if (srcfd < 0) {
+               SYSERROR("failed to open '%s'", srcfile);
+               goto err;
+       }
+
+       if (fstat(srcfd, &stat)) {
+               SYSERROR("failed to stat '%s'", srcfile);
+               goto err;
+       }
+
+       if (!stat.st_size) {
+               INFO("copy '%s' which is an empty file", srcfile);
+               ret = 0;
+               goto out_close;
+       }
+
+       if (lseek(dstfd, stat.st_size - 1, SEEK_SET) < 0) {
+               SYSERROR("failed to seek dest file '%s'", dstfile);
+               goto err;
+       }
+
+       /* fixup length */
+       if (write(dstfd, &c, 1) < 0) {
+               SYSERROR("failed to write to '%s'", dstfile);
+               goto err;
+       }
+
+       srcaddr = mmap(NULL, stat.st_size, PROT_READ, MAP_SHARED, srcfd, 0L);
+       if (srcaddr == MAP_FAILED) {
+               SYSERROR("failed to mmap '%s'", srcfile);
+               goto err;
+       }
+
+       dstaddr = mmap(NULL, stat.st_size, PROT_WRITE, MAP_SHARED, dstfd, 0L);
+       if (dstaddr == MAP_FAILED) {
+               SYSERROR("failed to mmap '%s'", dstfile);
+               goto err;
+       }
+
+       ret = 0;
+
+       memcpy(dstaddr, srcaddr, stat.st_size);
+
+       munmap(dstaddr, stat.st_size);
+out_mmap:
+       if (srcaddr)
+               munmap(srcaddr, stat.st_size);
+out_close:
+       close(dstfd);
+       close(srcfd);
+out:
+       return ret;
+err:
+       unlink(dstfile);
+       goto out_mmap;
+}
index 5fc3946fbb23777ec45343096764c1ed9bc2c061..2fe078946a9cd32d54512aa23230641d0bc84504 100644 (file)
@@ -49,3 +49,5 @@
        } while (0)
 
 #endif
+
+extern int lxc_copy_file(const char *src, const char *dst);