remove unused seek_file(); don't hardcode '\' when printing the auth-user
[tprouty/samba.git] / source / smbd / fileio.c
index d5df9826dfd09cb6f8a62c3d0073e77777b74488..3462a3b9fa57ff03324b838019e0104a50aea479 100644 (file)
 
 static BOOL setup_write_cache(files_struct *, SMB_OFF_T);
 
-/****************************************************************************
- Seek a file. Try to avoid the seek if possible.
-****************************************************************************/
-
-static SMB_OFF_T seek_file(files_struct *fsp,SMB_OFF_T pos)
-{
-       SMB_OFF_T offset = 0;
-       SMB_OFF_T seek_ret;
-
-       if (fsp->print_file && lp_postscript(fsp->conn->service))
-               offset = 3;
-
-       seek_ret = fsp->conn->vfs_ops.lseek(fsp,fsp->fd,pos+offset,SEEK_SET);
-
-       if(seek_ret == -1) {
-               DEBUG(0,("seek_file: (%s) sys_lseek failed. Error was %s\n",
-                       fsp->fsp_name, strerror(errno) ));
-               fsp->pos = -1;
-               return -1;
-       }
-
-       fsp->pos = seek_ret - offset;
-
-       DEBUG(10,("seek_file (%s): requested pos = %.0f, new pos = %.0f\n",
-               fsp->fsp_name, (double)(pos+offset), (double)fsp->pos ));
-
-       return(fsp->pos);
-}
-
 /****************************************************************************
  Read from write cache if we can.
 ****************************************************************************/
@@ -91,21 +62,22 @@ ssize_t read_file(files_struct *fsp,char *data,SMB_OFF_T pos,size_t n)
         * Serve from write cache if we can.
         */
 
-       if(read_from_write_cache(fsp, data, pos, n))
+       if(read_from_write_cache(fsp, data, pos, n)) {
+               fsp->pos = pos + n;
+               fsp->position_information = fsp->pos;
                return n;
+       }
 
        flush_write_cache(fsp, READ_FLUSH);
 
-       if (seek_file(fsp,pos) == -1) {
-               DEBUG(3,("read_file: Failed to seek to %.0f\n",(double)pos));
-               return(ret);
-       }
-  
+       fsp->pos = pos;
+
        if (n > 0) {
 #ifdef DMF_FIX
                int numretries = 3;
 tryagain:
-               readret = fsp->conn->vfs_ops.read(fsp,fsp->fd,data,n);
+               readret = SMB_VFS_PREAD(fsp,fsp->fd,data,n,pos);
+
                if (readret == -1) {
                        if ((errno == EAGAIN) && numretries) {
                                DEBUG(3,("read_file EAGAIN retry in 10 seconds\n"));
@@ -116,7 +88,8 @@ tryagain:
                        return -1;
                }
 #else /* NO DMF fix. */
-               readret = fsp->conn->vfs_ops.read(fsp,fsp->fd,data,n);
+               readret = SMB_VFS_PREAD(fsp,fsp->fd,data,n,pos);
+
                if (readret == -1)
                        return -1;
 #endif
@@ -127,6 +100,9 @@ tryagain:
        DEBUG(10,("read_file (%s): pos = %.0f, size = %lu, returned %lu\n",
                fsp->fsp_name, (double)pos, (unsigned long)n, (long)ret ));
 
+       fsp->pos += ret;
+       fsp->position_information = fsp->pos;
+
        return(ret);
 }
 
@@ -141,14 +117,26 @@ static ssize_t real_write_file(files_struct *fsp,char *data,SMB_OFF_T pos, size_
 {
        ssize_t ret;
 
-       if ((pos != -1) && (seek_file(fsp,pos) == -1))
-               return -1;
-
-       ret = vfs_write_data(fsp,data,n);
+        if (pos == -1)
+                ret = vfs_write_data(fsp, data, n);
+        else {
+               fsp->pos = pos;
+                ret = vfs_pwrite_data(fsp, data, n, pos);
+       }
 
        DEBUG(10,("real_write_file (%s): pos = %.0f, size = %lu, returned %ld\n",
                fsp->fsp_name, (double)pos, (unsigned long)n, (long)ret ));
 
+       if (ret != -1) {
+               fsp->pos += ret;
+
+/* Yes - this is correct - writes don't update this. JRA. */
+/* Found by Samba4 tests. */
+#if 0
+               fsp->position_information = fsp->pos;
+#endif
+       }
+
        return ret;
 }
 
@@ -162,8 +150,19 @@ ssize_t write_file(files_struct *fsp, char *data, SMB_OFF_T pos, size_t n)
        ssize_t total_written = 0;
        int write_path = -1; 
 
-       if (fsp->print_file)
-               return print_job_write(SNUM(fsp->conn), fsp->print_jobid, data, n);
+       if (fsp->print_file) {
+               int snum;
+               uint32 jobid;
+
+               if (!rap_to_pjobid(fsp->rap_print_jobid, &snum, &jobid)) {
+                       DEBUG(3,("write_file: Unable to map RAP jobid %u to jobid.\n",
+                                               (unsigned int)fsp->rap_print_jobid ));
+                       errno = EBADF;
+                       return -1;
+               }
+
+               return print_job_write(SNUM(fsp->conn), jobid, data, n);
+       }
 
        if (!fsp->can_write) {
                errno = EPERM;
@@ -174,9 +173,9 @@ ssize_t write_file(files_struct *fsp, char *data, SMB_OFF_T pos, size_t n)
                SMB_STRUCT_STAT st;
                fsp->modified = True;
 
-               if (fsp->conn->vfs_ops.fstat(fsp,fsp->fd,&st) == 0) {
+               if (SMB_VFS_FSTAT(fsp,fsp->fd,&st) == 0) {
                        int dosmode = dos_mode(fsp->conn,fsp->fsp_name,&st);
-                       fsp->size = st.st_size;
+                       fsp->size = (SMB_BIG_UINT)st.st_size;
                        if (MAP_ARCHIVE(fsp->conn) && !IS_DOS_ARCHIVE(dosmode))
                                file_chmod(fsp->conn,fsp->fsp_name,dosmode | aARCH,&st);
 
@@ -237,14 +236,16 @@ nonop=%u allocated=%u active=%u direct=%u perfect=%u readhits=%u\n",
        if(!wcp) {
                DO_PROFILE_INC(writecache_direct_writes);
                total_written = real_write_file(fsp, data, pos, n);
-               if ((total_written != -1) && (pos + total_written > fsp->size))
-                       fsp->size = pos + total_written;
+               if ((total_written != -1) && (pos + total_written > (SMB_OFF_T)fsp->size)) 
+                       fsp->size = (SMB_BIG_UINT)(pos + total_written);
                return total_written;
        }
 
        DEBUG(9,("write_file (%s)(fd=%d pos=%.0f size=%u) wcp->offset=%.0f wcp->data_size=%u\n",
                fsp->fsp_name, fsp->fd, (double)pos, (unsigned int)n, (double)wcp->offset, (unsigned int)wcp->data_size));
 
+       fsp->pos = pos + n;
+
        /* 
         * If we have active cache and it isn't contiguous then we flush.
         * NOTE: There is a small problem with running out of disk ....
@@ -287,8 +288,10 @@ nonop=%u allocated=%u active=%u direct=%u perfect=%u readhits=%u\n",
                         * Update the file size if changed.
                         */
 
-                       if (wcp->offset + wcp->data_size > wcp->file_size)
-                               fsp->size = wcp->file_size = wcp->offset + wcp->data_size;
+                       if (wcp->offset + wcp->data_size > wcp->file_size) {
+                               wcp->file_size = wcp->offset + wcp->data_size;
+                               fsp->size = (SMB_BIG_UINT)wcp->file_size;
+                       }
 
                        /*
                         * If we used all the data then
@@ -348,8 +351,10 @@ nonop=%u allocated=%u active=%u direct=%u perfect=%u readhits=%u\n",
                         * Update the file size if changed.
                         */
 
-                       if (wcp->offset + wcp->data_size > wcp->file_size)
-                               fsp->size = wcp->file_size = wcp->offset + wcp->data_size;
+                       if (wcp->offset + wcp->data_size > wcp->file_size) {
+                               wcp->file_size = wcp->offset + wcp->data_size;
+                               fsp->size = (SMB_BIG_UINT)wcp->file_size;
+                       }
 
                        /*
                         * We don't need to move the start of data, but we
@@ -422,8 +427,10 @@ nonop=%u allocated=%u active=%u direct=%u perfect=%u readhits=%u\n",
                         * Update the file size if changed.
                         */
 
-                       if (wcp->offset + wcp->data_size > wcp->file_size)
-                               fsp->size = wcp->file_size = wcp->offset + wcp->data_size;
+                       if (wcp->offset + wcp->data_size > wcp->file_size) {
+                               wcp->file_size = wcp->offset + wcp->data_size;
+                               fsp->size = (SMB_BIG_UINT)wcp->file_size;
+                       }
 
                        /*
                         * If we used all the data then
@@ -497,8 +504,10 @@ len = %u\n",fsp->fd, (double)pos, (unsigned int)n, (double)wcp->offset, (unsigne
                         * Update the file size if needed.
                         */
 
-                       if(pos + n > wcp->file_size)
-                               fsp->size = wcp->file_size = pos + n;
+                       if(pos + n > wcp->file_size) {
+                               wcp->file_size = pos + n;
+                               fsp->size = (SMB_BIG_UINT)wcp->file_size;
+                       }
 
                        /*
                         * If write would fit in the cache, and is larger than
@@ -529,8 +538,10 @@ cache: fd = %d, off=%.0f, size=%u\n", fsp->fd, (double)wcp->offset, (unsigned in
                                if (ret == -1)
                                        return ret;
 
-                               if (pos + ret > wcp->file_size)
-                                       fsp->size = wcp->file_size = pos + ret;
+                               if (pos + ret > wcp->file_size) {
+                                       wcp->file_size = pos + ret;
+                                       fsp->size = (SMB_BIG_UINT)wcp->file_size;
+                               }
 
                                return ret;
                        }
@@ -539,8 +550,10 @@ cache: fd = %d, off=%.0f, size=%u\n", fsp->fd, (double)wcp->offset, (unsigned in
 
                }
 
-               if(wcp->data_size > wcp->file_size)
-                       fsp->size = wcp->file_size = wcp->data_size;
+               if(wcp->data_size > wcp->file_size) {
+                       wcp->file_size = wcp->data_size;
+                       fsp->size = (SMB_BIG_UINT)wcp->file_size;
+               }
 
                if (cache_flush_needed) {
                        DEBUG(3,("WRITE_FLUSH:%d: due to noncontinuous write: fd = %d, size = %.0f, pos = %.0f, \
@@ -562,8 +575,10 @@ n = %u, wcp->offset=%.0f, wcp->data_size=%u\n",
                if (ret == -1)
                        return -1;
 
-               if (pos + ret > wcp->file_size)
-                       fsp->size = wcp->file_size = pos + n;
+               if (pos + ret > wcp->file_size) {
+                       wcp->file_size = pos + n;
+                       fsp->size = (SMB_BIG_UINT)wcp->file_size;
+               }
 
                DO_PROFILE_INC(writecache_direct_writes);
                return total_written + n;
@@ -592,8 +607,10 @@ n = %u, wcp->offset=%.0f, wcp->data_size=%u\n",
                 * Update the file size if changed.
                 */
 
-               if (wcp->offset + wcp->data_size > wcp->file_size)
-                       fsp->size = wcp->file_size = wcp->offset + wcp->data_size;
+               if (wcp->offset + wcp->data_size > wcp->file_size) {
+                       wcp->file_size = wcp->offset + wcp->data_size;
+                       fsp->size = (SMB_BIG_UINT)wcp->file_size;
+               }
                DEBUG(9,("wcp->offset = %.0f wcp->data_size = %u cache return %u\n",
                        (double)wcp->offset, (unsigned int)wcp->data_size, (unsigned int)n));
 
@@ -666,8 +683,8 @@ static BOOL setup_write_cache(files_struct *fsp, SMB_OFF_T file_size)
        DO_PROFILE_INC(writecache_allocated_write_caches);
        allocated_write_caches++;
 
-       DEBUG(10,("setup_write_cache: File %s allocated write cache size %u\n",
-               fsp->fsp_name, wcp->alloc_size ));
+       DEBUG(10,("setup_write_cache: File %s allocated write cache size %lu\n",
+               fsp->fsp_name, (unsigned long)wcp->alloc_size ));
 
        return True;
 }
@@ -678,13 +695,13 @@ static BOOL setup_write_cache(files_struct *fsp, SMB_OFF_T file_size)
 
 void set_filelen_write_cache(files_struct *fsp, SMB_OFF_T file_size)
 {
-       fsp->size = file_size;
+       fsp->size = (SMB_BIG_UINT)file_size;
        if(fsp->wcp) {
                /* The cache *must* have been flushed before we do this. */
                if (fsp->wcp->data_size != 0) {
                        pstring msg;
                        slprintf(msg, sizeof(msg)-1, "set_filelen_write_cache: size change \
-on file %s with write cache size = %u\n", fsp->fsp_name, fsp->wcp->data_size );
+on file %s with write cache size = %lu\n", fsp->fsp_name, (unsigned long)fsp->wcp->data_size );
                        smb_panic(msg);
                }
                fsp->wcp->file_size = file_size;
@@ -737,7 +754,7 @@ void sync_file(connection_struct *conn, files_struct *fsp)
 {
        if(lp_strict_sync(SNUM(conn)) && fsp->fd != -1) {
                flush_write_cache(fsp, SYNC_FLUSH);
-               conn->vfs_ops.fsync(fsp,fsp->fd);
+               SMB_VFS_FSYNC(fsp,fsp->fd);
        }
 }
 
@@ -749,7 +766,7 @@ void sync_file(connection_struct *conn, files_struct *fsp)
 int fsp_stat(files_struct *fsp, SMB_STRUCT_STAT *pst)
 {
        if (fsp->fd == -1)
-               return vfs_stat(fsp->conn, fsp->fsp_name, pst);
+               return SMB_VFS_STAT(fsp->conn, fsp->fsp_name, pst);
        else
-               return vfs_fstat(fsp,fsp->fd, pst);
+               return SMB_VFS_FSTAT(fsp,fsp->fd, pst);
 }