summaryrefslogtreecommitdiff
path: root/source4/ntvfs/posix/pvfs_setfileinfo.c
blob: e570743abacc43ecc8aba1e793150572f8b7692d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
/* 
   Unix SMB/CIFS implementation.

   POSIX NTVFS backend - setfileinfo

   Copyright (C) Andrew Tridgell 2004

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"
#include "vfs_posix.h"
#include "system/time.h"
#include "librpc/gen_ndr/ndr_xattr.h"


/*
  add a single DOS EA
*/
static NTSTATUS pvfs_setfileinfo_ea_set(struct pvfs_state *pvfs, 
					struct pvfs_filename *name,
					int fd, struct ea_struct *ea)
{
	struct xattr_DosEAs *ealist = talloc_p(pvfs, struct xattr_DosEAs);
	int i;
	NTSTATUS status;

	if (!(pvfs->flags & PVFS_FLAG_XATTR_ENABLE)) {
		return NT_STATUS_NOT_SUPPORTED;
	}

	/* load the current list */
	status = pvfs_doseas_load(pvfs, name, fd, ealist);
	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}

	/* see if its already there */
	for (i=0;i<ealist->num_eas;i++) {
		if (StrCaseCmp(ealist->eas[i].name, ea->name.s) == 0) {
			ealist->eas[i].value = ea->value;
			goto save;
		}
	}

	/* add it */
	ealist->eas = talloc_realloc_p(ealist, ealist->eas, struct xattr_EA, ealist->num_eas+1);
	if (ealist->eas == NULL) {
		return NT_STATUS_NO_MEMORY;
	}
	ealist->eas[i].name = ea->name.s;
	ealist->eas[i].value = ea->value;
	ealist->num_eas++;
	
save:
	status = pvfs_doseas_save(pvfs, name, fd, ealist);
	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}

	name->dos.ea_size = 4;
	for (i=0;i<ealist->num_eas;i++) {
		name->dos.ea_size += 4 + strlen(ealist->eas[i].name)+1 + 
			ealist->eas[i].value.length;
	}

	/* update the ea_size attrib */
	return pvfs_dosattrib_save(pvfs, name, fd);
}

/*
  set info on a open file
*/
NTSTATUS pvfs_setfileinfo(struct ntvfs_module_context *ntvfs,
			  struct smbsrv_request *req, 
			  union smb_setfileinfo *info)
{
	struct pvfs_state *pvfs = ntvfs->private_data;
	struct utimbuf unix_times;
	struct pvfs_file *f;
	uint32_t create_options;
	struct pvfs_filename newstats;
	NTSTATUS status;

	f = pvfs_find_fd(pvfs, req, info->generic.file.fnum);
	if (!f) {
		return NT_STATUS_INVALID_HANDLE;
	}

	/* update the file information */
	status = pvfs_resolve_name_fd(pvfs, f->fd, f->name);
	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}

	/* we take a copy of the current file stats, then update
	   newstats in each of the elements below. At the end we
	   compare, and make any changes needed */
	newstats = *f->name;

	switch (info->generic.level) {
	case RAW_SFILEINFO_SETATTR:
		if (!null_time(info->setattr.in.write_time)) {
			unix_to_nt_time(&newstats.dos.write_time, info->setattr.in.write_time);
		}
		if (info->setattr.in.attrib != FILE_ATTRIBUTE_NORMAL) {
			newstats.dos.attrib = info->setattr.in.attrib;
		}
  		break;

	case RAW_SFILEINFO_SETATTRE:
	case RAW_SFILEINFO_STANDARD:
		if (!null_time(info->setattre.in.create_time)) {
			unix_to_nt_time(&newstats.dos.create_time, info->setattre.in.create_time);
		}
		if (!null_time(info->setattre.in.access_time)) {
			unix_to_nt_time(&newstats.dos.access_time, info->setattre.in.access_time);
		}
		if (!null_time(info->setattre.in.write_time)) {
			unix_to_nt_time(&newstats.dos.write_time, info->setattre.in.write_time);
		}
  		break;

	case RAW_SFILEINFO_EA_SET:
		return pvfs_setfileinfo_ea_set(pvfs, f->name, f->fd, &info->ea_set.in.ea);

	case RAW_SFILEINFO_BASIC_INFO:
	case RAW_SFILEINFO_BASIC_INFORMATION:
		if (info->basic_info.in.create_time) {
			newstats.dos.create_time = info->basic_info.in.create_time;
		}
		if (info->basic_info.in.access_time) {
			newstats.dos.access_time = info->basic_info.in.access_time;
		}
		if (info->basic_info.in.write_time) {
			newstats.dos.write_time = info->basic_info.in.write_time;
		}
		if (info->basic_info.in.change_time) {
			newstats.dos.change_time = info->basic_info.in.change_time;
		}
		if (info->basic_info.in.attrib != 0) {
			newstats.dos.attrib = info->basic_info.in.attrib;
		}
  		break;

	case RAW_SFILEINFO_DISPOSITION_INFO:
	case RAW_SFILEINFO_DISPOSITION_INFORMATION:
		if (!(f->access_mask & STD_RIGHT_DELETE_ACCESS)) {
			return NT_STATUS_ACCESS_DENIED;
		}
		create_options = f->create_options;
		if (info->disposition_info.in.delete_on_close) {
			create_options |= NTCREATEX_OPTIONS_DELETE_ON_CLOSE;
		} else {
			create_options &= ~NTCREATEX_OPTIONS_DELETE_ON_CLOSE;
		}
		return pvfs_change_create_options(pvfs, req, f, create_options);

	case RAW_SFILEINFO_ALLOCATION_INFO:
	case RAW_SFILEINFO_ALLOCATION_INFORMATION:
		newstats.dos.alloc_size = info->allocation_info.in.alloc_size;
		break;

	case RAW_SFILEINFO_END_OF_FILE_INFO:
	case RAW_SFILEINFO_END_OF_FILE_INFORMATION:
		newstats.st.st_size = info->end_of_file_info.in.size;
		break;

	case RAW_SFILEINFO_POSITION_INFORMATION:
		f->position = info->position_information.in.position;
		break;

	default:
		return NT_STATUS_INVALID_LEVEL;
	}

	/* possibly change the file size */
	if (newstats.st.st_size != f->name->st.st_size) {
		if (f->name->dos.attrib & FILE_ATTRIBUTE_DIRECTORY) {
			return NT_STATUS_FILE_IS_A_DIRECTORY;
		}
		if (ftruncate(f->fd, newstats.st.st_size) == -1) {
			return pvfs_map_errno(pvfs, errno);
		}
	}

	/* possibly change the file timestamps */
	ZERO_STRUCT(unix_times);
	if (newstats.dos.access_time != f->name->dos.access_time) {
		unix_times.actime = nt_time_to_unix(newstats.dos.access_time);
	}
	if (newstats.dos.write_time != f->name->dos.write_time) {
		unix_times.modtime = nt_time_to_unix(newstats.dos.write_time);
	}
	if (unix_times.actime != 0 || unix_times.modtime != 0) {
		if (utime(f->name->full_name, &unix_times) == -1) {
			return pvfs_map_errno(pvfs, errno);
		}
	}

	/* possibly change the attribute */
	if (newstats.dos.attrib != f->name->dos.attrib) {
		mode_t mode = pvfs_fileperms(pvfs, newstats.dos.attrib);
		if (f->name->dos.attrib & FILE_ATTRIBUTE_DIRECTORY) {
			/* ignore on directories for now */
			return NT_STATUS_OK;
		}
		if (fchmod(f->fd, mode) == -1) {
			return pvfs_map_errno(pvfs, errno);
		}
	}

	*f->name = newstats;

	return pvfs_dosattrib_save(pvfs, f->name, f->fd);
}


/*
  set info on a pathname
*/
NTSTATUS pvfs_setpathinfo(struct ntvfs_module_context *ntvfs,
			  struct smbsrv_request *req, union smb_setfileinfo *info)
{
	struct pvfs_state *pvfs = ntvfs->private_data;
	struct pvfs_filename *name;
	struct pvfs_filename newstats;
	NTSTATUS status;
	struct utimbuf unix_times;

	/* resolve the cifs name to a posix name */
	status = pvfs_resolve_name(pvfs, req, info->generic.file.fname, 
				   PVFS_RESOLVE_NO_WILDCARD, &name);
	if (!NT_STATUS_IS_OK(status)) {
		return status;
	}

	if (!name->exists) {
		return NT_STATUS_OBJECT_NAME_NOT_FOUND;
	}


	/* we take a copy of the current file stats, then update
	   newstats in each of the elements below. At the end we
	   compare, and make any changes needed */
	newstats = *name;

	switch (info->generic.level) {
	case RAW_SFILEINFO_SETATTR:
		if (!null_time(info->setattr.in.write_time)) {
			unix_to_nt_time(&newstats.dos.write_time, info->setattr.in.write_time);
		}
		if (info->setattr.in.attrib != FILE_ATTRIBUTE_NORMAL) {
			newstats.dos.attrib = info->setattr.in.attrib;
		}
  		break;

	case RAW_SFILEINFO_SETATTRE:
	case RAW_SFILEINFO_STANDARD:
		if (!null_time(info->setattre.in.create_time)) {
			unix_to_nt_time(&newstats.dos.create_time, info->setattre.in.create_time);
		}
		if (!null_time(info->setattre.in.access_time)) {
			unix_to_nt_time(&newstats.dos.access_time, info->setattre.in.access_time);
		}
		if (!null_time(info->setattre.in.write_time)) {
			unix_to_nt_time(&newstats.dos.write_time, info->setattre.in.write_time);
		}
  		break;

	case RAW_SFILEINFO_EA_SET:
		return pvfs_setfileinfo_ea_set(pvfs, name, -1, &info->ea_set.in.ea);

	case RAW_SFILEINFO_BASIC_INFO:
	case RAW_SFILEINFO_BASIC_INFORMATION:
		if (info->basic_info.in.create_time) {
			newstats.dos.create_time = info->basic_info.in.create_time;
		}
		if (info->basic_info.in.access_time) {
			newstats.dos.access_time = info->basic_info.in.access_time;
		}
		if (info->basic_info.in.write_time) {
			newstats.dos.write_time = info->basic_info.in.write_time;
		}
		if (info->basic_info.in.change_time) {
			newstats.dos.change_time = info->basic_info.in.change_time;
		}
		if (info->basic_info.in.attrib != 0) {
			newstats.dos.attrib = info->basic_info.in.attrib;
		}
  		break;

	case RAW_SFILEINFO_ALLOCATION_INFO:
	case RAW_SFILEINFO_ALLOCATION_INFORMATION:
		newstats.dos.alloc_size = info->allocation_info.in.alloc_size;
		break;

	case RAW_SFILEINFO_END_OF_FILE_INFO:
	case RAW_SFILEINFO_END_OF_FILE_INFORMATION:
		newstats.st.st_size = info->end_of_file_info.in.size;
		break;

	case RAW_SFILEINFO_DISPOSITION_INFO:
	case RAW_SFILEINFO_DISPOSITION_INFORMATION:
	case RAW_SFILEINFO_POSITION_INFORMATION:
		return NT_STATUS_OK;

	default:
		return NT_STATUS_INVALID_LEVEL;
	}

	/* possibly change the file size */
	if (newstats.st.st_size != name->st.st_size) {
		if (truncate(name->full_name, newstats.st.st_size) == -1) {
			return pvfs_map_errno(pvfs, errno);
		}
	}

	/* possibly change the file timestamps */
	ZERO_STRUCT(unix_times);
	if (newstats.dos.access_time != name->dos.access_time) {
		unix_times.actime = nt_time_to_unix(newstats.dos.access_time);
	}
	if (newstats.dos.write_time != name->dos.write_time) {
		unix_times.modtime = nt_time_to_unix(newstats.dos.write_time);
	}
	if (unix_times.actime != 0 || unix_times.modtime != 0) {
		if (utime(name->full_name, &unix_times) == -1) {
			return pvfs_map_errno(pvfs, errno);
		}
	}

	/* possibly change the attribute */
	if (newstats.dos.attrib != name->dos.attrib) {
		mode_t mode = pvfs_fileperms(pvfs, newstats.dos.attrib);
		if (chmod(name->full_name, mode) == -1) {
			return pvfs_map_errno(pvfs, errno);
		}
	}

	*name = newstats;

	return pvfs_dosattrib_save(pvfs, name, -1);
}