From 613624fc1aa097908473a5e0374aedd49bec0e42 Mon Sep 17 00:00:00 2001 From: Enrico Scholz Date: Tue, 17 Feb 2004 02:34:53 +0000 Subject: [PATCH] moved functionality into lib_internal library git-svn-id: http://svn.linux-vserver.org/svn/util-vserver/trunk@911 94cd875c-1c1d-0410-91d2-eb244daf1a30 --- util-vserver/src/vunify-compare.hc | 38 -------- util-vserver/src/vunify-debug.c | 43 --------- util-vserver/src/vunify-doit.hc | 164 -------------------------------- util-vserver/src/vunify-matchlist.c | 183 ------------------------------------ util-vserver/src/vunify-matchlist.h | 77 --------------- 5 files changed, 505 deletions(-) delete mode 100644 util-vserver/src/vunify-compare.hc delete mode 100644 util-vserver/src/vunify-debug.c delete mode 100644 util-vserver/src/vunify-doit.hc delete mode 100644 util-vserver/src/vunify-matchlist.c delete mode 100644 util-vserver/src/vunify-matchlist.h diff --git a/util-vserver/src/vunify-compare.hc b/util-vserver/src/vunify-compare.hc deleted file mode 100644 index 567b03f..0000000 --- a/util-vserver/src/vunify-compare.hc +++ /dev/null @@ -1,38 +0,0 @@ -// $Id$ --*- 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. - - -static bool -compareUnify(struct stat const *lhs, struct stat const *rhs) -{ - return (lhs->st_dev ==rhs->st_dev && - lhs->st_ino !=rhs->st_ino && - lhs->st_mode ==rhs->st_mode && - lhs->st_uid ==rhs->st_uid && - lhs->st_gid ==rhs->st_gid && - lhs->st_size ==rhs->st_size && - lhs->st_mtime==rhs->st_mtime); - - // TODO: acl? time? -} - -static bool -compareDeUnify(struct stat const *lhs, struct stat const *rhs) -{ - return (lhs->st_dev ==rhs->st_dev && - lhs->st_ino ==rhs->st_ino); -} diff --git a/util-vserver/src/vunify-debug.c b/util-vserver/src/vunify-debug.c deleted file mode 100644 index 9b867d4..0000000 --- a/util-vserver/src/vunify-debug.c +++ /dev/null @@ -1,43 +0,0 @@ -// $Id$ --*- 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 "vunify-matchlist.h" - -void -MatchList_destroy(struct MatchList *list) -{ - size_t i; - - String_destroy(&list->id); - free(list->data); - - for (i=0; ibuf_count; ++i) - free(const_cast(void *)(list->buf[i])); - - free(list->buf); -} - -void -String_destroy(String *str) -{ - free(const_cast(char *)(str->d)); -} diff --git a/util-vserver/src/vunify-doit.hc b/util-vserver/src/vunify-doit.hc deleted file mode 100644 index c49d720..0000000 --- a/util-vserver/src/vunify-doit.hc +++ /dev/null @@ -1,164 +0,0 @@ -// $Id$ --*- 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. - -#include -#include -#include - -static bool -doitUnify(char const *src, struct stat const *src_stat, - char const *dst, struct stat const UNUSED *dst_stat) -{ - size_t l = strlen(dst); - char tmpfile[l + sizeof(";XXXXXX")]; - int fd; - bool res = false; - - // at first, set the ILI flags on 'src' - if (vc_set_iattr_compat(src, src_stat->st_dev, src_stat->st_ino, - 0, VC_IATTR_IUNLINK, VC_IATTR_IUNLINK, - &src_stat->st_mode)==-1) - return false; - - // now, create a temporary filename - memcpy(tmpfile, dst, l); - memcpy(tmpfile+l, ";XXXXXX", 8); - fd = mkstemp(tmpfile); - close(fd); - - if (fd==-1) { - perror("mkstemp()"); - return false; - } - - // and rename the old file to this name - if (rename(dst, tmpfile)==-1) { - perror("rename()"); - goto err; - } - - // now, link the src-file to dst - if (link(src, dst)==-1) { - perror("link()"); - - unlink(dst); - if (rename(tmpfile, dst)==-1) { - perror("FATAL error in rename()"); - _exit(1); - } - goto err; - } - - res = true; - - err: - unlink(tmpfile); - - return res; -} - -static bool -doitDeUnify(char const UNUSED *src, struct stat const UNUSED *src_stat, - char const *dst, struct stat const UNUSED *dst_stat) -{ - 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)) 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; -} diff --git a/util-vserver/src/vunify-matchlist.c b/util-vserver/src/vunify-matchlist.c deleted file mode 100644 index 2801aa3..0000000 --- a/util-vserver/src/vunify-matchlist.c +++ /dev/null @@ -1,183 +0,0 @@ -// $Id$ --*- c -*-- - -// Copyright (C) 2003 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 "vunify-matchlist.h" - -#include -#include - -#define ENSC_WRAPPERS_STDLIB 1 -#include - -bool -MatchList_compare(struct MatchList const *list, char const *path) -{ - bool res = true; - struct MatchItem const * ptr = list->data; - struct MatchItem const * const end_ptr = list->data + list->count; - - //write(1, path, strlen(path)); - //write(1, "\n", 1); - for (; ptrcmp==0 && strcmp(ptr->name, path)==0) || - (ptr->cmp!=0 && (ptr->cmp)(ptr->name, path)==0)) { - switch (ptr->type) { - case stINCLUDE : res = true; break; - case stEXCLUDE : res = false; break; - default : abort(); - } - break; - } - } - - return res; -} - -void -MatchList_init(struct MatchList *list, char const *root, size_t count) -{ - list->skip_depth = 0; - list->root.d = root; - list->root.l = strlen(root); - list->data = Emalloc(sizeof(struct MatchItem) * count); - list->count = count; - list->buf = 0; - list->buf_count = 0; - - String_init(&list->id); -} - -static int -fnmatchWrap(char const *a, char const *b) -{ - return fnmatch(a, b, 0); -} - - -static MatchItemCompareFunc -determineCompareFunc(char const UNUSED *fname) -{ - return fnmatchWrap; -} - -void -MatchList_appendFiles(struct MatchList *list, size_t idx, - char **files, size_t count, - bool auto_type) -{ - struct MatchItem *ptr = list->data + idx; - size_t i; - - assert(idx+count <= list->count); - - if (auto_type) { - for (i=0; itype = stINCLUDE; ++file; break; - case '-' : ++file; /*@fallthrough@*/ - default : ptr->type = stEXCLUDE; break; - } - ptr->cmp = determineCompareFunc(file); - ptr->name = file; - ++ptr; - } - } - else { - for (i=0; itype = stEXCLUDE; - ptr->name = files[i]; - ptr->cmp = 0; - ++ptr; - } - } -} - - -void -PathInfo_append(PathInfo * restrict lhs, - PathInfo const * restrict rhs, - char *buf) -{ - char * ptr = buf; - char const * rhs_ptr = rhs->d; - size_t rhs_len = rhs->l; - - while (lhs->l>1 && lhs->d[lhs->l-1]=='/') --lhs->l; - - if (lhs->l>0) { - while (rhs->l>0 && *rhs_ptr=='/') { - ++rhs_ptr; - --rhs_len; - } - - ptr = Xmemcpy(ptr, lhs->d, lhs->l); - if (ptr[-1]!='/') - ptr = Xmemcpy(ptr, "/", 1); - } -// else if (*rhs_ptr!='/') -// ptr = Xmemcpy(ptr, "/", 1); - - ptr = Xmemcpy(ptr, rhs_ptr, rhs_len+1); - - lhs->d = buf; - lhs->l = ptr-buf-1; -} - -void -String_init(String *str) -{ - str->d = 0; - str->l = 0; -} - -#ifdef ENSC_TESTSUITE -#define CHECK(LHS,RHS, EXP) \ - do { \ - PathInfo lhs = { LHS, sizeof(LHS)-1 }; \ - PathInfo rhs = { RHS, sizeof(RHS)-1 }; \ - char *buf = malloc(ENSC_PI_APPSZ(lhs,rhs)); \ - assert(ENSC_PI_APPSZ(lhs,rhs)>=sizeof(EXP)); \ - PathInfo_append(&lhs, &rhs, buf); \ - assert(memcmp(lhs.d, EXP, sizeof(EXP))==0); \ - assert(lhs.l == sizeof(EXP)-1); \ - free(buf); \ - } while (0) - - -void -PathInfo_test() -{ - CHECK("/var", "/tmp", "/var/tmp"); - CHECK("/var", "tmp", "/var/tmp"); - CHECK("/var/", "/tmp", "/var/tmp"); - CHECK("/var/", "tmp", "/var/tmp"); - - CHECK("/", "tmp", "/tmp"); - CHECK("/", "/tmp", "/tmp"); - - CHECK("", "/tmp", "/tmp"); - - CHECK("", "tmp", "tmp"); - CHECK("", "", ""); -} -#endif diff --git a/util-vserver/src/vunify-matchlist.h b/util-vserver/src/vunify-matchlist.h deleted file mode 100644 index 82db861..0000000 --- a/util-vserver/src/vunify-matchlist.h +++ /dev/null @@ -1,77 +0,0 @@ -// $Id$ --*- c -*-- - -// Copyright (C) 2003 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. - - -#ifndef H_UTIL_VSERVER_SRC_VUNIFY_MATCHLIST_H -#define H_UTIL_VSERVER_SRC_VUNIFY_MATCHLIST_H - -#include "util.h" - -#include -#include -#include - -typedef int (*MatchItemCompareFunc)(char const *, char const *); - -struct MatchItem -{ - enum { stINCLUDE, stEXCLUDE } type; - char const * name; - MatchItemCompareFunc cmp; -}; - -typedef struct -{ - char const * d; - size_t l; -} String; - -typedef String PathInfo; - -struct MatchList -{ - size_t skip_depth; - PathInfo root; - String id; - struct MatchItem *data; - size_t count; - - void const **buf; - size_t buf_count; -}; - - -void MatchList_init(struct MatchList *, char const *root, size_t count) NONNULL((1,2)); -void MatchList_destroy(struct MatchList *) NONNULL((1)); -void MatchList_appendFiles(struct MatchList *, size_t idx, - char **files, size_t count, - bool auto_type); - -bool MatchList_compare(struct MatchList const *, char const *path) NONNULL((1,2)); -struct MatchItem -const * MatchList_find(struct MatchList const *, char const *path) NONNULL((1,2)); - -void String_init(String *); -void String_destroy(String *); - -void PathInfo_append(PathInfo * restrict, - PathInfo const * restrict, - char *buf) NONNULL((1,2,3)); - -#define ENSC_PI_APPSZ(P1,P2) ((P1).l + sizeof("/") + (P2).l) - -#endif // H_UTIL_VSERVER_SRC_VUNIFY_MATCHLIST_H -- 1.8.1.5