r14173: change smb interface structures to always use
[kai/samba.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 "includes.h"
24 #include "vfs_posix.h"
25 #include "librpc/gen_ndr/ndr_security.h"
26
27 /*
28   read from a file
29 */
30 NTSTATUS pvfs_read(struct ntvfs_module_context *ntvfs,
31                    struct ntvfs_request *req, union smb_read *rd)
32 {
33         struct pvfs_state *pvfs = ntvfs->private_data;
34         ssize_t ret;
35         struct pvfs_file *f;
36         NTSTATUS status;
37         uint32_t maxcnt;
38         uint32_t mask;
39
40         if (rd->generic.level != RAW_READ_READX) {
41                 return ntvfs_map_read(ntvfs, req, rd);
42         }
43
44         f = pvfs_find_fd(pvfs, req, rd->readx.file.fnum);
45         if (!f) {
46                 return NT_STATUS_INVALID_HANDLE;
47         }
48
49         if (f->handle->fd == -1) {
50                 return NT_STATUS_FILE_IS_A_DIRECTORY;
51         }
52
53         mask = SEC_FILE_READ_DATA;
54         if (req->flags2 & FLAGS2_READ_PERMIT_EXECUTE) {
55                 mask |= SEC_FILE_EXECUTE;
56         }
57         if (!(f->access_mask & mask)) {
58                 return NT_STATUS_ACCESS_DENIED;
59         }
60
61         maxcnt = rd->readx.in.maxcnt;
62         if (maxcnt > UINT16_MAX) {
63                 maxcnt = 0;
64         }
65
66         status = pvfs_check_lock(pvfs, f, req->smbpid, 
67                                  rd->readx.in.offset,
68                                  maxcnt,
69                                  READ_LOCK);
70         if (!NT_STATUS_IS_OK(status)) {
71                 return status;
72         }
73
74         if (f->handle->name->stream_name) {
75                 ret = pvfs_stream_read(pvfs, f->handle, 
76                                        rd->readx.out.data, maxcnt, rd->readx.in.offset);
77         } else {
78                 ret = pread(f->handle->fd, 
79                             rd->readx.out.data, 
80                             maxcnt,
81                             rd->readx.in.offset);
82         }
83         if (ret == -1) {
84                 return pvfs_map_errno(pvfs, errno);
85         }
86
87         f->handle->position = f->handle->seek_offset = rd->readx.in.offset + ret;
88
89         rd->readx.out.nread = ret;
90         rd->readx.out.remaining = 0xFFFF;
91         rd->readx.out.compaction_mode = 0; 
92
93         return NT_STATUS_OK;
94 }