[vhffs-dev] [663] Add vhffsfs (with a poor cache)

[ Thread Index | Date Index | More vhffs.org/vhffs-dev Archives ]


Revision: 663
Author:   gradator
Date:     2007-07-05 11:23:50 +0000 (Thu, 05 Jul 2007)

Log Message:
-----------
Add vhffsfs (with a poor cache)

Added Paths:
-----------
    trunk/vhffs-tools/src/md5.h
    trunk/vhffs-tools/src/md5c.c
    trunk/vhffs-tools/src/vhffsfs.c
    trunk/vhffs-tools/src/vhffsfs_compil.sh
    trunk/vhffs-tools/src/vhffsfs_run.sh


Added: trunk/vhffs-tools/src/md5.h
===================================================================
--- trunk/vhffs-tools/src/md5.h	2007-07-05 07:49:46 UTC (rev 662)
+++ trunk/vhffs-tools/src/md5.h	2007-07-05 11:23:50 UTC (rev 663)
@@ -0,0 +1,55 @@
+/* MD5.H - header file for MD5C.C
+ */
+
+/* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
+rights reserved.
+
+License to copy and use this software is granted provided that it
+is identified as the "RSA Data Security, Inc. MD5 Message-Digest
+Algorithm" in all material mentioning or referencing this software
+or this function.
+
+License is also granted to make and use derivative works provided
+that such works are identified as "derived from the RSA Data
+Security, Inc. MD5 Message-Digest Algorithm" in all material
+mentioning or referencing the derived work.
+
+RSA Data Security, Inc. makes no representations concerning either
+the merchantability of this software or the suitability of this
+software for any particular purpose. It is provided "as is"
+without express or implied warranty of any kind.
+
+These notices must be retained in any copies of any part of this
+documentation and/or software.
+ */
+
+#ifndef MD5_H
+#define MD5_H 1
+
+#include <sys/types.h>
+#include <inttypes.h>
+#include <stdint.h>
+
+/* POINTER defines a generic pointer type */
+typedef unsigned char *POINTER;
+
+#ifndef HAVE_UINT32_T
+#  if SIZEOF_INT == 4
+typedef unsigned int uint32_t;
+#  elif SIZEOF_LONG == 4
+typedef unsigned long int uint32_t;
+#  endif
+#endif
+
+/* MD5 context. */
+typedef struct {
+  uint32_t state[4];                                   /* state (ABCD) */
+  uint32_t count[2];        /* number of bits, modulo 2^64 (lsb first) */
+  unsigned char buffer[64];                         /* input buffer */
+} MD5_CTX;
+
+void MD5Init (MD5_CTX *);
+void MD5Update (MD5_CTX *, unsigned char *, unsigned int);
+void MD5Final (unsigned char [16], MD5_CTX *);
+
+#endif

Added: trunk/vhffs-tools/src/md5c.c
===================================================================
--- trunk/vhffs-tools/src/md5c.c	2007-07-05 07:49:46 UTC (rev 662)
+++ trunk/vhffs-tools/src/md5c.c	2007-07-05 11:23:50 UTC (rev 663)
@@ -0,0 +1,330 @@
+/* MD5C.C - RSA Data Security, Inc., MD5 message-digest algorithm
+ */
+
+/* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
+rights reserved.
+
+License to copy and use this software is granted provided that it
+is identified as the "RSA Data Security, Inc. MD5 Message-Digest
+Algorithm" in all material mentioning or referencing this software
+or this function.
+
+License is also granted to make and use derivative works provided
+that such works are identified as "derived from the RSA Data
+Security, Inc. MD5 Message-Digest Algorithm" in all material
+mentioning or referencing the derived work.
+
+RSA Data Security, Inc. makes no representations concerning either
+the merchantability of this software or the suitability of this
+software for any particular purpose. It is provided "as is"
+without express or implied warranty of any kind.
+
+These notices must be retained in any copies of any part of this
+documentation and/or software.
+ */
+
+#include "md5.h"
+
+/* Constants for MD5Transform routine.
+ */
+
+#define S11 7
+#define S12 12
+#define S13 17
+#define S14 22
+#define S21 5
+#define S22 9
+#define S23 14
+#define S24 20
+#define S31 4
+#define S32 11
+#define S33 16
+#define S34 23
+#define S41 6
+#define S42 10
+#define S43 15
+#define S44 21
+
+static void MD5Transform (uint32_t [4], unsigned char [64]);
+static void Encode (unsigned char *, uint32_t *, unsigned int);
+static void Decode (uint32_t *, unsigned char *, unsigned int);
+static void MD5_memcpy (POINTER, POINTER, unsigned int);
+static void MD5_memset (POINTER, int, unsigned int);
+
+static unsigned char PADDING[64] = {
+  0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
+};
+
+/* F, G, H and I are basic MD5 functions.
+ */
+#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
+#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
+#define H(x, y, z) ((x) ^ (y) ^ (z))
+#define I(x, y, z) ((y) ^ ((x) | (~z)))
+
+/* ROTATE_LEFT rotates x left n bits.
+ */
+#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
+
+/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4.
+Rotation is separate from addition to prevent recomputation.
+ */
+#define FF(a, b, c, d, x, s, ac) { \
+ (a) += F ((b), (c), (d)) + (x) + (uint32_t)(ac); \
+ (a) = ROTATE_LEFT ((a), (s)); \
+ (a) += (b); \
+  }
+#define GG(a, b, c, d, x, s, ac) { \
+ (a) += G ((b), (c), (d)) + (x) + (uint32_t)(ac); \
+ (a) = ROTATE_LEFT ((a), (s)); \
+ (a) += (b); \
+  }
+#define HH(a, b, c, d, x, s, ac) { \
+ (a) += H ((b), (c), (d)) + (x) + (uint32_t)(ac); \
+ (a) = ROTATE_LEFT ((a), (s)); \
+ (a) += (b); \
+  }
+#define II(a, b, c, d, x, s, ac) { \
+ (a) += I ((b), (c), (d)) + (x) + (uint32_t)(ac); \
+ (a) = ROTATE_LEFT ((a), (s)); \
+ (a) += (b); \
+  }
+
+/* MD5 initialization. Begins an MD5 operation, writing a new context.
+ */
+void MD5Init (context)
+MD5_CTX *context;                                        /* context */
+{
+  context->count[0] = context->count[1] = 0;
+  /* Load magic initialization constants.
+*/
+  context->state[0] = 0x67452301;
+  context->state[1] = 0xefcdab89;
+  context->state[2] = 0x98badcfe;
+  context->state[3] = 0x10325476;
+}
+
+/* MD5 block update operation. Continues an MD5 message-digest
+  operation, processing another message block, and updating the
+  context.
+ */
+void MD5Update (context, input, inputLen)
+MD5_CTX *context;                                        /* context */
+unsigned char *input;                                /* input block */
+unsigned int inputLen;                     /* length of input block */
+{
+  unsigned int i, index, partLen;
+
+  /* Compute number of bytes mod 64 */
+  index = (unsigned int)((context->count[0] >> 3) & 0x3F);
+
+  /* Update number of bits */
+  if ((context->count[0] += ((uint32_t)inputLen << 3))
+   < ((uint32_t)inputLen << 3))
+ context->count[1]++;
+  context->count[1] += ((uint32_t)inputLen >> 29);
+
+  partLen = 64 - index;
+
+  /* Transform as many times as possible.
+*/
+  if (inputLen >= partLen) {
+ MD5_memcpy
+   ((POINTER)&context->buffer[index], (POINTER)input, partLen);
+ MD5Transform (context->state, context->buffer);
+
+ for (i = partLen; i + 63 < inputLen; i += 64)
+   MD5Transform (context->state, &input[i]);
+
+ index = 0;
+  }
+  else
+ i = 0;
+
+  /* Buffer remaining input */
+  MD5_memcpy
+ ((POINTER)&context->buffer[index], (POINTER)&input[i],
+  inputLen-i);
+}
+
+/* MD5 finalization. Ends an MD5 message-digest operation, writing the
+  the message digest and zeroizing the context.
+ */
+void MD5Final (digest, context)
+unsigned char digest[16];                         /* message digest */
+MD5_CTX *context;                                       /* context */
+{
+  unsigned char bits[8];
+  unsigned int index, padLen;
+
+  /* Save number of bits */
+  Encode (bits, context->count, 8);
+
+  /* Pad out to 56 mod 64.
+*/
+  index = (unsigned int)((context->count[0] >> 3) & 0x3f);
+  padLen = (index < 56) ? (56 - index) : (120 - index);
+  MD5Update (context, PADDING, padLen);
+
+  /* Append length (before padding) */
+  MD5Update (context, bits, 8);
+  /* Store state in digest */
+  Encode (digest, context->state, 16);
+
+  /* Zeroize sensitive information.
+*/
+  MD5_memset ((POINTER)context, 0, sizeof (*context));
+}
+
+/* MD5 basic transformation. Transforms state based on block.
+ */
+static void MD5Transform (state, block)
+uint32_t state[4];
+unsigned char block[64];
+{
+  uint32_t a = state[0], b = state[1], c = state[2], d = state[3], x[16];
+
+  Decode (x, block, 64);
+
+  /* Round 1 */
+  FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
+  FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
+  FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
+  FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
+  FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
+  FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
+  FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
+  FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
+  FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
+  FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
+  FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
+  FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
+  FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
+  FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
+  FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
+  FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
+
+ /* Round 2 */
+  GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
+  GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
+  GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
+  GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
+  GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
+  GG (d, a, b, c, x[10], S22,  0x2441453); /* 22 */
+  GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
+  GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
+  GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
+  GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
+  GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
+  GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
+  GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
+  GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
+  GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
+  GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
+
+  /* Round 3 */
+  HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
+  HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
+  HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
+  HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
+  HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
+  HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
+  HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
+  HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
+  HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
+  HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
+  HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
+  HH (b, c, d, a, x[ 6], S34,  0x4881d05); /* 44 */
+  HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
+  HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
+  HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
+  HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
+
+  /* Round 4 */
+  II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
+  II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
+  II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
+  II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
+  II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
+  II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
+  II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
+  II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
+  II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
+  II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
+  II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
+  II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
+  II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
+  II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
+  II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
+  II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
+
+  state[0] += a;
+  state[1] += b;
+  state[2] += c;
+  state[3] += d;
+
+  /* Zeroize sensitive information. */
+  MD5_memset ((POINTER)x, 0, sizeof (x));
+}
+
+/* Encodes input (uint32_t) into output (unsigned char). Assumes len is
+  a multiple of 4.
+ */
+static void Encode (output, input, len)
+unsigned char *output;
+uint32_t *input;
+unsigned int len;
+{
+  unsigned int i, j;
+
+  for (i = 0, j = 0; j < len; i++, j += 4) {
+ output[j] = (unsigned char)(input[i] & 0xff);
+ output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
+ output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
+ output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
+  }
+}
+
+/* Decodes input (unsigned char) into output (uint32_t). Assumes len is
+  a multiple of 4.
+ */
+static void Decode (output, input, len)
+uint32_t *output;
+unsigned char *input;
+unsigned int len;
+{
+  unsigned int i, j;
+
+  for (i = 0, j = 0; j < len; i++, j += 4)
+ output[i] = ((uint32_t)input[j]) | (((uint32_t)input[j+1]) << 8) |
+   (((uint32_t)input[j+2]) << 16) | (((uint32_t)input[j+3]) << 24);
+}
+
+/* Note: Replace "for loop" with standard memcpy if possible.
+ */
+
+static void MD5_memcpy (output, input, len)
+POINTER output;
+POINTER input;
+unsigned int len;
+{
+  unsigned int i;
+
+  for (i = 0; i < len; i++)
+  output[i] = input[i];
+}
+
+/* Note: Replace "for loop" with standard memset if possible.
+ */
+static void MD5_memset (output, value, len)
+POINTER output;
+int value;
+unsigned int len;
+{
+  unsigned int i;
+
+  for (i = 0; i < len; i++)
+ ((char *)output)[i] = (char)value;
+}

Added: trunk/vhffs-tools/src/vhffsfs.c
===================================================================
--- trunk/vhffs-tools/src/vhffsfs.c	2007-07-05 07:49:46 UTC (rev 662)
+++ trunk/vhffs-tools/src/vhffsfs.c	2007-07-05 11:23:50 UTC (rev 663)
@@ -0,0 +1,1311 @@
+/*
+	VHFFSFS: Virtual chroot for your users, using FUSE !
+	Copyright (C) 2007 Sylvain Rochet <gradator@xxxxxxxxxxxx>
+
+	This program can be distributed under the terms of the GNU GPL.
+	See the file COPYING.
+*/
+
+#define _GNU_SOURCE
+#define WITH_CACHE 1
+
+#include <fuse.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <dirent.h>
+#include <errno.h>
+#include <sys/time.h>
+#include <time.h>
+#ifdef HAVE_SETXATTR
+#include <sys/xattr.h>
+#endif
+#include <pthread.h>
+#include <signal.h>
+#ifdef WITH_CACHE
+#include <glib.h>
+#endif
+#include <postgresql/libpq-fe.h>
+#include "md5.h"
+
+#define VHFFSFS_MAXCONNDB 5
+#define VHFFSFS_CONFIG "/etc/vhffsfs.conf"
+
+#ifdef WITH_CACHE
+#define VHFFSFS_CACHE_UID_HOMEDIR_TIMEOUT 3600
+#define VHFFSFS_CACHE_UID_GROUP_TIMEOUT 3600
+
+typedef struct {
+	gid_t gid;
+	char *groupname;
+	char *groupdir;
+	time_t arrival;
+} vhffsfs_cache_uid_group;
+
+typedef struct {
+	uid_t uid;
+	pthread_mutex_t uid_lock;
+	char *homedir;
+	time_t homedir_arrival;
+	GHashTable *groups;
+} vhffsfs_cache_uid;
+#endif
+
+struct vhffsfs {
+	pthread_mutex_t pg_lock[VHFFSFS_MAXCONNDB];
+	PGconn *pg_conn[VHFFSFS_MAXCONNDB];
+	char *grouppath;
+	char *webspath;
+	char *repositoriespath;
+	char *dbhost;
+	int dbport;
+	char *dbuser;
+	char *dbpass;
+	char *dbname;
+	int dbtimeout;
+	char *dbconninfo;
+	mode_t minimumrightfile;
+	mode_t minimumrightdir;
+#ifdef WITH_CACHE
+	GHashTable *cache;
+#endif
+};
+
+static struct vhffsfs vhffsfs;
+
+#ifdef WITH_CACHE
+time_t vhffsfs_cache_arrival()  {
+	struct timeval tv;
+	gettimeofday(&tv, NULL);
+	return tv.tv_sec;
+}
+
+int vhffsfs_cache_timeout(time_t arrival, time_t timeout)  {
+	struct timeval tv;
+	gettimeofday(&tv, NULL);
+	if(tv.tv_sec - arrival >= timeout) return 1;
+	return 0;
+}
+
+// return context cache entry
+vhffsfs_cache_uid *vhffsfs_cache_uid_context()  {
+	uid_t uid = fuse_get_context()->uid;
+	vhffsfs_cache_uid *vc = g_hash_table_lookup(vhffsfs.cache, &uid);
+	if(!vc)  {
+		vc = malloc(sizeof(vhffsfs_cache_uid));
+		memset(vc, 0, sizeof(vhffsfs_cache_uid));
+		vc->uid = uid;
+		vc->groups = g_hash_table_new(g_str_hash, g_str_equal);
+		pthread_mutex_init(&vc->uid_lock, NULL);
+		g_hash_table_insert(vhffsfs.cache, &vc->uid, vc);
+	}
+	return vc;
+}
+
+vhffsfs_cache_uid_group *vhffsfs_cache_uid_lookup_group(vhffsfs_cache_uid *vc, char *groupname)  {
+	vhffsfs_cache_uid_group *vcg = g_hash_table_lookup(vc->groups, groupname);
+	if(vcg && vhffsfs_cache_timeout(vcg->arrival, VHFFSFS_CACHE_UID_GROUP_TIMEOUT))  {
+		g_hash_table_remove(vc->groups, vcg->groupname);
+		free(vcg->groupname);
+		if(vcg->groupdir) free(vcg->groupdir);
+		free(vcg);
+		return NULL;
+	}
+	return vcg;
+}
+
+vhffsfs_cache_uid_group *vhffsfs_cache_uid_fetch_group(vhffsfs_cache_uid *vc, char *groupname)  {
+	vhffsfs_cache_uid_group *vcg = vhffsfs_cache_uid_lookup_group(vc, groupname);
+	if(vcg) return vcg;
+	vcg = malloc(sizeof(vhffsfs_cache_uid_group));
+	memset(vcg, 0, sizeof(vhffsfs_cache_uid_group));
+	vcg->groupname = strdup(groupname);
+	g_hash_table_insert(vc->groups, vcg->groupname, vcg);
+	return vcg;
+}
+
+static inline void vhffsfs_cache_uid_lock(vhffsfs_cache_uid *vc)  {
+	pthread_mutex_lock(&vc->uid_lock);
+}
+
+static inline void vhffsfs_cache_uid_unlock(vhffsfs_cache_uid *vc)  {
+	pthread_mutex_unlock(&vc->uid_lock);
+}
+#endif
+
+
+PGresult *vhffsfs_PGQuery(char *query)  {
+
+	int i;
+	PGresult *res;
+
+	do {
+		for(i = 0 ; i < VHFFSFS_MAXCONNDB && pthread_mutex_trylock(&vhffsfs.pg_lock[i]) ; i++);
+	// all slots used, sleep for 100ms and try again
+	} while(i == VHFFSFS_MAXCONNDB && !usleep(100000));
+
+	// something very weird happened
+	if(i == VHFFSFS_MAXCONNDB) return NULL;
+
+	// wait until we are connected to the pgsql server
+	while(!vhffsfs.pg_conn[i]  ||  PQstatus(vhffsfs.pg_conn[i]) != CONNECTION_OK)  {
+		vhffsfs.pg_conn[i] = PQconnectdb(vhffsfs.dbconninfo);
+
+		if(PQstatus(vhffsfs.pg_conn[i]) != CONNECTION_OK)  {
+			fprintf(stderr, "SLOT[%d]: Connection to database '%s' failed: %s\n", i, PQdb(vhffsfs.pg_conn[i]), PQerrorMessage(vhffsfs.pg_conn[i]));
+			PQfinish(vhffsfs.pg_conn[i]);
+			sleep(1);
+		}
+		else {
+			fprintf(stdout, "SLOT[%d]: Connected to database '%s'\n", i, PQdb(vhffsfs.pg_conn[i]));
+		}
+	}
+
+	res = PQexec(vhffsfs.pg_conn[i], query);
+	if(PQresultStatus(res) != PGRES_TUPLES_OK)
+	{
+		fprintf(stderr, "SLOT[%d]: QUERY: '%s' failed: %s\n", i, query, PQerrorMessage(vhffsfs.pg_conn[i]));
+		PQclear(res);
+		res=NULL;
+	}
+	else {
+		fprintf(stdout, "SLOT[%d]: QUERY: '%s'\n", i, query);
+	}
+
+	pthread_mutex_unlock(&vhffsfs.pg_lock[i]);
+	return res;
+}
+
+
+char *vhffsfs_gethomedir()  {
+	char query[128];
+	PGresult *res;
+	char *homedir=NULL;
+
+#ifdef WITH_CACHE
+	// try to fetch homedir from cache
+	vhffsfs_cache_uid *vc = vhffsfs_cache_uid_context();
+	vhffsfs_cache_uid_lock(vc);
+	if(vc->homedir && !vhffsfs_cache_timeout(vc->homedir_arrival, VHFFSFS_CACHE_UID_HOMEDIR_TIMEOUT)) {
+		char *t = strdup(vc->homedir);
+		vhffsfs_cache_uid_unlock(vc);
+		return t;
+	}
+	vhffsfs_cache_uid_unlock(vc);
+#endif
+
+	snprintf(query, 128, "SELECT homedir FROM vhffs_users WHERE uid = %d", fuse_get_context()->uid);
+	res=vhffsfs_PGQuery(query);
+	if(!res) return NULL;
+
+	if(PQnfields(res) == 1 && PQntuples(res) == 1)  {
+		homedir = strdup(PQgetvalue(res, 0, 0));
+#ifdef WITH_CACHE
+		// store homedir to cache
+		vhffsfs_cache_uid_lock(vc);
+		if(vc->homedir) free(vc->homedir);
+		vc->homedir = strdup(homedir);
+		vc->homedir_arrival = vhffsfs_cache_arrival();
+		vhffsfs_cache_uid_unlock(vc);
+#endif
+	}
+
+
+	PQclear(res);
+	return homedir;
+}
+
+
+char *vhffsfs_getgroupdir(char *group)  {
+
+	char query[256];
+	PGresult *res;
+	char *groupname=NULL;
+	char *groupdir=NULL;
+	char *cur;
+	gid_t gid=0;
+
+	// check if group match ^[a-z0-9]+$ to prevent SQL injections
+	for(cur = group ; *cur ; cur++)
+ 		if(! ( (*cur >= 'a' && *cur <= 'z') || (*cur >= '0' && *cur <= '9') ) ) return NULL;
+
+#ifdef WITH_CACHE
+	// try to fetch groupdir from cache
+	vhffsfs_cache_uid *vc;
+	vhffsfs_cache_uid_group *vcg;
+
+ 	vc = vhffsfs_cache_uid_context();
+	vhffsfs_cache_uid_lock(vc);
+	vcg = vhffsfs_cache_uid_lookup_group(vc, group);
+	if(vcg)  {
+		char *t=NULL;
+		if(vcg->groupdir) t = strdup(vcg->groupdir);
+		vhffsfs_cache_uid_unlock(vc);
+		return t;
+	}
+	vhffsfs_cache_uid_unlock(vc);
+#endif
+
+	snprintf(query, 256, "SELECT g.gid,g.groupname FROM vhffs_users u, vhffs_groups g, vhffs_user_group ug WHERE u.uid = %d AND g.groupname = '%s' AND u.uid = ug.uid AND g.gid = ug.gid", fuse_get_context()->uid, group);
+	res=vhffsfs_PGQuery(query);
+	if(!res) return NULL;
+
+	if(PQnfields(res) == 2 && PQntuples(res) == 1) {
+		gid = atoi(PQgetvalue(res, 0, 0));
+		groupname = PQgetvalue(res, 0, 1);
+	}
+
+	if(groupname)  {
+		groupdir = malloc(strlen(vhffsfs.grouppath)+strlen(groupname)+6);
+		sprintf(groupdir, "%s/%c/%c/%s", vhffsfs.grouppath, groupname[0], groupname[1], groupname);
+	}
+
+#ifdef WITH_CACHE
+	// store groupdir to cache
+	vhffsfs_cache_uid_lock(vc);
+	vcg = vhffsfs_cache_uid_fetch_group(vc, group);
+	vcg->gid = gid;
+	if(vcg->groupdir) {
+		free(vcg->groupdir);
+		vcg->groupdir = NULL;
+	}
+	if(groupdir) vcg->groupdir = strdup(groupdir);
+	vcg->arrival = vhffsfs_cache_arrival();
+	vhffsfs_cache_uid_unlock(vc);
+#endif
+
+	PQclear(res);
+	return groupdir;
+}
+
+
+gid_t vhffsfs_getgroupgid(char *group)  {
+
+	char query[256];
+	PGresult *res;
+	gid_t gid=0;
+	char *cur;
+
+#ifdef WITH_CACHE
+	// try to fetch grouppid from cache
+	vhffsfs_cache_uid *vc;
+	vhffsfs_cache_uid_group *vcg;
+
+ 	vc = vhffsfs_cache_uid_context();
+	vhffsfs_cache_uid_lock(vc);
+	vcg = vhffsfs_cache_uid_lookup_group(vc, group);
+	if(vcg && vcg->gid)  {
+		gid = vcg->gid;
+		vhffsfs_cache_uid_unlock(vc);
+		return vcg->gid;
+	}
+	vhffsfs_cache_uid_unlock(vc);
+#endif
+
+	// check if group match ^[a-z0-9]+$ to prevent SQL injections
+	for(cur = group ; *cur ; cur++)
+		if(! ( (*cur >= 'a' && *cur <= 'z') || (*cur >= '0' && *cur <= '9') ) ) return 0;
+
+	snprintf(query, 256, "SELECT gid FROM vhffs_groups WHERE groupname = '%s'", group);
+	res=vhffsfs_PGQuery(query);
+	if(!res) return 0;
+
+	if(PQnfields(res) == 1 && PQntuples(res) == 1)
+		gid = atoi(PQgetvalue(res, 0, 0));
+
+	PQclear(res);
+	return gid;
+}
+
+
+char *vhffsfs_getwebdir(char *group, char *website)  {
+
+	char query[256];
+	PGresult *res;
+	char *servername=NULL;
+	char *webdir=NULL;
+	char *cur;
+
+	// check if group match ^[a-z0-9]+$ to prevent SQL injections
+	for(cur = group ; *cur ; cur++)
+ 		if(! ( (*cur >= 'a' && *cur <= 'z') || (*cur >= '0' && *cur <= '9') ) ) return NULL;
+
+	// check if website match ^[a-z0-9\-\.]+$ to prevent SQL injections
+	for(cur = website ; *cur ; cur++)
+ 		if(! ( (*cur >= 'a' && *cur <= 'z') || (*cur >= '0' && *cur <= '9') || *cur == '-' || *cur == '.') ) return NULL;
+
+	snprintf(query, 256, "SELECT servername FROM vhffs_httpd WHERE owner_gid = (SELECT gid FROM vhffs_groups WHERE groupname = '%s') AND servername = '%s'", group, website);
+	res=vhffsfs_PGQuery(query);
+	if(!res) return NULL;
+
+	if(PQnfields(res) == 1 && PQntuples(res) == 1)
+		servername = PQgetvalue(res, 0, 0);
+
+	if(servername)  {
+		MD5_CTX context;
+		unsigned char digest[16];
+
+		webdir = malloc(strlen(vhffsfs.webspath)+strlen(servername)+11);
+		MD5Init(&context);
+		MD5Update(&context, (unsigned char*)servername, strlen(servername));
+		MD5Final(digest, &context);
+		sprintf(webdir, "%s/%02x/%02x/%02x/%s", vhffsfs.webspath, digest[0], digest[1], digest[2], servername);
+	}
+
+	PQclear(res);
+	return webdir;
+}
+
+
+char *vhffsfs_getrepositorydir(char *group, char *name)  {
+
+	char query[256];
+	PGresult *res;
+	char *reponame=NULL;
+	char *repodir=NULL;
+	char *cur;
+
+	// check if group match ^[a-z0-9]+$ to prevent SQL injections
+	for(cur = group ; *cur ; cur++)
+ 		if(! ( (*cur >= 'a' && *cur <= 'z') || (*cur >= '0' && *cur <= '9') ) ) return NULL;
+
+	// check if name match ^[a-z0-9]+$ to prevent SQL injections
+	for(cur = name ; *cur ; cur++)
+ 		if(! ( (*cur >= 'a' && *cur <= 'z') || (*cur >= '0' && *cur <= '9') ) ) return NULL;
+
+	snprintf(query, 256, "SELECT name FROM vhffs_repository WHERE owner_gid = (SELECT gid FROM vhffs_groups WHERE groupname = '%s') AND name = '%s'", group, name);
+	res=vhffsfs_PGQuery(query);
+	if(!res) return NULL;
+
+	if(PQnfields(res) == 1 && PQntuples(res) == 1)
+		reponame = PQgetvalue(res, 0, 0);
+
+	if(reponame)  {
+		repodir = malloc(strlen(vhffsfs.repositoriespath)+strlen(reponame)+2);
+		sprintf(repodir, "%s/%s", vhffsfs.repositoriespath, reponame);
+	}
+
+	PQclear(res);
+	return repodir;
+}
+
+
+char **vhffsfs_getusergroups(uid_t uid)  {
+
+	char query[256];
+	PGresult *res;
+	char **groups=NULL;
+
+	snprintf(query, 256, "SELECT g.groupname FROM vhffs_users u, vhffs_groups g, vhffs_user_group ug WHERE u.uid = %d AND u.uid = ug.uid AND g.gid = ug.gid", uid);
+	res=vhffsfs_PGQuery(query);
+	if(!res) return NULL;
+
+	if(PQnfields(res) == 1)  {
+		int i, x;
+		groups = malloc((PQntuples(res)*2+1)*sizeof(char*));
+		*(groups+PQntuples(res)*2) = '\0';
+
+		for(i = x = 0 ; i < PQntuples(res) ; i++, x+=2)  {
+			char *groupname = PQgetvalue(res, i, 0);
+			if(groupname)  {
+				char *groupdir = malloc(strlen(vhffsfs.grouppath)+strlen(groupname)+6);
+				sprintf(groupdir, "%s/%c/%c/%s", vhffsfs.grouppath, groupname[0], groupname[1], groupname);
+				*(groups+x) = strdup(groupname);
+				*(groups+x+1) = groupdir;
+			}
+		}
+	}
+
+	PQclear(res);
+	return groups;
+}
+
+
+char **vhffsfs_getgroupservices(gid_t gid)  {
+
+	char query[256];
+	PGresult *res1,*res2;
+	char **services=NULL;
+
+	// fetch websites
+	snprintf(query, 256, "SELECT servername FROM vhffs_httpd WHERE owner_gid = %d", gid);
+	res1=vhffsfs_PGQuery(query);
+	if(!res1) return NULL;
+
+	// fetch repositories
+	snprintf(query, 256, "SELECT name FROM vhffs_repository WHERE owner_gid = %d", gid);
+	res2=vhffsfs_PGQuery(query);
+	if(!res2) {
+		PQclear(res1);
+		return NULL;
+	}
+
+	if(PQnfields(res1) == 1  &&  PQnfields(res2) == 1)  {
+		int i, x=0;
+		int nb = PQntuples(res1)+PQntuples(res2);
+
+		services = malloc((nb*2+1)*sizeof(char*) );
+		*(services+nb*2) = '\0';
+
+		// websites
+		for(i = 0 ; i < PQntuples(res1) ; i++, x+=2)  {
+			char *servername = PQgetvalue(res1, i, 0);
+			if(servername)  {
+				char *displayedname;
+				char *webdir;
+				MD5_CTX context;
+				unsigned char digest[16];
+
+				displayedname = malloc(strlen(servername)+5);
+				sprintf(displayedname, "%s-web", servername);
+
+				webdir = malloc(strlen(vhffsfs.webspath)+strlen(servername)+11);
+				MD5Init(&context);
+				MD5Update(&context, (unsigned char*)servername, strlen(servername));
+				MD5Final(digest, &context);
+				sprintf(webdir, "%s/%02x/%02x/%02x/%s", vhffsfs.webspath, digest[0], digest[1], digest[2], servername);
+
+				*(services+x) = displayedname;
+				*(services+x+1) = webdir;
+			}
+		}
+
+		// repositories
+		for(i = 0 ; i < PQntuples(res2) ; i++, x+=2)  {
+			char *reponame = PQgetvalue(res2, i, 0);
+			if(reponame)  {
+				char *displayedname;
+				char *repodir;
+
+ 				displayedname = malloc(strlen(reponame)+12);
+				sprintf(displayedname, "%s-repository", reponame);
+
+				repodir = malloc(strlen(vhffsfs.repositoriespath)+strlen(reponame)+2);
+				sprintf(repodir, "%s/%s", vhffsfs.repositoriespath, reponame);
+
+				*(services+x) = displayedname;
+				*(services+x+1) = repodir;
+			}
+		}
+	}
+
+	PQclear(res1);
+	PQclear(res2);
+	return services;
+}
+
+
+// return the real path
+char *vhffsfs_realpath(const char *path, uid_t *uid, gid_t *gid)  {
+	char *begin, *cur;
+	char *first=NULL;
+	char *homedir;
+	char *rpath=NULL;
+
+	printf("======= PATH: %s\n", path);
+	printf("======= UID: %d\n", fuse_get_context()->uid);
+	printf("======= GID: %d\n", fuse_get_context()->gid);
+
+	// path empty
+	if(*(path+0) == '\0') return NULL;
+	// set uid and gid
+	if(uid) *uid = fuse_get_context()->uid;
+	if(gid) *gid = fuse_get_context()->gid;
+	// path relative
+	if(*(path+0) == '.') return strdup(path);
+	// fetch homedir
+	homedir = vhffsfs_gethomedir();
+	if(!homedir) return NULL;
+	// -- parse path
+	// /                   | home directory ----------------------------|
+	// /first              | home file | group directory ---------------|
+	// /first/second       | home file | group file | service directory |
+	// /first/second/third | home file | group file | service file -----|
+
+	// get first node
+	cur = (char*)path;
+	if(*cur++)  {
+		for(begin = cur ; *cur && *cur != '/'; cur++);
+		if(cur != begin) first = strndup(begin, cur - begin);
+	}
+
+	// home directory
+	if(!first) rpath = strdup(homedir);
+
+	// FIRST EXIST BELOW
+
+	// so, try to fetch group directory
+	else {
+		char *groupdir = vhffsfs_getgroupdir(first);
+
+		// home file
+		if(!groupdir)  {
+			rpath = malloc(strlen(homedir)+strlen(begin)+2);
+			sprintf(rpath, "%s/%s", homedir, begin);
+		}
+
+		else {
+			char *groupname = first;
+			char *second=NULL;
+
+			// set gid
+			if(gid) *gid = vhffsfs_getgroupgid(groupname);
+
+			// get second node
+			if(*cur++) {
+				for(begin = cur ; *cur && *cur != '/'; cur++);
+				if(cur != begin) second = strndup(begin, cur - begin);
+			}
+
+			// group directory
+			if(!second) rpath = strdup(groupdir);
+
+			// SECOND EXIST BELOW
+
+			// so, try to fetch service directory
+			else {
+				char *servicedir=NULL;
+				char *servicetype, *servicename;
+
+				// fetch servicetype and servicename
+				for(servicetype = servicename = second ; *servicetype ; servicetype++);
+				for(; servicetype > second && *servicetype != '-' ; servicetype--);
+				if(*servicetype == '-') *servicetype++ = '\0';
+
+				if(!strcmp(servicetype, "web")) {
+					servicedir = vhffsfs_getwebdir(groupname, servicename);
+				} else if(!strcmp(servicetype, "repository")) {
+					servicedir = vhffsfs_getrepositorydir(groupname, servicename);
+				}
+
+				// group file
+				if(!servicedir)  {
+					rpath = malloc(strlen(groupdir)+strlen(begin)+2);
+					sprintf(rpath, "%s/%s", groupdir, begin);
+				}
+
+				else {
+					char *third=NULL;
+
+					// get third node
+					if(*cur++) third = strdup(cur);
+
+					// service directory
+					if(!third) rpath = strdup(servicedir);
+
+					// THIRD EXIST BELOW
+					else {
+						rpath = malloc(strlen(servicedir)+strlen(third)+2);
+						sprintf(rpath, "%s/%s", servicedir, third);
+
+						free(third);
+					}
+					free(servicedir);
+				}
+				free(second);
+			}
+			free(groupdir);
+		}
+		free(first);
+	}
+	free(homedir);
+
+	printf("======= RETURN: %s\n\n", rpath);
+	return rpath;
+}
+
+
+char **vhffsfs_virtualdirs(const char *path)  {
+
+	char *groupname=NULL;
+	char **vdirs=NULL;
+
+	// path empty
+	if(*(path+0) == '\0') return NULL;
+	// get groupname
+	if(*(path+1) != '\0') groupname = (char*)path+1;
+
+	if(!groupname) vdirs = vhffsfs_getusergroups(fuse_get_context()->uid);
+
+	else  {
+		gid_t gid = vhffsfs_getgroupgid(groupname);
+		if(gid > 0) vdirs = vhffsfs_getgroupservices(gid);
+	}
+
+	return vdirs;
+}
+
+
+static void *vhffsfs_init(void)
+{
+	return NULL;
+}
+
+
+static int vhffsfs_getattr(const char *path, struct stat *stbuf)
+{
+	int res;
+	char *rpath;
+
+	rpath = vhffsfs_realpath(path, NULL, NULL);
+	if(!rpath) return -ENOENT;
+
+	res = lstat(rpath, stbuf);
+	free(rpath);
+
+	if(res == -1) return -errno;
+	// force find -noleaf
+	stbuf->st_nlink = 1;
+	return 0;
+}
+
+
+static int vhffsfs_fgetattr(const char *path, struct stat *stbuf, struct fuse_file_info *fi)
+{
+	int res;
+
+	(void) path;
+
+	res = fstat(fi->fh, stbuf);
+
+	if(res == -1) return -errno;
+	// force find -noleaf
+	stbuf->st_nlink = 1;
+	return 0;
+}
+
+
+static int vhffsfs_access(const char *path, int mask)
+{
+	int res;
+	char *rpath;
+
+	rpath = vhffsfs_realpath(path, NULL, NULL);
+	if(!rpath) return -ENOENT;
+
+	res = access(rpath, mask);
+	free(rpath);
+
+	if(res == -1) return -errno;
+	return 0;
+}
+
+
+static int vhffsfs_readlink(const char *path, char *buf, size_t size)
+{
+	int res;
+	char *rpath;
+
+	rpath = vhffsfs_realpath(path, NULL, NULL);
+	if(!rpath) return -ENOENT;
+
+	res = readlink(rpath, buf, size - 1);
+	free(rpath);
+
+	if(res == -1) return -errno;
+	buf[res] = '\0';
+	return 0;
+}
+
+
+static int vhffsfs_opendir(const char *path, struct fuse_file_info *fi)
+{
+	char *rpath;
+
+	rpath = vhffsfs_realpath(path, NULL, NULL);
+	if(!rpath) return -ENOENT;
+
+	DIR *dp = opendir(rpath);
+	free(rpath);
+
+	if(dp == NULL) return -errno;
+	fi->fh = (unsigned long) dp;
+	return 0;
+}
+
+
+static inline DIR *get_dirp(struct fuse_file_info *fi)
+{
+	return (DIR *) (uintptr_t) fi->fh;
+}
+
+
+static int vhffsfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi)
+{
+	DIR *dp = get_dirp(fi);
+	struct dirent *de;
+	char **vdirs;
+
+	//(void) path;
+	seekdir(dp, offset);
+	while ((de = readdir(dp)) != NULL) {
+		struct stat st;
+		memset(&st, 0, sizeof(st));
+		st.st_ino = de->d_ino;
+		st.st_mode = de->d_type << 12;
+		if (filler(buf, de->d_name, &st, telldir(dp)))
+			break;
+	}
+
+	// add virtual directories
+	vdirs = vhffsfs_virtualdirs(path);
+	if(vdirs)  {
+		char **cur;
+		for(cur = vdirs ; *cur ; cur+=2)  {
+			char *vname = *cur;
+			char *vpath = *(cur+1);
+
+			struct stat st;
+			stat(vpath, &st);
+			filler(buf, vname, &st, 0);
+
+			free(vname);
+			free(vpath);
+		}
+		free(vdirs);
+	}
+
+	return 0;
+}
+
+
+static int vhffsfs_releasedir(const char *path, struct fuse_file_info *fi)
+{
+	DIR *dp = get_dirp(fi);
+	(void) path;
+	closedir(dp);
+	return 0;
+}
+
+
+static int vhffsfs_mknod(const char *path, mode_t mode, dev_t rdev)
+{
+/* -- DISABLED - user must not be allowed to create that !
+	int res;
+	char *rpath;
+
+	rpath = vhffsfs_realpath(path, NULL, NULL);
+	if(!rpath) return -ENOENT;
+
+	if (S_ISFIFO(mode))
+		res = mkfifo(rpath, mode);
+	else
+		res = mknod(rpath, mode, rdev);
+
+	if(res == -1) return -errno;
+	return 0;
+*/
+	return -EACCES;
+}
+
+
+static int vhffsfs_mkdir(const char *path, mode_t mode)
+{
+	int res;
+	char *rpath;
+	uid_t uid;
+	gid_t gid;
+
+	rpath = vhffsfs_realpath(path, &uid, &gid);
+	if(!rpath) return -ENOENT;
+
+	res = mkdir(rpath, mode);
+	if(res == 0) {
+		struct stat st;
+		lchown(rpath, uid, gid);
+		stat(rpath, &st);
+		chmod(rpath, st.st_mode | vhffsfs.minimumrightdir);
+	}
+	free(rpath);
+
+	if(res == -1) return -errno;
+	return 0;
+}
+
+
+static int vhffsfs_unlink(const char *path)
+{
+	int res;
+	char *rpath;
+
+	rpath = vhffsfs_realpath(path, NULL, NULL);
+	if(!rpath) return -ENOENT;
+
+	res = unlink(rpath);
+	free(rpath);
+
+	if(res == -1) return -errno;
+	return 0;
+}
+
+
+static int vhffsfs_rmdir(const char *path)
+{
+	int res;
+	char *rpath;
+
+	rpath = vhffsfs_realpath(path, NULL, NULL);
+	if(!rpath) return -ENOENT;
+
+	res = rmdir(rpath);
+	free(rpath);
+
+	if(res == -1) return -errno;
+	return 0;
+}
+
+
+static int vhffsfs_symlink(const char *from, const char *to)
+{
+	int res;
+	char *rfrom, *rto;
+	uid_t uid;
+	gid_t gid;
+
+	rfrom = vhffsfs_realpath(from, NULL, NULL);
+	if(!rfrom) return -ENOENT;
+
+	rto = vhffsfs_realpath(to, &uid, &gid);
+	if(!rto) {
+		free(rfrom);
+		return -ENOENT;
+	}
+
+	res = symlink(rfrom, rto);
+	if(res == 0) lchown(rto, uid, gid);
+	free(rfrom);
+	free(rto);
+
+	if(res == -1) return -errno;
+	return 0;
+}
+
+
+static int vhffsfs_rename(const char *from, const char *to)
+{
+	int res;
+	char *rfrom, *rto;
+
+	rfrom = vhffsfs_realpath(from, NULL, NULL);
+	if(!rfrom) return -ENOENT;
+
+	rto = vhffsfs_realpath(to, NULL, NULL);
+	if(!rto) {
+		free(rfrom);
+		return -ENOENT;
+	}
+
+	res = rename(rfrom, rto);
+	free(rfrom);
+	free(rto);
+
+	if(res == -1) return -errno;
+	return 0;
+}
+
+
+static int vhffsfs_link(const char *from, const char *to)
+{
+/* -- DISABLED - user must not be allowed to create that !
+	int res;
+	char *rfrom, *rto;
+	uid_t uid;
+	gid_t gid;
+
+	rfrom = vhffsfs_realpath(from, NULL, NULL);
+	if(!rfrom) return -ENOENT;
+
+	rto = vhffsfs_realpath(to, NULL, NULL);
+	if(!rto) {
+		free(rfrom);
+		return -ENOENT;
+	}
+
+	res = link(rfrom, rto);
+	if(res == 0) lchown(rto, uid, gid);
+	free(rfrom);
+	free(rto);
+
+	if(res == -1) return -errno;
+	return 0;
+*/
+	return -EACCES;
+}
+
+
+static int vhffsfs_chmod(const char *path, mode_t mode)
+{
+	int res;
+	char *rpath;
+	struct stat st;
+
+	rpath = vhffsfs_realpath(path, NULL, NULL);
+	if(!rpath) return -ENOENT;
+
+	stat(rpath, &st);
+	res = chmod(rpath, mode | (S_ISDIR(st.st_mode) ? vhffsfs.minimumrightdir : vhffsfs.minimumrightfile) );
+	free(rpath);
+
+	if(res == -1) return -errno;
+	return 0;
+}
+
+
+static int vhffsfs_chown(const char *path, uid_t uid, gid_t gid)
+{
+	int res;
+	char *rpath;
+
+	rpath = vhffsfs_realpath(path, NULL, NULL);
+	if(!rpath) return -ENOENT;
+
+	res = lchown(rpath, uid, gid);
+	free(rpath);
+
+	if(res == -1) return -errno;
+	return 0;
+}
+
+
+static int vhffsfs_truncate(const char *path, off_t size)
+{
+	int res;
+	char *rpath;
+
+	rpath = vhffsfs_realpath(path, NULL, NULL);
+	if(!rpath) return -ENOENT;
+
+	res = truncate(rpath, size);
+	free(rpath);
+
+	if(res == -1) return -errno;
+	return 0;
+}
+
+
+static int vhffsfs_ftruncate(const char *path, off_t size, struct fuse_file_info *fi)
+{
+	int res;
+	(void) path;
+
+	res = ftruncate(fi->fh, size);
+
+	if(res == -1) return -errno;
+	return 0;
+}
+
+
+static int vhffsfs_utime(const char *path, struct utimbuf *buf)
+{
+	int res;
+	char *rpath;
+
+	rpath = vhffsfs_realpath(path, NULL, NULL);
+	if(!rpath) return -ENOENT;
+
+	res = utime(rpath, buf);
+	free(rpath);
+
+	if(res == -1) return -errno;
+	return 0;
+}
+
+
+static int vhffsfs_create(const char *path, mode_t mode, struct fuse_file_info *fi)
+{
+	int fd;
+	char *rpath;
+	uid_t uid;
+	gid_t gid;
+
+	rpath = vhffsfs_realpath(path, &uid, &gid);
+	if(!rpath) return -ENOENT;
+
+	fd = open(rpath, fi->flags, mode);
+	if(fd >= 0) {
+		struct stat st;
+		lchown(rpath, uid, gid);
+		stat(rpath, &st);
+		chmod(rpath, st.st_mode | vhffsfs.minimumrightfile);
+	}
+	free(rpath);
+
+	if(fd == -1) return -errno;
+	fi->fh = fd;
+	return 0;
+}
+
+
+static int vhffsfs_open(const char *path, struct fuse_file_info *fi)
+{
+	int fd;
+	char *rpath;
+
+	rpath = vhffsfs_realpath(path, NULL, NULL);
+	if(!rpath) return -ENOENT;
+
+	fd = open(rpath, fi->flags);
+	free(rpath);
+
+	if(fd == -1) return -errno;
+	fi->fh = fd;
+	return 0;
+}
+
+
+static int vhffsfs_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi)
+{
+	int res;
+
+	(void) path;
+	res = pread(fi->fh, buf, size, offset);
+
+	if(res == -1) res = -errno;
+	return res;
+}
+
+
+static int vhffsfs_write(const char *path, const char *buf, size_t size, off_t offset, struct fuse_file_info *fi)
+{
+	int res;
+
+	(void) path;
+	res = pwrite(fi->fh, buf, size, offset);
+
+	if(res == -1) res = -errno;
+	return res;
+}
+
+
+static int vhffsfs_statfs(const char *path, struct statvfs *stbuf)
+{
+	int res;
+	char *rpath;
+
+	rpath = vhffsfs_realpath(path, NULL, NULL);
+	if(!rpath) return -ENOENT;
+
+	res = statvfs(rpath, stbuf);
+	free(rpath);
+
+	if(res == -1) return -errno;
+	return 0;
+}
+
+
+static int vhffsfs_release(const char *path, struct fuse_file_info *fi)
+{
+	(void) path;
+	close(fi->fh);
+
+	return 0;
+}
+
+
+static int vhffsfs_fsync(const char *path, int isdatasync, struct fuse_file_info *fi)
+{
+	int res;
+	(void) path;
+
+#ifndef HAVE_FDATASYNC
+	(void) isdatasync;
+#else
+	if(isdatasync)
+		res = fdatasync(fi->fh);
+	else
+#endif
+		res = fsync(fi->fh);
+
+	if(res == -1) return -errno;
+	return 0;
+}
+
+
+#ifdef HAVE_SETXATTR
+/* xattr operations are optional and can safely be left unimplemented */
+static int vhffsfs_setxattr(const char *path, const char *name, const char *value, size_t size, int flags)
+{
+	int res;
+	char *rpath;
+
+	rpath = vhffsfs_realpath(path, NULL, NULL);
+	if(!rpath) return -ENOENT;
+
+	res = lsetxattr(rpath, name, value, size, flags);
+	free(rpath);
+
+	if(res == -1) return -errno;
+	return 0;
+}
+
+
+static int vhffsfs_getxattr(const char *path, const char *name, char *value, size_t size)
+{
+	int res;
+	char *rpath;
+
+	rpath = vhffsfs_realpath(path, NULL, NULL);
+	if(!rpath) return -ENOENT;
+
+	res = lgetxattr(rpath, name, value, size);
+	free(rpath);
+
+	if(res == -1) return -errno;
+	return res;
+}
+
+
+static int vhffsfs_listxattr(const char *path, char *list, size_t size)
+{
+	int res;
+	char *rpath;
+
+	rpath = vhffsfs_realpath(path, NULL, NULL);
+	if(!rpath) return -ENOENT;
+
+	res = llistxattr(rpath, list, size);
+	free(rpath);
+
+	if(res == -1) return -errno;
+	return res;
+}
+
+
+static int vhffsfs_removexattr(const char *path, const char *name)
+{
+	int res;
+	char *rpath;
+
+	rpath = vhffsfs_realpath(path, NULL, NULL);
+	if(!rpath) return -ENOENT;
+
+	res = lremovexattr(rpath, name);
+	free(rpath);
+
+	if(res == -1) return -errno;
+	return 0;
+}
+#endif /* HAVE_SETXATTR */
+
+
+static struct fuse_operations vhffsfs_oper = {
+	.init		= vhffsfs_init,
+	.getattr	= vhffsfs_getattr,
+	.fgetattr	= vhffsfs_fgetattr,
+	.access		= vhffsfs_access,
+	.readlink	= vhffsfs_readlink,
+	.opendir	= vhffsfs_opendir,
+	.readdir	= vhffsfs_readdir,
+	.releasedir	= vhffsfs_releasedir,
+	.mknod		= vhffsfs_mknod,
+	.mkdir		= vhffsfs_mkdir,
+	.symlink	= vhffsfs_symlink,
+	.unlink		= vhffsfs_unlink,
+	.rmdir		= vhffsfs_rmdir,
+	.rename		= vhffsfs_rename,
+	.link		= vhffsfs_link,
+	.chmod		= vhffsfs_chmod,
+	.chown		= vhffsfs_chown,
+	.truncate	= vhffsfs_truncate,
+	.ftruncate	= vhffsfs_ftruncate,
+	.utime		= vhffsfs_utime,
+	.create		= vhffsfs_create,
+	.open		= vhffsfs_open,
+	.read		= vhffsfs_read,
+	.write		= vhffsfs_write,
+	.statfs		= vhffsfs_statfs,
+	.release	= vhffsfs_release,
+	.fsync		= vhffsfs_fsync,
+#ifdef HAVE_SETXATTR
+	.setxattr	= vhffsfs_setxattr,
+	.getxattr	= vhffsfs_getxattr,
+	.listxattr	= vhffsfs_listxattr,
+	.removexattr	= vhffsfs_removexattr,
+#endif
+};
+
+int vhffsfs_readconfig(char *conffile)  {
+
+	FILE *conf;
+	char line[128];
+
+	conf = fopen(conffile, "r");
+	if(!conf) return -1;
+
+	while( fgets(line, 128, conf) )  {
+		line[strlen(line)-1] = '\0';
+		if(!strncmp("grouppath ", line, 10))  {
+			vhffsfs.grouppath = strdup(line+10);
+		}
+		else if(!strncmp("webspath ", line, 9))  {
+			vhffsfs.webspath = strdup(line+9);
+		}
+		else if(!strncmp("repositoriespath ", line, 17))  {
+			vhffsfs.repositoriespath = strdup(line+17);
+		}
+		else if(!strncmp("dbhost ", line, 7))  {
+			vhffsfs.dbhost = strdup(line+7);
+		}
+		else if(!strncmp("dbport ", line, 7))  {
+			vhffsfs.dbport = atoi(line+7);
+		}
+		else if(!strncmp("dbuser ", line, 7))  {
+			vhffsfs.dbuser = strdup(line+7);
+		}
+		else if(!strncmp("dbpass ", line, 7))  {
+			vhffsfs.dbpass = strdup(line+7);
+		}
+		else if(!strncmp("dbname ", line, 7))  {
+			vhffsfs.dbname = strdup(line+7);
+		}
+		else if(!strncmp("dbtimeout ", line, 10))  {
+			vhffsfs.dbtimeout = atoi(line+10);
+		}
+		else if(!strncmp("minimumrightfile ", line, 17))  {
+                     vhffsfs.minimumrightfile = strtoul(line+17, NULL, 8);
+		}
+		else if(!strncmp("minimumrightdir ", line, 16))  {
+                     vhffsfs.minimumrightdir = strtoul(line+16, NULL, 8);
+		}
+	}
+
+	fclose(conf);
+	return 0;
+}
+
+int main(int argc, char *argv[])
+{
+	int i;
+	for(i = 0 ; i < VHFFSFS_MAXCONNDB ; i++)   {
+		vhffsfs.pg_conn[i] = NULL;
+		pthread_mutex_init(&vhffsfs.pg_lock[i], NULL);
+	}
+	vhffsfs.grouppath = "/data/groups";
+	vhffsfs.webspath = "/data/web";
+	vhffsfs.repositoriespath = "/data/repository";
+	vhffsfs.dbhost = "127.0.0.1";
+	vhffsfs.dbport = 5432;
+	vhffsfs.dbuser = "vhffs";
+	vhffsfs.dbpass = "pass";
+	vhffsfs.dbname = "vhffs";
+	vhffsfs.dbtimeout = 30;
+//	vhffsfs.defaultrightfile = 00664;
+//	vhffsfs.defaultrightdir = 02775;
+	vhffsfs.minimumrightfile = 00400;
+	vhffsfs.minimumrightdir = 02700;
+#ifdef WITH_CACHE
+	vhffsfs.cache = g_hash_table_new(g_int_hash, g_int_equal);
+#endif
+
+	vhffsfs_readconfig(VHFFSFS_CONFIG);
+
+	// generate dbconninfo
+	vhffsfs.dbconninfo = malloc(256);
+	snprintf(vhffsfs.dbconninfo, 256, "host='%s' port='%d' user='%s' password='%s' dbname='%s' connect_timeout='%d'", vhffsfs.dbhost, vhffsfs.dbport, vhffsfs.dbuser, vhffsfs.dbpass, vhffsfs.dbname, vhffsfs.dbtimeout);
+	vhffsfs.dbconninfo = realloc(vhffsfs.dbconninfo, strlen(vhffsfs.dbconninfo)+1);
+
+	umask(0);
+	return fuse_main(argc, argv, &vhffsfs_oper);
+}

Added: trunk/vhffs-tools/src/vhffsfs_compil.sh
===================================================================
--- trunk/vhffs-tools/src/vhffsfs_compil.sh	2007-07-05 07:49:46 UTC (rev 662)
+++ trunk/vhffs-tools/src/vhffsfs_compil.sh	2007-07-05 11:23:50 UTC (rev 663)
@@ -0,0 +1,2 @@
+gcc -I/usr/include/fuse `pkg-config --cflags glib-2.0` -D_FILE_OFFSET_BITS=64 -D_REENTRANT -DFUSE_USE_VERSION=25 -Wall -ggdb -lfuse -lpthread -lpq `pkg-config --libs glib-2.0` vhffsfs.c md5c.c -o vhffsfs
+


Property changes on: trunk/vhffs-tools/src/vhffsfs_compil.sh
___________________________________________________________________
Name: svn:executable
   + *

Added: trunk/vhffs-tools/src/vhffsfs_run.sh
===================================================================
--- trunk/vhffs-tools/src/vhffsfs_run.sh	2007-07-05 07:49:46 UTC (rev 662)
+++ trunk/vhffs-tools/src/vhffsfs_run.sh	2007-07-05 11:23:50 UTC (rev 663)
@@ -0,0 +1,2 @@
+./vhffsfs -d -o allow_other,default_permissions,kernel_cache /mnt/fuse/
+


Property changes on: trunk/vhffs-tools/src/vhffsfs_run.sh
___________________________________________________________________
Name: svn:executable
   + *


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/