971d309ff9861e07b3732ed23f9338ced45b3143
[samba.git] / source3 / 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 int seek_file(files_struct *fsp,uint32 pos)
31 {
32   uint32 offset = 0;
33
34   if (fsp->print_file && lp_postscript(fsp->conn->service))
35     offset = 3;
36
37   fsp->pos = (int)(lseek(fsp->fd_ptr->fd,pos+offset,SEEK_SET) - offset);
38   return(fsp->pos);
39 }
40
41 /****************************************************************************
42 read from a file
43 ****************************************************************************/
44 int read_file(files_struct *fsp,char *data,uint32 pos,int n)
45 {
46   int ret=0,readret;
47
48 #if USE_READ_PREDICTION
49   if (!fsp->can_write)
50     {
51       ret = read_predict(fsp->fd_ptr->fd,pos,data,NULL,n);
52
53       data += ret;
54       n -= ret;
55       pos += ret;
56     }
57 #endif
58
59 #if WITH_MMAP
60   if (fsp->mmap_ptr)
61     {
62       int num = (fsp->mmap_size > pos) ? (fsp->mmap_size - pos) : -1;
63       num = MIN(n,num);
64       if (num > 0)
65         {
66           memcpy(data,fsp->mmap_ptr+pos,num);
67           data += num;
68           pos += num;
69           n -= num;
70           ret += num;
71         }
72     }
73 #endif
74
75   if (n <= 0)
76     return(ret);
77
78   if (seek_file(fsp,pos) != pos)
79     {
80       DEBUG(3,("Failed to seek to %d\n",pos));
81       return(ret);
82     }
83   
84   if (n > 0) {
85     readret = read(fsp->fd_ptr->fd,data,n);
86     if (readret > 0) ret += readret;
87   }
88
89   return(ret);
90 }
91
92
93 /****************************************************************************
94 write to a file
95 ****************************************************************************/
96 int write_file(files_struct *fsp,char *data,int n)
97 {
98
99   if (!fsp->can_write) {
100     errno = EPERM;
101     return(0);
102   }
103
104   if (!fsp->modified) {
105     struct stat st;
106     fsp->modified = True;
107     if (fstat(fsp->fd_ptr->fd,&st) == 0) {
108       int dosmode = dos_mode(fsp->conn,fsp->fsp_name,&st);
109       if (MAP_ARCHIVE(fsp->conn) && !IS_DOS_ARCHIVE(dosmode)) { 
110         dos_chmod(fsp->conn,fsp->fsp_name,dosmode | aARCH,&st);
111       }
112     }  
113   }
114
115   return(write_data(fsp->fd_ptr->fd,data,n));
116 }
117
118
119 /*******************************************************************
120 sync a file
121 ********************************************************************/
122 void sync_file(connection_struct *conn, files_struct *fsp)
123 {
124 #ifdef HAVE_FSYNC
125     if(lp_strict_sync(SNUM(conn)))
126       fsync(fsp->fd_ptr->fd);
127 #endif
128 }
129