r3241: don't skip the read completely for a zero-length read, as it could give a...
[ira/wip.git] / source4 / ntvfs / posix / pvfs_read.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    POSIX NTVFS backend - read
5
6    Copyright (C) Andrew Tridgell 2004
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "include/includes.h"
24 #include "vfs_posix.h"
25
26 /*
27   read from a file
28 */
29 NTSTATUS pvfs_read(struct ntvfs_module_context *ntvfs,
30                    struct smbsrv_request *req, union smb_read *rd)
31 {
32         struct pvfs_state *pvfs = ntvfs->private_data;
33         ssize_t ret;
34         struct pvfs_file *f;
35         NTSTATUS status;
36         uint32_t maxcnt;
37
38         if (rd->generic.level != RAW_READ_READX) {
39                 return ntvfs_map_read(req, rd, ntvfs);
40         }
41
42         f = pvfs_find_fd(pvfs, req, rd->readx.in.fnum);
43         if (!f) {
44                 return NT_STATUS_INVALID_HANDLE;
45         }
46
47         if (f->name->dos.attrib & FILE_ATTRIBUTE_DIRECTORY) {
48                 return NT_STATUS_FILE_IS_A_DIRECTORY;
49         }
50
51         if (!(f->access_mask & SA_RIGHT_FILE_READ_DATA)) {
52                 return NT_STATUS_ACCESS_VIOLATION;
53         }
54
55         maxcnt = rd->readx.in.maxcnt;
56         if (maxcnt > UINT16_MAX) {
57                 maxcnt = 0;
58         }
59
60         status = pvfs_check_lock(pvfs, f, req->smbpid, 
61                                  rd->readx.in.offset,
62                                  maxcnt,
63                                  READ_LOCK);
64         if (!NT_STATUS_IS_OK(status)) {
65                 return status;
66         }
67
68         ret = pread(f->fd, 
69                     rd->readx.out.data, 
70                     maxcnt,
71                     rd->readx.in.offset);
72         if (ret == -1) {
73                 return pvfs_map_errno(pvfs, errno);
74         }
75
76         f->position = f->seek_offset = rd->readx.in.offset + ret;
77
78         rd->readx.out.nread = ret;
79         rd->readx.out.remaining = 0xFFFF;
80         rd->readx.out.compaction_mode = 0; 
81
82         return NT_STATUS_OK;
83 }