vfs_fruit: remove now unused AppleDouble code for resource fork in xattr
[metze/samba/wip.git] / source3 / modules / vfs_fruit.c
index 0ccc3c6f069520a29fa0fdc76a754b87792fca06..ba53abfdd6a3f6955ef78e5bb1ac49575dd30577 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;
@@ -537,8 +536,16 @@ struct fio {
  */
 static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
                               adouble_type_t type);
-static int ad_set(struct adouble *ad, const struct smb_filename *smb_fname);
-static int ad_fset(struct adouble *ad, files_struct *fsp);
+static struct adouble *ad_get(TALLOC_CTX *ctx,
+                             vfs_handle_struct *handle,
+                             const struct smb_filename *smb_fname,
+                             adouble_type_t type);
+static int ad_set(vfs_handle_struct *handle,
+                 struct adouble *ad,
+                 const struct smb_filename *smb_fname);
+static int ad_fset(struct vfs_handle_struct *handle,
+                  struct adouble *ad,
+                  files_struct *fsp);
 static int adouble_path(TALLOC_CTX *ctx,
                        const struct smb_filename *smb_fname__in,
                        struct smb_filename **ppsmb_fname_out);
@@ -1051,7 +1058,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)
 {
@@ -1102,7 +1110,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(),
@@ -1139,7 +1147,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 */
@@ -1218,7 +1226,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;
@@ -1275,7 +1284,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 */
@@ -1338,7 +1347,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;
@@ -1351,7 +1361,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) {
@@ -1402,7 +1412,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;
@@ -1413,7 +1424,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) {
@@ -1425,7 +1436,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));
@@ -1448,50 +1459,66 @@ static bool ad_convert_delete_adfile(struct adouble *ad,
  * @return -1 in case an error occurred, 0 if no conversion was done, 1
  * otherwise
  **/
-static int ad_convert(struct adouble *ad,
+static int ad_convert(struct vfs_handle_struct *handle,
                      const struct smb_filename *smb_fname)
 {
+       struct adouble *ad = NULL;
        bool ok;
        bool converted_xattr = false;
        bool blank;
+       int ret;
 
-       ok = ad_convert_xattr(ad, smb_fname, &converted_xattr);
+       ad = ad_get(talloc_tos(), handle, smb_fname, ADOUBLE_RSRC);
+       if (ad == NULL) {
+               return 0;
+       }
+
+       ok = ad_convert_xattr(handle, ad, smb_fname, &converted_xattr);
        if (!ok) {
-               return -1;
+               ret = -1;
+               goto done;
        }
 
-       ok = ad_convert_blank_rfork(ad, &blank);
+       ok = ad_convert_blank_rfork(handle, ad, &blank);
        if (!ok) {
-               return -1;
+               ret = -1;
+               goto done;
        }
 
        if (converted_xattr || blank) {
                ok = ad_convert_truncate(ad, smb_fname);
                if (!ok) {
-                       return -1;
+                       ret = -1;
+                       goto done;
                }
        }
 
-       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));
-               return -1;
+               ret = -1;
+               goto done;
        }
 
-       ok = ad_convert_delete_adfile(ad, smb_fname);
+       ok = ad_convert_delete_adfile(handle, ad, smb_fname);
        if (!ok) {
-               return -1;
+               ret = -1;
+               goto done;
        }
 
-       return 0;
+       ret = 0;
+done:
+       TALLOC_FREE(ad);
+       return ret;
 }
 
 /**
  * Read and parse Netatalk AppleDouble metadata xattr
  **/
-static ssize_t ad_read_meta(struct adouble *ad,
-                               const struct smb_filename *smb_fname)
+static ssize_t ad_read_meta(vfs_handle_struct *handle,
+                           struct adouble *ad,
+                           const struct smb_filename *smb_fname)
 {
        int      rc = 0;
        ssize_t  ealen;
@@ -1499,7 +1526,7 @@ static ssize_t ad_read_meta(struct adouble *ad,
 
        DEBUG(10, ("reading meta xattr for %s\n", smb_fname->base_name));
 
-       ealen = SMB_VFS_GETXATTR(ad->ad_handle->conn, smb_fname,
+       ealen = SMB_VFS_GETXATTR(handle->conn, smb_fname,
                                 AFPINFO_EA_NETATALK, ad->ad_data,
                                 AD_DATASZ_XATTR);
        if (ealen == -1) {
@@ -1556,7 +1583,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;
                }
@@ -1564,20 +1593,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)
@@ -1602,19 +1617,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);
 }
 
 /*
@@ -1658,25 +1661,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;
@@ -1686,7 +1673,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;
        }
@@ -1737,51 +1724,31 @@ static ssize_t ad_read_rsrc_adouble(struct adouble *ad,
                return -1;
        }
 
-       /*
-        * Try to fixup AppleDouble files created by OS X with xattrs
-        * appended to the ADEID_FINDERI entry.
-        */
-
-       ret = ad_convert(ad, smb_fname);
-       if (ret != 0) {
-               DBG_WARNING("Failed to convert [%s]\n", smb_fname->base_name);
-               return len;
-       }
-
        return len;
 }
 
 /**
  * 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);
 }
 
 /**
  * Read and unpack an AppleDouble metadata xattr or resource
  **/
-static ssize_t ad_read(struct adouble *ad, const struct smb_filename *smb_fname)
+static ssize_t ad_read(vfs_handle_struct *handle,
+                      struct adouble *ad,
+                      const struct smb_filename *smb_fname)
 {
        switch (ad->ad_type) {
        case ADOUBLE_META:
-               return ad_read_meta(ad, smb_fname);
+               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;
        }
@@ -1846,7 +1813,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;
@@ -1876,22 +1842,14 @@ static struct adouble *ad_init(TALLOC_CTX *ctx, vfs_handle_struct *handle,
        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;
@@ -1960,7 +1918,7 @@ static struct adouble *ad_get_internal(TALLOC_CTX *ctx,
 
        }
 
-       len = ad_read(ad, smb_fname);
+       len = ad_read(handle, ad, smb_fname);
        if (len == -1) {
                DEBUG(10, ("error reading AppleDouble for %s\n",
                        smb_fname->base_name));
@@ -2022,7 +1980,9 @@ static struct adouble *ad_fget(TALLOC_CTX *ctx, vfs_handle_struct *handle,
  *
  * @return            status code, 0 means success
  **/
-static int ad_set(struct adouble *ad, const struct smb_filename *smb_fname)
+static int ad_set(vfs_handle_struct *handle,
+                 struct adouble *ad,
+                 const struct smb_filename *smb_fname)
 {
        bool ok;
        int ret;
@@ -2040,7 +2000,7 @@ static int ad_set(struct adouble *ad, const struct smb_filename *smb_fname)
                return -1;
        }
 
-       ret = SMB_VFS_SETXATTR(ad->ad_handle->conn,
+       ret = SMB_VFS_SETXATTR(handle->conn,
                               smb_fname,
                               AFPINFO_EA_NETATALK,
                               ad->ad_data,
@@ -2059,7 +2019,9 @@ static int ad_set(struct adouble *ad, const struct smb_filename *smb_fname)
  *
  * @return            status code, 0 means success
  **/
-static int ad_fset(struct adouble *ad, files_struct *fsp)
+static int ad_fset(struct vfs_handle_struct *handle,
+                  struct adouble *ad,
+                  files_struct *fsp)
 {
        int rc = -1;
        ssize_t len;
@@ -2081,7 +2043,7 @@ static int ad_fset(struct adouble *ad, files_struct *fsp)
 
        switch (ad->ad_type) {
        case ADOUBLE_META:
-               rc = SMB_VFS_NEXT_SETXATTR(ad->ad_handle,
+               rc = SMB_VFS_NEXT_SETXATTR(handle,
                                           fsp->fsp_name,
                                           AFPINFO_EA_NETATALK,
                                           ad->ad_data,
@@ -2089,7 +2051,7 @@ static int ad_fset(struct adouble *ad, files_struct *fsp)
                break;
 
        case ADOUBLE_RSRC:
-               len = SMB_VFS_NEXT_PWRITE(ad->ad_handle,
+               len = SMB_VFS_NEXT_PWRITE(handle,
                                          fsp,
                                          ad->ad_data,
                                          AD_DATASZ_DOT_UND,
@@ -2135,9 +2097,8 @@ static bool is_afpresource_stream(const struct smb_filename *smb_fname)
 }
 
 /**
- * Test whether stream is an Apple stream, not used atm
+ * Test whether stream is an Apple stream.
  **/
-#if 0
 static bool is_apple_stream(const struct smb_filename *smb_fname)
 {
        if (is_afpinfo_stream(smb_fname)) {
@@ -2148,7 +2109,27 @@ static bool is_apple_stream(const struct smb_filename *smb_fname)
        }
        return false;
 }
-#endif
+
+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
@@ -3196,12 +3177,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;
@@ -3605,7 +3586,7 @@ static int fruit_open_rsrc_adouble(vfs_handle_struct *handle,
 
                fsp->fh->fd = hostfd;
 
-               rc = ad_fset(ad, fsp);
+               rc = ad_fset(handle, ad, fsp);
                fsp->fh->fd = -1;
                if (rc != 0) {
                        rc = -1;
@@ -4057,7 +4038,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);
        }
 
@@ -4203,16 +4186,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;
                }
 
@@ -4693,7 +4672,7 @@ static ssize_t fruit_pwrite_meta_netatalk(vfs_handle_struct *handle,
 
        memcpy(p, &ai->afpi_FinderInfo[0], ADEDLEN_FINDERI);
 
-       ret = ad_fset(ad, fsp);
+       ret = ad_fset(handle, ad, fsp);
        if (ret != 0) {
                DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
                TALLOC_FREE(ad);
@@ -4849,7 +4828,7 @@ static ssize_t fruit_pwrite_rsrc_adouble(vfs_handle_struct *handle,
 
        if ((n + offset) > ad_getentrylen(ad, ADEID_RFORK)) {
                ad_setentrylen(ad, ADEID_RFORK, n + offset);
-               ret = ad_fset(ad, fsp);
+               ret = ad_fset(handle, ad, fsp);
                if (ret != 0) {
                        DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp));
                        TALLOC_FREE(ad);
@@ -5887,7 +5866,7 @@ static int fruit_ntimes(vfs_handle_struct *handle,
        ad_setdate(ad, AD_DATE_CREATE | AD_DATE_UNIX,
                   convert_time_t_to_uint32_t(ft->create_time.tv_sec));
 
-       rc = ad_set(ad, smb_fname);
+       rc = ad_set(handle, ad, smb_fname);
 
 exit:
 
@@ -5951,7 +5930,7 @@ static int fruit_ftruncate_rsrc_adouble(struct vfs_handle_struct *handle,
 
        ad_setentrylen(ad, ADEID_RFORK, offset);
 
-       rc = ad_fset(ad, fsp);
+       rc = ad_fset(handle, ad, fsp);
        if (rc != 0) {
                DBG_ERR("ad_fset [%s] failed [%s]\n",
                        fsp_str_dbg(fsp), strerror(errno));
@@ -6070,6 +6049,8 @@ static NTSTATUS fruit_create_file(vfs_handle_struct *handle,
        struct fruit_config_data *config = NULL;
        files_struct *fsp = NULL;
        struct fio *fio = NULL;
+       bool internal_open = (oplock_request & INTERNAL_OPEN_ONLY);
+       int ret;
 
        status = check_aapl(handle, req, in_context_blobs, out_context_blobs);
        if (!NT_STATUS_IS_OK(status)) {
@@ -6079,6 +6060,14 @@ static NTSTATUS fruit_create_file(vfs_handle_struct *handle,
        SMB_VFS_HANDLE_GET_DATA(handle, config, struct fruit_config_data,
                                return NT_STATUS_UNSUCCESSFUL);
 
+       if (is_apple_stream(smb_fname) && !internal_open) {
+               ret = ad_convert(handle, smb_fname);
+               if (ret != 0) {
+                       DBG_ERR("ad_convert() failed\n");
+                       return NT_STATUS_UNSUCCESSFUL;
+               }
+       }
+
        status = SMB_VFS_NEXT_CREATE_FILE(
                handle, req, root_dir_fid, smb_fname,
                access_mask, share_access,
@@ -6161,6 +6150,7 @@ static NTSTATUS fruit_readdir_attr(struct vfs_handle_struct *handle,
        struct fruit_config_data *config = NULL;
        struct readdir_attr_data *attr_data;
        NTSTATUS status;
+       int ret;
 
        SMB_VFS_HANDLE_GET_DATA(handle, config,
                                struct fruit_config_data,
@@ -6172,6 +6162,12 @@ static NTSTATUS fruit_readdir_attr(struct vfs_handle_struct *handle,
 
        DEBUG(10, ("fruit_readdir_attr %s\n", fname->base_name));
 
+       ret = ad_convert(handle, fname);
+       if (ret != 0) {
+               DBG_ERR("ad_convert() failed\n");
+               return NT_STATUS_UNSUCCESSFUL;
+       }
+
        *pattr_data = talloc_zero(mem_ctx, struct readdir_attr_data);
        if (*pattr_data == NULL) {
                return NT_STATUS_UNSUCCESSFUL;