X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=lib_internal%2Funify-deunify.c;fp=lib_internal%2Funify-deunify.c;h=3d03249e64c69b8e9d444f9cf4ca45fbe4c8753c;hb=3f3cf95f755f3ef1c31ad8e38153deb4ee214c66;hp=0000000000000000000000000000000000000000;hpb=bfa4b37aaa195007a09bc166e8c8563d5a3c2ef1;p=util-vserver.git diff --git a/lib_internal/unify-deunify.c b/lib_internal/unify-deunify.c new file mode 100644 index 0000000..3d03249 --- /dev/null +++ b/lib_internal/unify-deunify.c @@ -0,0 +1,125 @@ +// $Id: unify-deunify.c,v 1.2 2004/02/18 04:48:24 ensc Exp $ --*- c -*-- + +// Copyright (C) 2004 Enrico Scholz +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; version 2 of the License. +// +// This program 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 General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include "unify.h" +#include +#include +#include +#include +#include +#include +#include +#include + +#define ENSC_WRAPPERS_IO 1 +#include + +bool +Unify_deUnify(char const *dst) +{ + size_t l = strlen(dst); + char tmpfile[l + sizeof(";XXXXXX")]; + int fd_src, fd_tmp; + struct stat st; + struct utimbuf utm; + + fd_src = open(dst, O_RDONLY); + if (fd_src==-1) { + perror("open()"); + return false; + } + + if (fstat(fd_src, &st)==-1) { + perror("fstat()"); + close(fd_src); + return false; + } + + memcpy(tmpfile, dst, l); + memcpy(tmpfile+l, ";XXXXXX", 8); + fd_tmp = mkstemp(tmpfile); + + if (fd_tmp==-1) { + perror("mkstemp()"); + tmpfile[0] = '\0'; + goto err; + } + + if (fchown(fd_tmp, st.st_uid, st.st_gid)==-1 || + fchmod(fd_tmp, st.st_mode)==-1) { + perror("fchown()/fchmod()"); + goto err; + } + + // todo: acl? + + for (;;) { + char buf[0x4000]; + ssize_t len = read(fd_src, buf, sizeof buf); + if (len==-1) { + perror("read()"); + goto err; + } + if (len==0) break; + + if (!WwriteAll(fd_tmp, buf, len, 0)) goto err; + } + + if (close(fd_src)==-1) { + perror("close()"); + goto err; + } + if (close(fd_tmp)==-1) { + perror("close()"); + goto err; + } + + utm.actime = st.st_atime; + utm.modtime = st.st_mtime; + + // ALERT: race !!! + if (utime(tmpfile, &utm)==-1) { + perror("utime()"); + goto err1; + } + + if (unlink(dst)==-1) { + perror("unlink()"); + goto err1; + } + + // ALERT: race !!! + if (rename(tmpfile, dst)==-1) { + perror("FATAL error in rename()"); + _exit(1); + } + + return true; + + err: + close(fd_src); + close(fd_tmp); + err1: + if (tmpfile[0]) unlink(tmpfile); + + return false; +}