Fixed sys_lseek and seek_file calls so all returns
[samba.git] / source / smbd / fileio.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    read/write to a files_struct
5    Copyright (C) Andrew Tridgell 1992-1998
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23
24 extern int DEBUGLEVEL;
25
26
27 /****************************************************************************
28 seek a file. Try to avoid the seek if possible
29 ****************************************************************************/
30
31 SMB_OFF_T seek_file(files_struct *fsp,SMB_OFF_T pos)
32 {
33   SMB_OFF_T offset = 0;
34   SMB_OFF_T seek_ret;
35
36   if (fsp->print_file && lp_postscript(fsp->conn->service))
37     offset = 3;
38
39   seek_ret = sys_lseek(fsp->fd_ptr->fd,pos+offset,SEEK_SET);
40
41   if((seek_ret == -1) || (seek_ret != pos+offset)) {
42     DEBUG(0,("seek_file: sys_lseek failed. Error was %s\n", strerror(errno) ));
43     fsp->pos = -1;
44     return -1;
45   }
46
47   fsp->pos = seek_ret - offset;
48
49   DEBUG(10,("seek_file: requested pos = %.0f, new pos = %.0f\n",
50         (double)(pos+offset), (double)fsp->pos ));
51
52   return(fsp->pos);
53 }
54
55 /****************************************************************************
56 read from a file
57 ****************************************************************************/
58
59 ssize_t read_file(files_struct *fsp,char *data,SMB_OFF_T pos,size_t n)
60 {
61   ssize_t ret=0,readret;
62
63 #if USE_READ_PREDICTION
64   if (!fsp->can_write) {
65     ret = read_predict(fsp->fd_ptr->fd,pos,data,NULL,n);
66
67     data += ret;
68     n -= ret;
69     pos += ret;
70   }
71 #endif
72
73 #if WITH_MMAP
74   if (fsp->mmap_ptr) {
75           SMB_OFF_T num = (fsp->mmap_size > pos) ? (fsp->mmap_size - pos) : 0;
76           num = MIN(n,num);
77           if (num > 0) {
78                   memcpy(data,fsp->mmap_ptr+pos,num);
79                   data += num;
80                   pos += num;
81                   n -= num;
82                   ret += num;
83           }
84   }
85 #endif
86
87   if (seek_file(fsp,pos) == -1) {
88     DEBUG(3,("read_file: Failed to seek to %.0f\n",(double)pos));
89     return(ret);
90   }
91   
92   if (n > 0) {
93     readret = read(fsp->fd_ptr->fd,data,n);
94     if (readret > 0) ret += readret;
95   }
96
97   return(ret);
98 }
99
100
101 /****************************************************************************
102 write to a file
103 ****************************************************************************/
104
105 ssize_t write_file(files_struct *fsp,char *data,size_t n)
106 {
107
108   if (!fsp->can_write) {
109     errno = EPERM;
110     return(0);
111   }
112
113   if (!fsp->modified) {
114     SMB_STRUCT_STAT st;
115     fsp->modified = True;
116     if (sys_fstat(fsp->fd_ptr->fd,&st) == 0) {
117       int dosmode = dos_mode(fsp->conn,fsp->fsp_name,&st);
118       if (MAP_ARCHIVE(fsp->conn) && !IS_DOS_ARCHIVE(dosmode)) { 
119         file_chmod(fsp->conn,fsp->fsp_name,dosmode | aARCH,&st);
120       }
121     }  
122   }
123
124   return(write_data(fsp->fd_ptr->fd,data,n));
125 }
126
127
128 /*******************************************************************
129 sync a file
130 ********************************************************************/
131
132 void sync_file(connection_struct *conn, files_struct *fsp)
133 {
134 #ifdef HAVE_FSYNC
135     if(lp_strict_sync(SNUM(conn)))
136       fsync(fsp->fd_ptr->fd);
137 #endif
138 }