updates to read write support
authorroot <root@test1n1.VSOFS1.COM>
Tue, 11 Nov 2008 00:53:08 +0000 (11:53 +1100)
committerroot <root@test1n1.VSOFS1.COM>
Tue, 11 Nov 2008 00:53:08 +0000 (11:53 +1100)
migrate/remote-cache.c

index 6d71ffc1498778d5540972482f145288f91830d3..7bf4b538b249b2517b945fcebb9630b94ea7cb02 100644 (file)
@@ -161,9 +161,16 @@ static int rate_limit_is_ok(void)
 
 static void unlink_object(const char *path)
 {
-//qqq should rename and do a recursive delete here in a child
-//qqq to handle when this is a directory tree
-       unlink(path);
+       TALLOC_CTX *mem_ctx = talloc_new(NULL);
+       char *cmd;
+       int ret;
+
+       cmd = talloc_asprintf(mem_ctx, "rm -rf %s", path);
+       ret = system(cmd);
+       if (ret != 0) {
+               DEBUG(DEBUG_DEBUG,("%s failed with ret:%d\n", cmd, ret));
+       }
+       talloc_free(mem_ctx);
 }
 
 
@@ -311,6 +318,31 @@ int open_parent_cache_ro(TALLOC_CTX *mem_ctx, const char *path)
        return cache_fd;
 }
 
+int unlink_parent_dir_cache(TALLOC_CTX *mem_ctx, const char *path)
+{
+       char *ppath;
+       char *sptr;
+       char *cache_path;
+       int ret;
+
+       ppath = talloc_strdup(mem_ctx, path);
+       sptr = rindex(ppath, '/');
+       if (sptr == NULL) {
+               return -1;
+       }
+       *sptr = 0;
+       if (ppath[0] == 0) {
+               return -1;
+       }
+
+       cache_path   = talloc_asprintf(mem_ctx, "%s/%s", ppath, CACHE_DIR_NAME);
+
+       ret = unlink(cache_path);
+       DEBUG(DEBUG_DEBUG,("unlink_parent_dir_cache(%s) == %d\n", cache_path, ret));
+       return ret;
+}
+
+
 int lookup_attribute_in_parent(TALLOC_CTX *mem_ctx, const char *path, struct stat *st)
 {
        int cache_fd = -1;
@@ -706,7 +738,7 @@ static int remote_cache_statfs(const char *path, struct statvfs *buf)
        char *old_path = NULL;
        int ret;
 
-       DEBUG(DEBUG_DEBUG, (__location__ " STATFS called\n"));
+//qqq  DEBUG(DEBUG_DEBUG, (__location__ " STATFS called\n"));
        old_path   = talloc_asprintf(mem_ctx, "%s/%s", remote, path);
        ret = statvfs(old_path, buf);
        if (ret == -1) {
@@ -1218,6 +1250,7 @@ static int remote_cache_mknod(const char *path, mode_t mode, dev_t rdev)
        */
        switch_back_to_root();
        unlink_object(new_path);
+       unlink_parent_dir_cache(mem_ctx, new_path);
 
 finished:
        switch_back_to_root();
@@ -1263,6 +1296,7 @@ static int remote_cache_mkdir(const char *path, mode_t mode)
        */
        switch_back_to_root();
        unlink_object(new_path);
+       unlink_parent_dir_cache(mem_ctx, new_path);
 
 finished:
        switch_back_to_root();
@@ -1307,6 +1341,7 @@ static int remote_cache_rmdir(const char *path)
        */
        switch_back_to_root();
        unlink_object(new_path);
+       unlink_parent_dir_cache(mem_ctx, new_path);
 
 finished:
        switch_back_to_root();
@@ -1339,7 +1374,7 @@ static int remote_cache_unlink(const char *path)
                goto finished;
        }
 
-
+       /* unlink the file on the remote site */
        ret = unlink(old_path);
        if (ret != 0) {
                DEBUG(DEBUG_DEBUG,(__location__ " UNLINK %s failed with errno:%u\n", old_path, errno));
@@ -1348,10 +1383,11 @@ static int remote_cache_unlink(const char *path)
        }
 
        /* if the unlink was successful we try to delete any local cached
-          entries for this name
+          entries for this name and remove the cache for this directory
        */
        switch_back_to_root();
        unlink_object(new_path);
+       unlink_parent_dir_cache(mem_ctx, new_path);
 
 finished:
        switch_back_to_root();
@@ -1395,6 +1431,7 @@ static int remote_cache_chmod(const char *path, mode_t mode)
        */
        switch_back_to_root();
        unlink_object(new_path);
+       unlink_parent_dir_cache(mem_ctx, new_path);
 
 finished:
        switch_back_to_root();
@@ -1438,6 +1475,7 @@ static int remote_cache_chown(const char *path, uid_t uid, gid_t gid)
        */
        switch_back_to_root();
        unlink_object(new_path);
+       unlink_parent_dir_cache(mem_ctx, new_path);
 
 finished:
        switch_back_to_root();
@@ -1470,7 +1508,7 @@ static int remote_cache_create(const char *path, mode_t mode, struct fuse_file_i
 
 
        ret = creat(old_path, mode);
-       if (ret != 0) {
+       if (ret == -1) {
                DEBUG(DEBUG_DEBUG,(__location__ " CREATE %s mode %o failed with errno:%u\n", old_path, mode, errno));
                ret = -errno;
                goto finished;
@@ -1479,8 +1517,10 @@ static int remote_cache_create(const char *path, mode_t mode, struct fuse_file_i
        /* if the creat was successful we try to delete any local cached
           entries for this name
        */
+       ret = 0;
        switch_back_to_root();
        unlink_object(new_path);
+       unlink_parent_dir_cache(mem_ctx, new_path);
 
 finished:
        switch_back_to_root();
@@ -1524,6 +1564,7 @@ static int remote_cache_utime(const char *path, struct utimbuf *times)
        */
        switch_back_to_root();
        unlink_object(new_path);
+       unlink_parent_dir_cache(mem_ctx, new_path);
 
 finished:
        switch_back_to_root();
@@ -1577,6 +1618,7 @@ static int remote_cache_write(const char *path, const char *buf, size_t size, of
        */
        switch_back_to_root();
        unlink_object(new_path);
+       unlink_parent_dir_cache(mem_ctx, new_path);
 
 finished:
        switch_back_to_root();
@@ -1623,6 +1665,7 @@ static int remote_cache_truncate(const char *path, off_t offset)
        */
        switch_back_to_root();
        unlink_object(new_path);
+       unlink_parent_dir_cache(mem_ctx, new_path);
 
 finished:
        switch_back_to_root();
@@ -1666,6 +1709,7 @@ static int remote_cache_ftruncate(const char *path, off_t offset, struct fuse_fi
        */
        switch_back_to_root();
        unlink_object(new_path);
+       unlink_parent_dir_cache(mem_ctx, new_path);
 
 finished:
        switch_back_to_root();
@@ -1712,6 +1756,7 @@ static int remote_cache_link(const char *path, const char *newpath)
        */
        switch_back_to_root();
        unlink_object(new_newpath);
+       unlink_parent_dir_cache(mem_ctx, new_newpath);
 
 finished:
        switch_back_to_root();
@@ -1755,6 +1800,7 @@ static int remote_cache_symlink(const char *linkname, const char *path)
        */
        switch_back_to_root();
        unlink_object(new_newpath);
+       unlink_parent_dir_cache(mem_ctx, new_newpath);
 
 finished:
        switch_back_to_root();
@@ -1800,7 +1846,9 @@ static int remote_cache_rename(const char *path, const char *newpath)
           entries for this name
        */
        switch_back_to_root();
+       unlink_object(new_path);
        unlink_object(new_newpath);
+       unlink_parent_dir_cache(mem_ctx, new_path);
 
 finished:
        switch_back_to_root();
@@ -1809,6 +1857,86 @@ finished:
 }
 
 
+static int remote_cache_lock(const char *path, struct fuse_file_info *ffi, int cmd, struct flock *fl)
+{
+       int fd;
+       int ret;
+
+       if (!read_write) {
+               return -EROFS;
+       }
+
+       DEBUG(DEBUG_DEBUG, ("LOCK %s cmd:%s(%d)\n", path, cmd==F_GETLK?"F_GETLK":cmd==F_SETLKW?"F_SETLKW":cmd==F_SETLK?"F_SETLK":"unknown", cmd));
+
+       ret = switch_to_real_user();
+       if (ret != 0) {
+               DEBUG(DEBUG_ERR, (__location__ " SETEUID failed\n"));
+               goto finished;
+       }
+
+       fd = *((int *)(&ffi->fh));
+       if (fd == -1) {
+               DEBUG(DEBUG_ERR,(__location__ " ERROR file to lock is not open %s\n", path));
+               ret = -errno;
+               goto finished;
+       }
+
+       ret = fcntl(fd, cmd, fl);
+       if (ret == -1) {
+               DEBUG(DEBUG_ERR,(__location__ " FCNTL lock failed with errno:%d for fd:%d for file %s\n", errno, fd, path));
+               ret = -errno;
+               goto finished;
+       }
+
+finished:
+       switch_back_to_root();
+       return ret;
+}
+
+
+static int remote_cache_open(const char *path, struct fuse_file_info *ffi)
+{
+       TALLOC_CTX *mem_ctx = talloc_new(NULL);
+       char *old_path = NULL;
+       int ret = 0;
+       int fd;
+
+       old_path   = talloc_asprintf(mem_ctx, "%s/%s", remote, path);
+
+       ret = switch_to_real_user();
+       if (ret != 0) {
+               DEBUG(DEBUG_ERR, (__location__ " SETEUID failed\n"));
+               goto finished;
+       }
+
+       fd = open(old_path, O_RDWR);
+       if (fd == -1) {
+               DEBUG(DEBUG_ERR,(__location__ " Failed to OPEN file %s errno:%d\n", old_path, errno));
+               ret = -errno;
+       }
+       *((int *)(&ffi->fh)) = fd;
+
+       DEBUG(DEBUG_DEBUG,("OPEN: %s fd:%d\n",old_path, fd));
+
+finished:
+       switch_back_to_root();
+       talloc_free(mem_ctx);
+       return ret;
+}
+static int remote_cache_release(const char *path, struct fuse_file_info *ffi)
+{
+       int fd;
+
+       fd = *((int *)(&ffi->fh));
+
+       DEBUG(DEBUG_DEBUG,("RELEASE: %s  close fd:%d\n",path, fd));
+
+       if (fd != -1) {
+               close(fd);
+       }
+
+       return 0;
+}
 
 static struct fuse_operations remote_cache_ops = {
        .getattr        = remote_cache_getattr,
@@ -1819,26 +1947,30 @@ static struct fuse_operations remote_cache_ops = {
        .read           = remote_cache_read,
 
 
-
+       .unlink         = remote_cache_unlink,
+       .create         = remote_cache_create,
        .mknod          = remote_cache_mknod,
        .mkdir          = remote_cache_mkdir,
        .rmdir          = remote_cache_rmdir,
-       .unlink         = remote_cache_unlink,
+       .rename         = remote_cache_rename,
        .chmod          = remote_cache_chmod,
        .chown          = remote_cache_chown,
-       .create         = remote_cache_create,
        .utime          = remote_cache_utime,
        .write          = remote_cache_write,
        .truncate       = remote_cache_truncate,
        .ftruncate      = remote_cache_ftruncate,
-       .link           = remote_cache_link,
        .symlink        = remote_cache_symlink,
-       .rename         = remote_cache_rename,
+       .link           = remote_cache_link,
+
+
+
+       .open           = remote_cache_open,
+       .release        = remote_cache_release,
+       .lock           = remote_cache_lock,
+
 
 
-//     .open           = migrate_open,
 //     .flush          = migrate_flush,
-//     .release        = migrate_release,
 //     .fsync          = migrate_fsync,
 //     .setxattr       = migrate_setxattr,
 //     .getxattr       = migrate_getxattr,
@@ -1848,7 +1980,6 @@ static struct fuse_operations remote_cache_ops = {
 //     .releasedir     = migrate_releasedir,
 //     .fsyncdir       = migrate_fsyncdir,
 //     .fgetattr       = migrate_fgetattr,
-//     .lock           = migrate_lock,
 //     .utimens        = migrate_utimens,
 };