summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Baumann <abaumann@yahoo.com>2012-04-13 10:07:46 +0200
committerAndreas Baumann <abaumann@yahoo.com>2012-04-13 10:07:46 +0200
commitf873bf42db96ca494f8c0fff7838c30b84b1849e (patch)
treea1dc3a7a426d354765386684a9d89582d056d0fd
parent24d3992039adc8f346c481084fdcfd1eea19eca7 (diff)
downloadpgfuse-f873bf42db96ca494f8c0fff7838c30b84b1849e.tar.gz
pgfuse-f873bf42db96ca494f8c0fff7838c30b84b1849e.tar.bz2
added unlink for files and corrected rmdir
-rw-r--r--Makefile7
-rw-r--r--pgfuse.c30
-rw-r--r--pgsql.c50
-rw-r--r--pgsql.h2
4 files changed, 88 insertions, 1 deletions
diff --git a/Makefile b/Makefile
index 7b8a076..dbe57ca 100644
--- a/Makefile
+++ b/Makefile
@@ -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
diff --git a/pgfuse.c b/pgfuse.c
index 495fe80..e556f1c 100644
--- a/pgfuse.c
+++ b/pgfuse.c
@@ -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,
diff --git a/pgsql.c b/pgsql.c
index 29fea0b..bcec422 100644
--- a/pgsql.c
+++ b/pgsql.c
@@ -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 *)&param1 };
+ 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 );
diff --git a/pgsql.h b/pgsql.h
index 32803b1..703a933 100644
--- a/pgsql.h
+++ b/pgsql.h
@@ -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 );