2 Unix SMB/Netbios implementation.
4 read/write to a files_struct
5 Copyright (C) Andrew Tridgell 1992-1998
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.
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.
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.
24 extern int DEBUGLEVEL;
27 /****************************************************************************
28 seek a file. Try to avoid the seek if possible
29 ****************************************************************************/
31 SMB_OFF_T seek_file(files_struct *fsp,SMB_OFF_T pos)
35 if (fsp->print_file && lp_postscript(fsp->conn->service))
38 fsp->pos = (sys_lseek(fsp->fd_ptr->fd,pos+offset,SEEK_SET) - offset);
40 DEBUG(10,("seek_file: requested pos = %.0f, new pos = %.0f\n",
41 (double)(pos+offset), (double)fsp->pos ));
46 /****************************************************************************
48 ****************************************************************************/
50 ssize_t read_file(files_struct *fsp,char *data,SMB_OFF_T pos,size_t n)
52 ssize_t ret=0,readret;
54 #if USE_READ_PREDICTION
55 if (!fsp->can_write) {
56 ret = read_predict(fsp->fd_ptr->fd,pos,data,NULL,n);
66 SMB_OFF_T num = (fsp->mmap_size > pos) ? (fsp->mmap_size - pos) : -1;
69 memcpy(data,fsp->mmap_ptr+pos,num);
78 if (seek_file(fsp,pos) != pos) {
79 DEBUG(3,("read_file: Failed to seek to %.0f\n",(double)pos));
84 readret = read(fsp->fd_ptr->fd,data,n);
85 if (readret > 0) ret += readret;
92 /****************************************************************************
94 ****************************************************************************/
96 ssize_t write_file(files_struct *fsp,char *data,size_t n)
99 if (!fsp->can_write) {
104 if (!fsp->modified) {
106 fsp->modified = True;
107 if (sys_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 file_chmod(fsp->conn,fsp->fsp_name,dosmode | aARCH,&st);
115 return(write_data(fsp->fd_ptr->fd,data,n));
119 /*******************************************************************
121 ********************************************************************/
123 void sync_file(connection_struct *conn, files_struct *fsp)
126 if(lp_strict_sync(SNUM(conn)))
127 fsync(fsp->fd_ptr->fd);