d206e3b830b304d18492903d04f9f429a662bb1b
[jelmer/samba4-debian.git] / source / ntvfs / posix / pvfs_write.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    POSIX NTVFS backend - write
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 /*
29   write to a file
30 */
31 NTSTATUS pvfs_write(struct ntvfs_module_context *ntvfs,
32                     struct ntvfs_request *req, union smb_write *wr)
33 {
34         struct pvfs_state *pvfs = ntvfs->private_data;
35         ssize_t ret;
36         struct pvfs_file *f;
37         NTSTATUS status;
38
39         if (wr->generic.level != RAW_WRITE_WRITEX) {
40                 return ntvfs_map_write(ntvfs, req, wr);
41         }
42
43         f = pvfs_find_fd(pvfs, req, wr->writex.in.fnum);
44         if (!f) {
45                 return NT_STATUS_INVALID_HANDLE;
46         }
47
48         if (f->handle->fd == -1) {
49                 return NT_STATUS_FILE_IS_A_DIRECTORY;
50         }
51
52         if (!(f->access_mask & (SEC_FILE_WRITE_DATA | SEC_FILE_APPEND_DATA))) {
53                 return NT_STATUS_ACCESS_VIOLATION;
54         }
55
56         status = pvfs_check_lock(pvfs, f, req->smbpid, 
57                                  wr->writex.in.offset,
58                                  wr->writex.in.count,
59                                  WRITE_LOCK);
60         if (!NT_STATUS_IS_OK(status)) {
61                 return status;
62         }
63         
64         if (f->handle->name->stream_name) {
65                 ret = pvfs_stream_write(pvfs,
66                                         f->handle,
67                                         wr->writex.in.data, 
68                                         wr->writex.in.count,
69                                         wr->writex.in.offset);
70         } else {
71                 ret = pwrite(f->handle->fd, 
72                              wr->writex.in.data, 
73                              wr->writex.in.count,
74                              wr->writex.in.offset);
75         }
76         if (ret == -1) {
77                 if (errno == EFBIG) {
78                         return NT_STATUS_INVALID_PARAMETER;
79                 }
80                 return pvfs_map_errno(pvfs, errno);
81         }
82
83         f->handle->seek_offset = wr->writex.in.offset + ret;
84         
85         wr->writex.out.nwritten = ret;
86         wr->writex.out.remaining = 0; /* should fill this in? */
87         
88         return NT_STATUS_OK;
89 }