vfs_fruit: remove xattr code from the AppleDouble subsystem
[metze/samba/wip.git] / source3 / modules / vfs_fruit.c
index eabdcc30c4db4de34ef127605336e1df60f4985a..8daf19319d778de43596af1e2971b632f4d28b96 100644 (file)
@@ -413,7 +413,6 @@ struct ad_entry {
 };
 
 struct adouble {
-       vfs_handle_struct        *ad_handle;
        int                       ad_fd;
        bool                      ad_opened;
        adouble_type_t            ad_type;
@@ -452,18 +451,6 @@ struct ad_entry_order entry_order_dot_und[ADEID_NUM_DOT_UND + 1] = {
        {0, 0, 0}
 };
 
-/*
- * Fake AppleDouble entry oder for resource fork xattr.  The xattr
- * isn't an AppleDouble file, it simply contains the resource data,
- * but in order to be able to use some API calls like ad_getentryoff()
- * we build a fake/helper struct adouble with this entry order struct.
- */
-static const
-struct ad_entry_order entry_order_rsrc_xattr[ADEID_NUM_RSRC_XATTR + 1] = {
-       {ADEID_RFORK, 0, 0},
-       {0, 0, 0}
-};
-
 /* Conversion from enumerated id to on-disk AppleDouble id */
 #define AD_EID_DISK(a) (set_eid[a])
 static const uint32_t set_eid[] = {
@@ -535,7 +522,7 @@ struct fio {
 /*
  * Forward declarations
  */
-static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
+static struct adouble *ad_init(TALLOC_CTX *ctx,
                               adouble_type_t type);
 static struct adouble *ad_get(TALLOC_CTX *ctx,
                              vfs_handle_struct *handle,
@@ -1059,7 +1046,8 @@ static bool ad_convert_move_reso(struct adouble *ad,
        return true;
 }
 
-static bool ad_convert_xattr(struct adouble *ad,
+static bool ad_convert_xattr(vfs_handle_struct *handle,
+                            struct adouble *ad,
                             const struct smb_filename *smb_fname,
                             bool *converted_xattr)
 {
@@ -1110,7 +1098,7 @@ static bool ad_convert_xattr(struct adouble *ad,
                files_struct *fsp = NULL;
                ssize_t nwritten;
 
-               status = string_replace_allocate(ad->ad_handle->conn,
+               status = string_replace_allocate(handle->conn,
                                                 e->adx_name,
                                                 string_replace_cmaps,
                                                 talloc_tos(),
@@ -1147,7 +1135,7 @@ static bool ad_convert_xattr(struct adouble *ad,
                DBG_DEBUG("stream_name: %s\n", smb_fname_str_dbg(stream_name));
 
                status = SMB_VFS_CREATE_FILE(
-                       ad->ad_handle->conn,            /* conn */
+                       handle->conn,                   /* conn */
                        NULL,                           /* req */
                        0,                              /* root_dir_fid */
                        stream_name,                    /* fname */
@@ -1226,7 +1214,8 @@ fail:
        return ok;
 }
 
-static bool ad_convert_finderinfo(struct adouble *ad,
+static bool ad_convert_finderinfo(vfs_handle_struct *handle,
+                                 struct adouble *ad,
                                  const struct smb_filename *smb_fname)
 {
        char *p_ad = NULL;
@@ -1283,7 +1272,7 @@ static bool ad_convert_finderinfo(struct adouble *ad,
        DBG_DEBUG("stream_name: %s\n", smb_fname_str_dbg(stream_name));
 
        status = SMB_VFS_CREATE_FILE(
-               ad->ad_handle->conn,            /* conn */
+               handle->conn,                   /* conn */
                NULL,                           /* req */
                0,                              /* root_dir_fid */
                stream_name,                    /* fname */
@@ -1346,7 +1335,8 @@ static bool ad_convert_truncate(struct adouble *ad,
        return true;
 }
 
-static bool ad_convert_blank_rfork(struct adouble *ad,
+static bool ad_convert_blank_rfork(vfs_handle_struct *handle,
+                                  struct adouble *ad,
                                   bool *blank)
 {
        struct fruit_config_data *config = NULL;
@@ -1359,7 +1349,7 @@ static bool ad_convert_blank_rfork(struct adouble *ad,
 
        *blank = false;
 
-       SMB_VFS_HANDLE_GET_DATA(ad->ad_handle, config,
+       SMB_VFS_HANDLE_GET_DATA(handle, config,
                                struct fruit_config_data, return false);
 
        if (!config->wipe_intentionally_left_blank_rfork) {
@@ -1410,7 +1400,8 @@ static bool ad_convert_blank_rfork(struct adouble *ad,
        return true;
 }
 
-static bool ad_convert_delete_adfile(struct adouble *ad,
+static bool ad_convert_delete_adfile(vfs_handle_struct *handle,
+                                    struct adouble *ad,
                                     const struct smb_filename *smb_fname)
 {
        struct fruit_config_data *config = NULL;
@@ -1421,7 +1412,7 @@ static bool ad_convert_delete_adfile(struct adouble *ad,
                return true;
        }
 
-       SMB_VFS_HANDLE_GET_DATA(ad->ad_handle, config,
+       SMB_VFS_HANDLE_GET_DATA(handle, config,
                                struct fruit_config_data, return false);
 
        if (!config->delete_empty_adfiles) {
@@ -1433,7 +1424,7 @@ static bool ad_convert_delete_adfile(struct adouble *ad,
                return false;
        }
 
-       rc = SMB_VFS_NEXT_UNLINK(ad->ad_handle, ad_name);
+       rc = SMB_VFS_NEXT_UNLINK(handle, ad_name);
        if (rc != 0) {
                DBG_ERR("Unlinking [%s] failed: %s\n",
                        smb_fname_str_dbg(ad_name), strerror(errno));
@@ -1470,13 +1461,13 @@ static int ad_convert(struct vfs_handle_struct *handle,
                return 0;
        }
 
-       ok = ad_convert_xattr(ad, smb_fname, &converted_xattr);
+       ok = ad_convert_xattr(handle, ad, smb_fname, &converted_xattr);
        if (!ok) {
                ret = -1;
                goto done;
        }
 
-       ok = ad_convert_blank_rfork(ad, &blank);
+       ok = ad_convert_blank_rfork(handle, ad, &blank);
        if (!ok) {
                ret = -1;
                goto done;
@@ -1490,7 +1481,7 @@ static int ad_convert(struct vfs_handle_struct *handle,
                }
        }
 
-       ok = ad_convert_finderinfo(ad, smb_fname);
+       ok = ad_convert_finderinfo(handle, ad, smb_fname);
        if (!ok) {
                DBG_ERR("Failed to convert [%s]\n",
                        smb_fname_str_dbg(smb_fname));
@@ -1498,7 +1489,7 @@ static int ad_convert(struct vfs_handle_struct *handle,
                goto done;
        }
 
-       ok = ad_convert_delete_adfile(ad, smb_fname);
+       ok = ad_convert_delete_adfile(handle, ad, smb_fname);
        if (!ok) {
                ret = -1;
                goto done;
@@ -1515,7 +1506,7 @@ done:
  **/
 static ssize_t ad_read_meta(vfs_handle_struct *handle,
                            struct adouble *ad,
-                               const struct smb_filename *smb_fname)
+                           const struct smb_filename *smb_fname)
 {
        int      rc = 0;
        ssize_t  ealen;
@@ -1580,7 +1571,9 @@ exit:
                ealen = -1;
                if (errno == EINVAL) {
                        become_root();
-                       removexattr(smb_fname->base_name, AFPINFO_EA_NETATALK);
+                       (void)SMB_VFS_REMOVEXATTR(handle->conn,
+                                                 smb_fname,
+                                                 AFPINFO_EA_NETATALK);
                        unbecome_root();
                        errno = ENOENT;
                }
@@ -1588,20 +1581,6 @@ exit:
        return ealen;
 }
 
-static int ad_open_rsrc_xattr(const struct smb_filename *smb_fname,
-                               int flags,
-                               mode_t mode)
-{
-#ifdef HAVE_ATTROPEN
-       /* FIXME: direct Solaris xattr syscall */
-       return attropen(smb_fname->base_name,
-                       AFPRESOURCE_EA_NETATALK, flags, mode);
-#else
-       errno = ENOSYS;
-       return -1;
-#endif
-}
-
 static int ad_open_rsrc_adouble(const struct smb_filename *smb_fname,
                                int flags,
                                mode_t mode)
@@ -1626,19 +1605,7 @@ static int ad_open_rsrc(vfs_handle_struct *handle,
                        int flags,
                        mode_t mode)
 {
-       struct fruit_config_data *config = NULL;
-       int fd;
-
-       SMB_VFS_HANDLE_GET_DATA(handle, config,
-                               struct fruit_config_data, return -1);
-
-       if (config->rsrc == FRUIT_RSRC_XATTR) {
-               fd = ad_open_rsrc_xattr(smb_fname, flags, mode);
-       } else {
-               fd = ad_open_rsrc_adouble(smb_fname, flags, mode);
-       }
-
-       return fd;
+       return ad_open_rsrc_adouble(smb_fname, flags, mode);
 }
 
 /*
@@ -1682,25 +1649,9 @@ static int ad_open(vfs_handle_struct *handle,
        return 0;
 }
 
-static ssize_t ad_read_rsrc_xattr(struct adouble *ad)
-{
-       int ret;
-       SMB_STRUCT_STAT st;
-
-       /* FIXME: direct sys_fstat(), don't have an fsp */
-       ret = sys_fstat(ad->ad_fd, &st,
-                       lp_fake_directory_create_times(
-                               SNUM(ad->ad_handle->conn)));
-       if (ret != 0) {
-               return -1;
-       }
-
-       ad_setentrylen(ad, ADEID_RFORK, st.st_ex_size);
-       return st.st_ex_size;
-}
-
-static ssize_t ad_read_rsrc_adouble(struct adouble *ad,
-                               const struct smb_filename *smb_fname)
+static ssize_t ad_read_rsrc_adouble(vfs_handle_struct *handle,
+                                   struct adouble *ad,
+                                   const struct smb_filename *smb_fname)
 {
        SMB_STRUCT_STAT sbuf;
        char *p_ad = NULL;
@@ -1710,7 +1661,7 @@ static ssize_t ad_read_rsrc_adouble(struct adouble *ad,
        bool ok;
 
        ret = sys_fstat(ad->ad_fd, &sbuf, lp_fake_directory_create_times(
-                               SNUM(ad->ad_handle->conn)));
+                               SNUM(handle->conn)));
        if (ret != 0) {
                return -1;
        }
@@ -1767,22 +1718,11 @@ static ssize_t ad_read_rsrc_adouble(struct adouble *ad,
 /**
  * Read and parse resource fork, either ._ AppleDouble file or xattr
  **/
-static ssize_t ad_read_rsrc(struct adouble *ad,
-                       const struct smb_filename *smb_fname)
+static ssize_t ad_read_rsrc(vfs_handle_struct *handle,
+                           struct adouble *ad,
+                           const struct smb_filename *smb_fname)
 {
-       struct fruit_config_data *config = NULL;
-       ssize_t len;
-
-       SMB_VFS_HANDLE_GET_DATA(ad->ad_handle, config,
-                               struct fruit_config_data, return -1);
-
-       if (config->rsrc == FRUIT_RSRC_XATTR) {
-               len = ad_read_rsrc_xattr(ad);
-       } else {
-               len = ad_read_rsrc_adouble(ad, smb_fname);
-       }
-
-       return len;
+       return ad_read_rsrc_adouble(handle, ad, smb_fname);
 }
 
 /**
@@ -1796,7 +1736,7 @@ static ssize_t ad_read(vfs_handle_struct *handle,
        case ADOUBLE_META:
                return ad_read_meta(handle, ad, smb_fname);
        case ADOUBLE_RSRC:
-               return ad_read_rsrc(ad, smb_fname);
+               return ad_read_rsrc(handle, ad, smb_fname);
        default:
                return -1;
        }
@@ -1823,25 +1763,19 @@ static int adouble_destructor(struct adouble *ad)
  *
  * @return               adouble handle
  **/
-static struct adouble *ad_alloc(TALLOC_CTX *ctx, vfs_handle_struct *handle,
+static struct adouble *ad_alloc(TALLOC_CTX *ctx,
                                adouble_type_t type)
 {
        int rc = 0;
        size_t adsize = 0;
        struct adouble *ad;
-       struct fruit_config_data *config;
-
-       SMB_VFS_HANDLE_GET_DATA(handle, config,
-                               struct fruit_config_data, return NULL);
 
        switch (type) {
        case ADOUBLE_META:
                adsize = AD_DATASZ_XATTR;
                break;
        case ADOUBLE_RSRC:
-               if (config->rsrc == FRUIT_RSRC_ADFILE) {
-                       adsize = AD_DATASZ_DOT_UND;
-               }
+               adsize = AD_DATASZ_DOT_UND;
                break;
        default:
                return NULL;
@@ -1861,7 +1795,6 @@ static struct adouble *ad_alloc(TALLOC_CTX *ctx, vfs_handle_struct *handle,
                }
        }
 
-       ad->ad_handle = handle;
        ad->ad_type = type;
        ad->ad_magic = AD_MAGIC;
        ad->ad_version = AD_VERSION;
@@ -1880,39 +1813,30 @@ exit:
  * Allocate and initialize a new struct adouble
  *
  * @param[in] ctx        talloc context
- * @param[in] handle     vfs handle
  * @param[in] type       type of AppleDouble, ADOUBLE_META or ADOUBLE_RSRC
  *
  * @return               adouble handle, initialized
  **/
-static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
+static struct adouble *ad_init(TALLOC_CTX *ctx,
                               adouble_type_t type)
 {
        int rc = 0;
        const struct ad_entry_order  *eid;
        struct adouble *ad = NULL;
-       struct fruit_config_data *config;
        time_t t = time(NULL);
 
-       SMB_VFS_HANDLE_GET_DATA(handle, config,
-                               struct fruit_config_data, return NULL);
-
        switch (type) {
        case ADOUBLE_META:
                eid = entry_order_meta_xattr;
                break;
        case ADOUBLE_RSRC:
-               if (config->rsrc == FRUIT_RSRC_ADFILE) {
-                       eid = entry_order_dot_und;
-               } else {
-                       eid = entry_order_rsrc_xattr;
-               }
+               eid = entry_order_dot_und;
                break;
        default:
                return NULL;
        }
 
-       ad = ad_alloc(ctx, handle, type);
+       ad = ad_alloc(ctx, type);
        if (ad == NULL) {
                return NULL;
        }
@@ -1954,7 +1878,7 @@ static struct adouble *ad_get_internal(TALLOC_CTX *ctx,
                   type == ADOUBLE_META ? "meta" : "rsrc",
                   smb_fname->base_name));
 
-       ad = ad_alloc(ctx, handle, type);
+       ad = ad_alloc(ctx, type);
        if (ad == NULL) {
                rc = -1;
                goto exit;
@@ -2167,6 +2091,27 @@ static bool is_apple_stream(const struct smb_filename *smb_fname)
        return false;
 }
 
+static bool is_adouble_file(const char *path)
+{
+       const char *p = NULL;
+       int match;
+
+       p = strrchr(path, '/');
+       if (p == NULL) {
+               p = path;
+       } else {
+               p++;
+       }
+
+       match = strncmp(p,
+                       ADOUBLE_NAME_PREFIX,
+                       strlen(ADOUBLE_NAME_PREFIX));
+       if (match != 0) {
+               return false;
+       }
+       return true;
+}
+
 /**
  * Initialize config struct from our smb.conf config parameters
  **/
@@ -3213,12 +3158,12 @@ static uint64_t readdir_attr_rfork_size(struct vfs_handle_struct *handle,
 
        switch (config->rsrc) {
        case FRUIT_RSRC_ADFILE:
-       case FRUIT_RSRC_XATTR:
                rfork_size = readdir_attr_rfork_size_adouble(handle,
                                                             smb_fname);
                break;
 
-       case FRUIT_META_STREAM:
+       case FRUIT_RSRC_XATTR:
+       case FRUIT_RSRC_STREAM:
                rfork_size = readdir_attr_rfork_size_stream(handle,
                                                            smb_fname);
                break;
@@ -3614,7 +3559,7 @@ static int fruit_open_rsrc_adouble(vfs_handle_struct *handle,
        }
 
        if (flags & (O_CREAT | O_TRUNC)) {
-               ad = ad_init(fsp, handle, ADOUBLE_RSRC);
+               ad = ad_init(fsp, ADOUBLE_RSRC);
                if (ad == NULL) {
                        rc = -1;
                        goto exit;
@@ -4074,7 +4019,9 @@ static int fruit_unlink(vfs_handle_struct *handle,
                return fruit_unlink_meta(handle, smb_fname);
        } else if (is_afpresource_stream(smb_fname)) {
                return fruit_unlink_rsrc(handle, smb_fname, false);
-       } if (is_ntfs_stream_smb_fname(smb_fname)) {
+       } else if (is_ntfs_stream_smb_fname(smb_fname)) {
+               return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
+       } else if (is_adouble_file(smb_fname->base_name)) {
                return SMB_VFS_NEXT_UNLINK(handle, smb_fname);
        }
 
@@ -4220,16 +4167,12 @@ static int fruit_rmdir(struct vfs_handle_struct *handle,
        }
 
        while ((de = SMB_VFS_READDIR(handle->conn, dh, NULL)) != NULL) {
-               int match;
                struct adouble *ad = NULL;
                char *p = NULL;
                struct smb_filename *ad_smb_fname = NULL;
                int ret;
 
-               match = strncmp(de->d_name,
-                               ADOUBLE_NAME_PREFIX,
-                               strlen(ADOUBLE_NAME_PREFIX));
-               if (match != 0) {
+               if (!is_adouble_file(de->d_name)) {
                        continue;
                }
 
@@ -4696,7 +4639,7 @@ static ssize_t fruit_pwrite_meta_netatalk(vfs_handle_struct *handle,
 
        ad = ad_fget(talloc_tos(), handle, fsp, ADOUBLE_META);
        if (ad == NULL) {
-               ad = ad_init(talloc_tos(), handle, ADOUBLE_META);
+               ad = ad_init(talloc_tos(), ADOUBLE_META);
                if (ad == NULL) {
                        return -1;
                }