2 Unix SMB/CIFS implementation.
4 POSIX NTVFS backend - rename
6 Copyright (C) Andrew Tridgell 2004
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 3 of the License, or
11 (at your option) any later version.
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.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "vfs_posix.h"
24 #include "librpc/gen_ndr/security.h"
25 #include "param/param.h"
29 do a file rename, and send any notify triggers
31 NTSTATUS pvfs_do_rename(struct pvfs_state *pvfs, const struct pvfs_filename *name1,
37 if (rename(name1->full_name, name2) == -1) {
38 return pvfs_map_errno(pvfs, errno);
41 if (name1->dos.attrib & FILE_ATTRIBUTE_DIRECTORY) {
42 mask = FILE_NOTIFY_CHANGE_DIR_NAME;
44 mask = FILE_NOTIFY_CHANGE_FILE_NAME;
47 renames to the same directory cause a OLD_NAME->NEW_NAME notify.
48 renames to a different directory are considered a remove/add
50 r1 = strrchr_m(name1->full_name, '/');
51 r2 = strrchr_m(name2, '/');
53 if ((r1-name1->full_name) != (r2-name2) ||
54 strncmp(name1->full_name, name2, r1-name1->full_name) != 0) {
55 notify_trigger(pvfs->notify_context,
56 NOTIFY_ACTION_REMOVED,
59 notify_trigger(pvfs->notify_context,
64 notify_trigger(pvfs->notify_context,
65 NOTIFY_ACTION_OLD_NAME,
68 notify_trigger(pvfs->notify_context,
69 NOTIFY_ACTION_NEW_NAME,
74 /* this is a strange one. w2k3 gives an additional event for CHANGE_ATTRIBUTES
75 and CHANGE_CREATION on the new file when renaming files, but not
77 if ((name1->dos.attrib & FILE_ATTRIBUTE_DIRECTORY) == 0) {
78 notify_trigger(pvfs->notify_context,
79 NOTIFY_ACTION_MODIFIED,
80 FILE_NOTIFY_CHANGE_ATTRIBUTES|FILE_NOTIFY_CHANGE_CREATION,
89 resolve a wildcard rename pattern. This works on one component of the name
91 static const char *pvfs_resolve_wildcard_component(TALLOC_CTX *mem_ctx,
92 struct smb_iconv_convenience *iconv_convenience,
99 /* the length is bounded by the length of the two strings combined */
100 dest = talloc_size(mem_ctx, strlen(fname) + strlen(pattern) + 1);
111 size_t c_size1, c_size2;
112 c1 = next_codepoint(iconv_convenience, p1, &c_size1);
113 c2 = next_codepoint(iconv_convenience, p2, &c_size2);
115 d += push_codepoint(iconv_convenience, d, c1);
116 } else if (c2 == '*') {
117 memcpy(d, p1, strlen(p1));
121 d += push_codepoint(iconv_convenience, d, c2);
134 resolve a wildcard rename pattern.
136 static const char *pvfs_resolve_wildcard(TALLOC_CTX *mem_ctx,
137 struct smb_iconv_convenience *iconv_convenience,
141 const char *base1, *base2;
142 const char *ext1, *ext2;
145 /* break into base part plus extension */
146 p = strrchr_m(fname, '.');
151 ext1 = talloc_strdup(mem_ctx, p+1);
152 base1 = talloc_strndup(mem_ctx, fname, p-fname);
154 if (ext1 == NULL || base1 == NULL) {
158 p = strrchr_m(pattern, '.');
163 ext2 = talloc_strdup(mem_ctx, p+1);
164 base2 = talloc_strndup(mem_ctx, pattern, p-pattern);
166 if (ext2 == NULL || base2 == NULL) {
170 base1 = pvfs_resolve_wildcard_component(mem_ctx, iconv_convenience, base1, base2);
171 ext1 = pvfs_resolve_wildcard_component(mem_ctx, iconv_convenience, ext1, ext2);
172 if (base1 == NULL || ext1 == NULL) {
180 return talloc_asprintf(mem_ctx, "%s.%s", base1, ext1);
184 rename one file from a wildcard set
186 static NTSTATUS pvfs_rename_one(struct pvfs_state *pvfs,
187 struct ntvfs_request *req,
188 const char *dir_path,
193 struct pvfs_filename *name1, *name2;
194 TALLOC_CTX *mem_ctx = talloc_new(req);
195 struct odb_lock *lck = NULL;
198 /* resolve the wildcard pattern for this name */
199 fname2 = pvfs_resolve_wildcard(mem_ctx, lp_iconv_convenience(pvfs->ntvfs->ctx->lp_ctx), fname1, fname2);
200 if (fname2 == NULL) {
201 return NT_STATUS_NO_MEMORY;
204 /* get a pvfs_filename source object */
205 status = pvfs_resolve_partial(pvfs, mem_ctx,
206 dir_path, fname1, &name1);
207 if (!NT_STATUS_IS_OK(status)) {
211 /* make sure its matches the given attributes */
212 status = pvfs_match_attrib(pvfs, name1, attrib, 0);
213 if (!NT_STATUS_IS_OK(status)) {
217 status = pvfs_can_rename(pvfs, req, name1, &lck);
218 if (!NT_STATUS_IS_OK(status)) {
223 /* get a pvfs_filename dest object */
224 status = pvfs_resolve_partial(pvfs, mem_ctx,
225 dir_path, fname2, &name2);
226 if (NT_STATUS_IS_OK(status)) {
227 status = pvfs_can_delete(pvfs, req, name2, NULL);
228 if (!NT_STATUS_IS_OK(status)) {
233 status = NT_STATUS_OK;
235 fname2 = talloc_asprintf(mem_ctx, "%s/%s", dir_path, fname2);
236 if (fname2 == NULL) {
237 return NT_STATUS_NO_MEMORY;
240 status = pvfs_do_rename(pvfs, name1, fname2);
242 if (NT_STATUS_IS_OK(status)) {
243 status = odb_rename(lck, fname2);
247 talloc_free(mem_ctx);
253 rename a set of files with wildcards
255 static NTSTATUS pvfs_rename_wildcard(struct pvfs_state *pvfs,
256 struct ntvfs_request *req,
257 union smb_rename *ren,
258 struct pvfs_filename *name1,
259 struct pvfs_filename *name2)
261 struct pvfs_dir *dir;
264 const char *fname, *fname2, *dir_path;
265 uint16_t attrib = ren->rename.in.attrib;
266 int total_renamed = 0;
268 /* get list of matching files */
269 status = pvfs_list_start(pvfs, name1, req, &dir);
270 if (!NT_STATUS_IS_OK(status)) {
274 status = NT_STATUS_NO_SUCH_FILE;
276 dir_path = pvfs_list_unix_path(dir);
278 /* only allow wildcard renames within a directory */
279 if (strncmp(dir_path, name2->full_name, strlen(dir_path)) != 0 ||
280 name2->full_name[strlen(dir_path)] != '/' ||
281 strchr(name2->full_name + strlen(dir_path) + 1, '/')) {
282 return NT_STATUS_INVALID_PARAMETER;
285 fname2 = talloc_strdup(name2, name2->full_name + strlen(dir_path) + 1);
286 if (fname2 == NULL) {
287 return NT_STATUS_NO_MEMORY;
290 while ((fname = pvfs_list_next(dir, &ofs))) {
291 status = pvfs_rename_one(pvfs, req,
293 fname, fname2, attrib);
294 if (NT_STATUS_IS_OK(status)) {
299 if (total_renamed == 0) {
307 rename a set of files - SMBmv interface
309 static NTSTATUS pvfs_rename_mv(struct ntvfs_module_context *ntvfs,
310 struct ntvfs_request *req, union smb_rename *ren)
312 struct pvfs_state *pvfs = ntvfs->private_data;
314 struct pvfs_filename *name1, *name2;
315 struct odb_lock *lck = NULL;
317 /* resolve the cifs name to a posix name */
318 status = pvfs_resolve_name(pvfs, req, ren->rename.in.pattern1,
319 PVFS_RESOLVE_WILDCARD, &name1);
320 if (!NT_STATUS_IS_OK(status)) {
324 status = pvfs_resolve_name(pvfs, req, ren->rename.in.pattern2,
325 PVFS_RESOLVE_WILDCARD, &name2);
326 if (!NT_STATUS_IS_OK(status)) {
330 if (name1->has_wildcard || name2->has_wildcard) {
331 return pvfs_rename_wildcard(pvfs, req, ren, name1, name2);
334 if (!name1->exists) {
335 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
338 if (strcmp(name1->full_name, name2->full_name) == 0) {
343 return NT_STATUS_OBJECT_NAME_COLLISION;
346 status = pvfs_match_attrib(pvfs, name1, ren->rename.in.attrib, 0);
347 if (!NT_STATUS_IS_OK(status)) {
351 status = pvfs_access_check_parent(pvfs, req, name2, SEC_DIR_ADD_FILE);
352 if (!NT_STATUS_IS_OK(status)) {
356 status = pvfs_can_rename(pvfs, req, name1, &lck);
357 if (!NT_STATUS_IS_OK(status)) {
362 status = pvfs_do_rename(pvfs, name1, name2->full_name);
363 if (NT_STATUS_IS_OK(status)) {
364 status = odb_rename(lck, name2->full_name);
372 rename a set of files - ntrename interface
374 static NTSTATUS pvfs_rename_nt(struct ntvfs_module_context *ntvfs,
375 struct ntvfs_request *req, union smb_rename *ren)
377 struct pvfs_state *pvfs = ntvfs->private_data;
379 struct pvfs_filename *name1, *name2;
381 switch (ren->ntrename.in.flags) {
382 case RENAME_FLAG_RENAME:
383 case RENAME_FLAG_HARD_LINK:
384 case RENAME_FLAG_COPY:
385 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
388 return NT_STATUS_ACCESS_DENIED;
391 /* resolve the cifs name to a posix name */
392 status = pvfs_resolve_name(pvfs, req, ren->ntrename.in.old_name,
393 PVFS_RESOLVE_WILDCARD, &name1);
394 if (!NT_STATUS_IS_OK(status)) {
398 status = pvfs_resolve_name(pvfs, req, ren->ntrename.in.new_name,
399 PVFS_RESOLVE_WILDCARD, &name2);
400 if (!NT_STATUS_IS_OK(status)) {
404 if (name1->has_wildcard || name2->has_wildcard) {
405 return NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
408 if (!name1->exists) {
409 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
412 if (strcmp(name1->full_name, name2->full_name) == 0) {
417 return NT_STATUS_OBJECT_NAME_COLLISION;
420 status = pvfs_match_attrib(pvfs, name1, ren->ntrename.in.attrib, 0);
421 if (!NT_STATUS_IS_OK(status)) {
425 status = pvfs_can_rename(pvfs, req, name1, NULL);
426 if (!NT_STATUS_IS_OK(status)) {
430 switch (ren->ntrename.in.flags) {
431 case RENAME_FLAG_RENAME:
432 status = pvfs_access_check_parent(pvfs, req, name2, SEC_DIR_ADD_FILE);
433 NT_STATUS_NOT_OK_RETURN(status);
434 status = pvfs_do_rename(pvfs, name1, name2->full_name);
435 NT_STATUS_NOT_OK_RETURN(status);
438 case RENAME_FLAG_HARD_LINK:
439 status = pvfs_access_check_parent(pvfs, req, name2, SEC_DIR_ADD_FILE);
440 NT_STATUS_NOT_OK_RETURN(status);
441 if (link(name1->full_name, name2->full_name) == -1) {
442 return pvfs_map_errno(pvfs, errno);
446 case RENAME_FLAG_COPY:
447 status = pvfs_access_check_parent(pvfs, req, name2, SEC_DIR_ADD_FILE);
448 NT_STATUS_NOT_OK_RETURN(status);
449 return pvfs_copy_file(pvfs, name1, name2);
451 case RENAME_FLAG_MOVE_CLUSTER_INFORMATION:
452 return NT_STATUS_INVALID_PARAMETER;
455 return NT_STATUS_ACCESS_DENIED;
463 rename a set of files - ntrename interface
465 NTSTATUS pvfs_rename(struct ntvfs_module_context *ntvfs,
466 struct ntvfs_request *req, union smb_rename *ren)
468 switch (ren->generic.level) {
469 case RAW_RENAME_RENAME:
470 return pvfs_rename_mv(ntvfs, req, ren);
472 case RAW_RENAME_NTRENAME:
473 return pvfs_rename_nt(ntvfs, req, ren);
479 return NT_STATUS_INVALID_LEVEL;