r3545: initial support for using extended attributes to hold extended dos attributes...
[kai/samba-autobuild/.git] / source4 / ntvfs / posix / pvfs_setfileinfo.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    POSIX NTVFS backend - setfileinfo
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 "system/time.h"
26
27 /*
28   set info on a open file
29 */
30 NTSTATUS pvfs_setfileinfo(struct ntvfs_module_context *ntvfs,
31                           struct smbsrv_request *req, 
32                           union smb_setfileinfo *info)
33 {
34         struct pvfs_state *pvfs = ntvfs->private_data;
35         struct utimbuf unix_times;
36         struct pvfs_file *f;
37         uint32_t create_options;
38         struct pvfs_filename newstats;
39         NTSTATUS status;
40
41         f = pvfs_find_fd(pvfs, req, info->generic.file.fnum);
42         if (!f) {
43                 return NT_STATUS_INVALID_HANDLE;
44         }
45
46         /* update the file information */
47         status = pvfs_resolve_name_fd(pvfs, f->fd, f->name);
48         if (!NT_STATUS_IS_OK(status)) {
49                 return status;
50         }
51
52         /* we take a copy of the current file stats, then update
53            newstats in each of the elements below. At the end we
54            compare, and make any changes needed */
55         newstats = *f->name;
56
57         switch (info->generic.level) {
58         case RAW_SFILEINFO_SETATTR:
59                 if (!null_time(info->setattr.in.write_time)) {
60                         unix_to_nt_time(&newstats.dos.write_time, info->setattr.in.write_time);
61                 }
62                 if (info->setattr.in.attrib != FILE_ATTRIBUTE_NORMAL) {
63                         newstats.dos.attrib = info->setattr.in.attrib;
64                 }
65                 break;
66
67         case RAW_SFILEINFO_SETATTRE:
68         case RAW_SFILEINFO_STANDARD:
69                 if (!null_time(info->setattre.in.create_time)) {
70                         unix_to_nt_time(&newstats.dos.create_time, info->setattre.in.create_time);
71                 }
72                 if (!null_time(info->setattre.in.access_time)) {
73                         unix_to_nt_time(&newstats.dos.access_time, info->setattre.in.access_time);
74                 }
75                 if (!null_time(info->setattre.in.write_time)) {
76                         unix_to_nt_time(&newstats.dos.write_time, info->setattre.in.write_time);
77                 }
78                 break;
79
80         case RAW_SFILEINFO_BASIC_INFO:
81         case RAW_SFILEINFO_BASIC_INFORMATION:
82                 if (info->basic_info.in.create_time) {
83                         newstats.dos.create_time = info->basic_info.in.create_time;
84                 }
85                 if (info->basic_info.in.access_time) {
86                         newstats.dos.access_time = info->basic_info.in.access_time;
87                 }
88                 if (info->basic_info.in.write_time) {
89                         newstats.dos.write_time = info->basic_info.in.write_time;
90                 }
91                 if (info->basic_info.in.change_time) {
92                         newstats.dos.change_time = info->basic_info.in.change_time;
93                 }
94                 if (info->basic_info.in.attrib != 0) {
95                         newstats.dos.attrib = info->basic_info.in.attrib;
96                 }
97                 break;
98
99         case RAW_SFILEINFO_DISPOSITION_INFO:
100         case RAW_SFILEINFO_DISPOSITION_INFORMATION:
101                 if (!(f->access_mask & STD_RIGHT_DELETE_ACCESS)) {
102                         return NT_STATUS_ACCESS_DENIED;
103                 }
104                 create_options = f->create_options;
105                 if (info->disposition_info.in.delete_on_close) {
106                         create_options |= NTCREATEX_OPTIONS_DELETE_ON_CLOSE;
107                 } else {
108                         create_options &= ~NTCREATEX_OPTIONS_DELETE_ON_CLOSE;
109                 }
110                 return pvfs_change_create_options(pvfs, req, f, create_options);
111
112         case RAW_SFILEINFO_ALLOCATION_INFO:
113         case RAW_SFILEINFO_ALLOCATION_INFORMATION:
114                 newstats.dos.alloc_size = info->allocation_info.in.alloc_size;
115                 break;
116
117         case RAW_SFILEINFO_END_OF_FILE_INFO:
118         case RAW_SFILEINFO_END_OF_FILE_INFORMATION:
119                 newstats.st.st_size = info->end_of_file_info.in.size;
120                 break;
121
122         case RAW_SFILEINFO_POSITION_INFORMATION:
123                 f->position = info->position_information.in.position;
124                 break;
125
126         default:
127                 return NT_STATUS_INVALID_LEVEL;
128         }
129
130         /* possibly change the file size */
131         if (newstats.st.st_size != f->name->st.st_size) {
132                 if (f->name->dos.attrib & FILE_ATTRIBUTE_DIRECTORY) {
133                         return NT_STATUS_FILE_IS_A_DIRECTORY;
134                 }
135                 if (ftruncate(f->fd, newstats.st.st_size) == -1) {
136                         return pvfs_map_errno(pvfs, errno);
137                 }
138         }
139
140         /* possibly change the file timestamps */
141         ZERO_STRUCT(unix_times);
142         if (newstats.dos.access_time != f->name->dos.access_time) {
143                 unix_times.actime = nt_time_to_unix(newstats.dos.access_time);
144         }
145         if (newstats.dos.write_time != f->name->dos.write_time) {
146                 unix_times.modtime = nt_time_to_unix(newstats.dos.write_time);
147         }
148         if (unix_times.actime != 0 || unix_times.modtime != 0) {
149                 if (utime(f->name->full_name, &unix_times) == -1) {
150                         return pvfs_map_errno(pvfs, errno);
151                 }
152         }
153
154         /* possibly change the attribute */
155         if (newstats.dos.attrib != f->name->dos.attrib) {
156                 mode_t mode = pvfs_fileperms(pvfs, newstats.dos.attrib);
157                 if (f->name->dos.attrib & FILE_ATTRIBUTE_DIRECTORY) {
158                         /* ignore on directories for now */
159                         return NT_STATUS_OK;
160                 }
161                 if (fchmod(f->fd, mode) == -1) {
162                         return pvfs_map_errno(pvfs, errno);
163                 }
164         }
165
166         *f->name = newstats;
167
168 #if HAVE_XATTR_SUPPORT
169         if (pvfs->flags & PVFS_FLAG_XATTR_ENABLE) {
170                 return pvfs_xattr_save(pvfs, f->name, f->fd);
171         }
172 #endif
173
174         return NT_STATUS_OK;
175 }
176
177
178 /*
179   set info on a pathname
180 */
181 NTSTATUS pvfs_setpathinfo(struct ntvfs_module_context *ntvfs,
182                           struct smbsrv_request *req, union smb_setfileinfo *info)
183 {
184         struct pvfs_state *pvfs = ntvfs->private_data;
185         struct pvfs_filename *name;
186         struct pvfs_filename newstats;
187         NTSTATUS status;
188         struct utimbuf unix_times;
189
190         /* resolve the cifs name to a posix name */
191         status = pvfs_resolve_name(pvfs, req, info->generic.file.fname, 
192                                    PVFS_RESOLVE_NO_WILDCARD, &name);
193         if (!NT_STATUS_IS_OK(status)) {
194                 return status;
195         }
196
197         if (!name->exists) {
198                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
199         }
200
201
202         /* we take a copy of the current file stats, then update
203            newstats in each of the elements below. At the end we
204            compare, and make any changes needed */
205         newstats = *name;
206
207         switch (info->generic.level) {
208         case RAW_SFILEINFO_SETATTR:
209                 if (!null_time(info->setattr.in.write_time)) {
210                         unix_to_nt_time(&newstats.dos.write_time, info->setattr.in.write_time);
211                 }
212                 if (info->setattr.in.attrib != FILE_ATTRIBUTE_NORMAL) {
213                         newstats.dos.attrib = info->setattr.in.attrib;
214                 }
215                 break;
216
217         case RAW_SFILEINFO_SETATTRE:
218         case RAW_SFILEINFO_STANDARD:
219                 if (!null_time(info->setattre.in.create_time)) {
220                         unix_to_nt_time(&newstats.dos.create_time, info->setattre.in.create_time);
221                 }
222                 if (!null_time(info->setattre.in.access_time)) {
223                         unix_to_nt_time(&newstats.dos.access_time, info->setattre.in.access_time);
224                 }
225                 if (!null_time(info->setattre.in.write_time)) {
226                         unix_to_nt_time(&newstats.dos.write_time, info->setattre.in.write_time);
227                 }
228                 break;
229
230         case RAW_SFILEINFO_BASIC_INFO:
231         case RAW_SFILEINFO_BASIC_INFORMATION:
232                 if (info->basic_info.in.create_time) {
233                         newstats.dos.create_time = info->basic_info.in.create_time;
234                 }
235                 if (info->basic_info.in.access_time) {
236                         newstats.dos.access_time = info->basic_info.in.access_time;
237                 }
238                 if (info->basic_info.in.write_time) {
239                         newstats.dos.write_time = info->basic_info.in.write_time;
240                 }
241                 if (info->basic_info.in.change_time) {
242                         newstats.dos.change_time = info->basic_info.in.change_time;
243                 }
244                 if (info->basic_info.in.attrib != 0) {
245                         newstats.dos.attrib = info->basic_info.in.attrib;
246                 }
247                 break;
248
249         case RAW_SFILEINFO_ALLOCATION_INFO:
250         case RAW_SFILEINFO_ALLOCATION_INFORMATION:
251                 newstats.dos.alloc_size = info->allocation_info.in.alloc_size;
252                 break;
253
254         case RAW_SFILEINFO_END_OF_FILE_INFO:
255         case RAW_SFILEINFO_END_OF_FILE_INFORMATION:
256                 newstats.st.st_size = info->end_of_file_info.in.size;
257                 break;
258
259         case RAW_SFILEINFO_DISPOSITION_INFO:
260         case RAW_SFILEINFO_DISPOSITION_INFORMATION:
261         case RAW_SFILEINFO_POSITION_INFORMATION:
262                 return NT_STATUS_OK;
263
264         default:
265                 return NT_STATUS_INVALID_LEVEL;
266         }
267
268         /* possibly change the file size */
269         if (newstats.st.st_size != name->st.st_size) {
270                 if (truncate(name->full_name, newstats.st.st_size) == -1) {
271                         return pvfs_map_errno(pvfs, errno);
272                 }
273         }
274
275         /* possibly change the file timestamps */
276         ZERO_STRUCT(unix_times);
277         if (newstats.dos.access_time != name->dos.access_time) {
278                 unix_times.actime = nt_time_to_unix(newstats.dos.access_time);
279         }
280         if (newstats.dos.write_time != name->dos.write_time) {
281                 unix_times.modtime = nt_time_to_unix(newstats.dos.write_time);
282         }
283         if (unix_times.actime != 0 || unix_times.modtime != 0) {
284                 if (utime(name->full_name, &unix_times) == -1) {
285                         return pvfs_map_errno(pvfs, errno);
286                 }
287         }
288
289         /* possibly change the attribute */
290         if (newstats.dos.attrib != name->dos.attrib) {
291                 mode_t mode = pvfs_fileperms(pvfs, newstats.dos.attrib);
292                 if (chmod(name->full_name, mode) == -1) {
293                         return pvfs_map_errno(pvfs, errno);
294                 }
295         }
296
297         *name = newstats;
298
299 #if HAVE_XATTR_SUPPORT
300         if (pvfs->flags & PVFS_FLAG_XATTR_ENABLE) {
301                 return pvfs_xattr_save(pvfs, name, -1);
302         }
303 #endif
304
305         return NT_STATUS_OK;
306 }
307