selftest/Samba4: make use of get_cmd_env_vars() to setup all relevant env variables
[samba.git] / source3 / modules / vfs_prealloc.c
index cf3019c588a9a3dfe1d6b2e03dbe2a9936381b26..51e9ad0d8fb41cdc2df9858f0e0d6892a13c7fd3 100644 (file)
@@ -106,105 +106,18 @@ static int prealloc_connect(
        return 0;
 }
 
-static int prealloc_open(vfs_handle_struct* handle,
-                       struct smb_filename *smb_fname,
-                       files_struct *      fsp,
-                       int                 flags,
-                       mode_t              mode)
-{
-       int fd;
-       off_t size = 0;
-
-       const char * dot;
-       char fext[10];
-
-       if (!(flags & (O_CREAT|O_TRUNC))) {
-               /* Caller is not intending to rewrite the file. Let's not mess
-                * with the allocation in this case.
-                */
-               goto normal_open;
-       }
-
-       *fext = '\0';
-       dot = strrchr(smb_fname->base_name, '.');
-       if (dot && *++dot) {
-               if (strlen(dot) < sizeof(fext)) {
-                       strncpy(fext, dot, sizeof(fext));
-                       if (!strnorm(fext, CASE_LOWER)) {
-                               goto normal_open;
-                       }
-               }
-       }
-
-       if (*fext == '\0') {
-               goto normal_open;
-       }
-
-       /* Syntax for specifying preallocation size is:
-        *      MODULE: <extension> = <size>
-        * where
-        *      <extension> is the file extension in lower case
-        *      <size> is a size like 10, 10K, 10M
-        */
-       size = conv_str_size(lp_parm_const_string(SNUM(handle->conn), MODULE,
-                                                   fext, NULL));
-       if (size <= 0) {
-               /* No need to preallocate this file. */
-               goto normal_open;
-       }
-
-       fd = SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
-       if (fd < 0) {
-               return fd;
-       }
-
-       /* Prellocate only if the file is being created or replaced. Note that
-        * Samba won't ever pass down O_TRUNC, which is why we have to handle
-        * truncate calls specially.
-        */
-       if ((flags & O_CREAT) || (flags & O_TRUNC)) {
-               off_t * psize;
-
-               psize = VFS_ADD_FSP_EXTENSION(handle, fsp, off_t, NULL);
-               if (psize == NULL || *psize == -1) {
-                       return fd;
-               }
-
-               DEBUG(module_debug,
-                       ("%s: preallocating %s (fd=%d) to %lld bytes\n",
-                           MODULE, smb_fname_str_dbg(smb_fname), fd,
-                           (long long)size));
-
-               *psize = size;
-               if (preallocate_space(fd, *psize) < 0) {
-                       VFS_REMOVE_FSP_EXTENSION(handle, fsp);
-               }
-       }
-
-       return fd;
-
-normal_open:
-       /* We are not creating or replacing a file. Skip the
-        * preallocation.
-        */
-       DEBUG(module_debug, ("%s: skipping preallocation for %s\n",
-               MODULE, smb_fname_str_dbg(smb_fname)));
-       return SMB_VFS_NEXT_OPEN(handle, smb_fname, fsp, flags, mode);
-}
-
 static int prealloc_openat(struct vfs_handle_struct* handle,
                           const struct files_struct *dirfsp,
                           const struct smb_filename *smb_fname,
                           files_struct *fsp,
-                          int flags,
-                          mode_t mode)
+                          const struct vfs_open_how *how)
 {
        int fd;
        off_t size = 0;
        const char * dot;
        char fext[10];
 
-       if (!(flags & (O_CREAT|O_TRUNC))) {
+       if (!(how->flags & (O_CREAT|O_TRUNC))) {
                /* Caller is not intending to rewrite the file. Let's not mess
                 * with the allocation in this case.
                 */
@@ -215,8 +128,10 @@ static int prealloc_openat(struct vfs_handle_struct* handle,
        dot = strrchr(smb_fname->base_name, '.');
        if (dot && *++dot) {
                if (strlen(dot) < sizeof(fext)) {
+                       bool ok;
                        strncpy(fext, dot, sizeof(fext));
-                       if (!strnorm(fext, CASE_LOWER)) {
+                       ok = strlower_m(fext);
+                       if (!ok);
                                goto normal_open;
                        }
                }
@@ -239,16 +154,16 @@ static int prealloc_openat(struct vfs_handle_struct* handle,
                goto normal_open;
        }
 
-       fd = SMB_VFS_NEXT_OPENAT(handle, dirfsp, smb_fname, fsp, flags, mode);
+       fd = SMB_VFS_NEXT_OPENAT(handle, dirfsp, smb_fname, fsp, how);
        if (fd < 0) {
                return fd;
        }
 
-       /* Prellocate only if the file is being created or replaced. Note that
+       /* Preallocate only if the file is being created or replaced. Note that
         * Samba won't ever pass down O_TRUNC, which is why we have to handle
         * truncate calls specially.
         */
-       if ((flags & O_CREAT) || (flags & O_TRUNC)) {
+       if ((how->flags & O_CREAT) || (how->flags & O_TRUNC)) {
                off_t * psize;
 
                psize = VFS_ADD_FSP_EXTENSION(handle, fsp, off_t, NULL);
@@ -275,7 +190,7 @@ normal_open:
         */
        DEBUG(module_debug, ("%s: skipping preallocation for %s\n",
                MODULE, smb_fname_str_dbg(smb_fname)));
-       return SMB_VFS_NEXT_OPENAT(handle, dirfsp, smb_fname, fsp, flags, mode);
+       return SMB_VFS_NEXT_OPENAT(handle, dirfsp, smb_fname, fsp, how);
 }
 
 static int prealloc_ftruncate(vfs_handle_struct * handle,
@@ -287,14 +202,13 @@ static int prealloc_ftruncate(vfs_handle_struct * handle,
 
        /* Maintain the allocated space even in the face of truncates. */
        if ((psize = VFS_FETCH_FSP_EXTENSION(handle, fsp))) {
-               preallocate_space(fsp->fh->fd, *psize);
+               preallocate_space(fsp_get_io_fd(fsp), *psize);
        }
 
        return ret;
 }
 
 static struct vfs_fn_pointers prealloc_fns = {
-       .open_fn = prealloc_open,
        .openat_fn = prealloc_openat,
        .ftruncate_fn = prealloc_ftruncate,
        .connect_fn = prealloc_connect,