s3-includes: only include system/filesys.h when needed.
[vlendec/samba-autobuild/.git] / source3 / libsmb / smb_share_modes.c
index 177e0114b3de25d5011bd61e7f1accf31edce1f0..1a6c2123ed6698f5dc686606a157232a475df035 100644 (file)
@@ -25,6 +25,7 @@
 */
 
 #include "includes.h"
+#include "system/filesys.h"
 #include "smb_share_modes.h"
 
 /* Database context handle. */
@@ -67,7 +68,7 @@ struct smbdb_ctx *smb_share_mode_db_open(const char *db_path)
        memset(smb_db, '\0', sizeof(struct smbdb_ctx));
 
        smb_db->smb_tdb = tdb_open(db_path,
-                               0, TDB_DEFAULT|TDB_CLEAR_IF_FIRST,
+                               0, TDB_DEFAULT|TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH,
                                O_RDWR|O_CREAT,
                                0644);
 
@@ -155,7 +156,8 @@ static int share_mode_entry_equal(const struct smb_share_mode_entry *e_entry,
  */
 
 static void create_share_mode_entry(struct share_mode_entry *out,
-                               const struct smb_share_mode_entry *in)
+                               const struct smb_share_mode_entry *in,
+                               uint32_t name_hash)
 {
        memset(out, '\0', sizeof(struct share_mode_entry));
 
@@ -170,6 +172,7 @@ static void create_share_mode_entry(struct share_mode_entry *out,
        out->id.extid = in->extid;
        out->uid = (uint32)geteuid();
        out->flags = 0;
+       out->name_hash = name_hash;
 }
 
 /*
@@ -255,12 +258,37 @@ int smb_get_share_mode_entries(struct smbdb_ctx *db_ctx,
                return 0;
        }
 
-       *p_delete_on_close = ld->u.s.delete_on_close;
+       *p_delete_on_close = ld->u.s.num_delete_token_entries != 0;
        *pp_list = list;
        free(db_data.dptr);
        return list_num;
 }
 
+static uint32_t smb_name_hash(const char *sharepath, const char *filename, int *err)
+{
+       TDB_DATA key;
+       char *fullpath = NULL;
+       size_t sharepath_size = strlen(sharepath);
+       size_t filename_size = strlen(filename);
+       uint32_t name_hash;
+
+       *err = 0;
+       fullpath = (char *)malloc(sharepath_size + filename_size + 2);
+       if (fullpath == NULL) {
+               *err = 1;
+               return 0;
+       }
+       memcpy(fullpath, sharepath, sharepath_size);
+       fullpath[sharepath_size] = '/';
+       memcpy(&fullpath[sharepath_size + 1], filename, filename_size + 1);
+
+       key.dptr = (uint8_t *)fullpath;
+       key.dsize = strlen(fullpath) + 1;
+       name_hash = tdb_jenkins_hash(&key);
+       free(fullpath);
+       return name_hash;
+}
+
 /* 
  * Create an entry in the Samba share mode db.
  */
@@ -281,6 +309,12 @@ int smb_create_share_mode_entry_ex(struct smbdb_ctx *db_ctx,
        struct share_mode_entry *shares = NULL;
        uint8 *new_data_p = NULL;
        size_t new_data_size = 0;
+       int err = 0;
+       uint32_t name_hash = smb_name_hash(sharepath, filename, &err);
+
+       if (err) {
+               return -1;
+       }
 
        db_data = tdb_fetch(db_ctx->smb_tdb, locking_key);
        if (!db_data.dptr) {
@@ -296,10 +330,9 @@ int smb_create_share_mode_entry_ex(struct smbdb_ctx *db_ctx,
                ld = (struct locking_data *)db_data.dptr;
                memset(ld, '\0', sizeof(struct locking_data));
                ld->u.s.num_share_mode_entries = 1;
-               ld->u.s.delete_on_close = 0;
-               ld->u.s.delete_token_size = 0;
+               ld->u.s.num_delete_token_entries = 0;
                shares = (struct share_mode_entry *)(db_data.dptr + sizeof(struct locking_data));
-               create_share_mode_entry(shares, new_entry);
+               create_share_mode_entry(shares, new_entry, name_hash);
 
                memcpy(db_data.dptr + sizeof(struct locking_data) + sizeof(struct share_mode_entry),
                        sharepath,
@@ -338,12 +371,12 @@ int smb_create_share_mode_entry_ex(struct smbdb_ctx *db_ctx,
        shares = (struct share_mode_entry *)(new_data_p + sizeof(struct locking_data) +
                        (orig_num_share_modes * sizeof(struct share_mode_entry)));
 
-       create_share_mode_entry(shares, new_entry);
+       create_share_mode_entry(shares, new_entry, name_hash);
 
        ld = (struct locking_data *)new_data_p;
        ld->u.s.num_share_mode_entries++;
 
-       /* Append the original delete_token and filenames. */
+       /* Append the original delete_tokens and filenames. */
        memcpy(new_data_p + sizeof(struct locking_data) + (ld->u.s.num_share_mode_entries * sizeof(struct share_mode_entry)),
                db_data.dptr + sizeof(struct locking_data) + (orig_num_share_modes * sizeof(struct share_mode_entry)),
                db_data.dsize - sizeof(struct locking_data) - (orig_num_share_modes * sizeof(struct share_mode_entry)));
@@ -460,7 +493,7 @@ int smb_delete_share_mode_entry(struct smbdb_ctx *db_ctx,
                return tdb_delete(db_ctx->smb_tdb, locking_key);
        }
 
-       /* Copy any delete token plus the terminating filenames. */
+       /* Copy any delete tokens plus the terminating filenames. */
        remaining_ptr = db_data.dptr + sizeof(struct locking_data) + (orig_num_share_modes * sizeof(struct share_mode_entry));
        remaining_size = db_data.dsize - (remaining_ptr - db_data.dptr);
 
@@ -521,7 +554,7 @@ int smb_change_share_mode_entry(struct smbdb_ctx *db_ctx,
                }
 
                if (share_mode_entry_equal(set_entry, share)) {
-                       create_share_mode_entry(share, new_entry);
+                       create_share_mode_entry(share, new_entry, share->name_hash);
                        found_entry = 1;
                        break;
                }