diff options
author | Andreas Baumann <abaumann@yahoo.com> | 2012-04-13 10:07:46 +0200 |
---|---|---|
committer | Andreas Baumann <abaumann@yahoo.com> | 2012-04-13 10:07:46 +0200 |
commit | f873bf42db96ca494f8c0fff7838c30b84b1849e (patch) | |
tree | a1dc3a7a426d354765386684a9d89582d056d0fd | |
parent | 24d3992039adc8f346c481084fdcfd1eea19eca7 (diff) | |
download | pgfuse-f873bf42db96ca494f8c0fff7838c30b84b1849e.tar.gz pgfuse-f873bf42db96ca494f8c0fff7838c30b84b1849e.tar.bz2 |
added unlink for files and corrected rmdir
-rw-r--r-- | Makefile | 7 | ||||
-rw-r--r-- | pgfuse.c | 30 | ||||
-rw-r--r-- | pgsql.c | 50 | ||||
-rw-r--r-- | pgsql.h | 2 |
4 files changed, 88 insertions, 1 deletions
@@ -21,6 +21,7 @@ test: pgfuse psql < schema.sql -./pgfuse -s -v "" mnt mount | grep pgfuse + # expect success for all -mkdir mnt/dir -mkdir mnt/dir/dir2 -mkdir mnt/dir/dir3 @@ -30,6 +31,12 @@ test: pgfuse -ls -al mnt -ls -al mnt/dir/dir2 -rmdir mnt/dir/dir3 + # expect fail (directory not empty) + -rmdir mnt/dir + # expect fail (not a directory) + -rmdir mnt/dir/dir2/bfile + # expect success + -rm mnt/dir/dir2/bfile fusermount -u mnt pgfuse: pgfuse.o pgsql.o @@ -448,6 +448,34 @@ static int pgfuse_rmdir( const char *path ) return res; } +static int pgfuse_unlink( const char *path ) +{ + PgFuseData *data = (PgFuseData *)fuse_get_context( )->private_data; + int id; + int res; + PgMeta meta; + + if( data->verbose ) { + syslog( LOG_INFO, "Remove file '%s' on '%s'", path, data->mountpoint ); + } + + id = psql_get_meta( data->conn, path, &meta ); + if( id < 0 ) { + return id; + } + if( meta.isdir ) { + return -EPERM; + } + + if( data->verbose ) { + syslog( LOG_DEBUG, "Id of file '%s' to be removed is %d", path, id ); + } + + res = psql_delete_file( data->conn, id, path ); + + return res; +} + static int pgfuse_flush( const char *path, struct fuse_file_info *fi ) { /* nothing to do currently as the temporary file buffer holds @@ -592,7 +620,7 @@ static struct fuse_operations pgfuse_oper = { .readlink = NULL, .mknod = NULL, .mkdir = pgfuse_mkdir, - .unlink = NULL, + .unlink = pgfuse_unlink, .rmdir = pgfuse_rmdir, .symlink = NULL, .rename = NULL, @@ -18,6 +18,7 @@ #include "pgsql.h" #include <string.h> /* for strlen, memcpy, strcmp */ +#include <stdlib.h> /* for atoi */ #include <syslog.h> /* for ERR_XXX */ #include <errno.h> /* for ENOENT and friends */ @@ -192,6 +193,55 @@ int psql_delete_dir( PGconn *conn, const int id, const char *path ) int lengths[1] = { sizeof( param1 ) }; int binary[1] = { 1 }; PGresult *res; + char *iptr; + int count; + + res = PQexecParams( conn, "SELECT COUNT(*) FROM dir where parent_id=$1::int4", + 1, NULL, values, lengths, binary, 0 ); + + if( PQresultStatus( res ) != PGRES_TUPLES_OK ) { + syslog( LOG_ERR, "Error in psql_delete_dir for path '%s': %s", path, PQerrorMessage( conn ) ); + PQclear( res ); + return -EIO; + } + + if( PQntuples( res ) != 1 ) { + syslog( LOG_ERR, "Expecting COUNT(*) to return 1 tupe, weird!" ); + PQclear( res ); + return -EIO; + } + + iptr = PQgetvalue( res, 0, 0 ); + count = atoi( iptr ); + + if( count > 0 ) { + PQclear( res ); + return -ENOTEMPTY; + } + + PQclear( res ); + + res = PQexecParams( conn, "DELETE FROM dir where id=$1::int4", + 1, NULL, values, lengths, binary, 1 ); + + if( PQresultStatus( res ) != PGRES_COMMAND_OK ) { + syslog( LOG_ERR, "Error in psql_delete_dir for path '%s': %s", path, PQerrorMessage( conn ) ); + PQclear( res ); + return -EIO; + } + + PQclear( res ); + + return 0; +} + +int psql_delete_file( PGconn *conn, const int id, const char *path ) +{ + int param1 = htonl( id ); + const char *values[1] = { (char *)¶m1 }; + int lengths[1] = { sizeof( param1 ) }; + int binary[1] = { 1 }; + PGresult *res; res = PQexecParams( conn, "DELETE FROM dir where id=$1::int4", 1, NULL, values, lengths, binary, 1 ); @@ -42,6 +42,8 @@ int psql_create_dir( PGconn *conn, const int parent_id, const char *path, const int psql_delete_dir( PGconn *conn, const int id, const char *path ); +int psql_delete_file( PGconn *conn, const int id, const char *path ); + int psql_write_buf( PGconn *conn, const int id, const char *path, const char *buf, const size_t len ); int psql_write_meta( PGconn *conn, const int id, const char *path, PgMeta meta ); |