xref: /titanic_44/usr/src/uts/common/syscall/rename.c (revision 8339b41da2395f0525c46ceedfeb01961893ec44)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 1994, 2010, Oracle and/or its affiliates. All rights reserved.
24  */
25 
26 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
27 /*	  All Rights Reserved  	*/
28 
29 /*
30  * Portions of this source code were derived from Berkeley 4.3 BSD
31  * under license from the Regents of the University of California.
32  */
33 
34 #include <sys/param.h>
35 #include <sys/isa_defs.h>
36 #include <sys/types.h>
37 #include <sys/sysmacros.h>
38 #include <sys/systm.h>
39 #include <sys/errno.h>
40 #include <sys/vnode.h>
41 #include <sys/uio.h>
42 #include <sys/debug.h>
43 #include <sys/file.h>
44 #include <sys/fcntl.h>
45 
46 /*
47  * Rename a file relative to a given directory
48  */
49 int
50 renameat(int fromfd, char *old, int tofd, char *new)
51 {
52 	vnode_t	*fromvp = NULL;
53 	vnode_t	*tovp = NULL;
54 	file_t *fp;
55 	int error;
56 	char oldstart;
57 	char newstart;
58 
59 	if (copyin(old, &oldstart, sizeof (char)) ||
60 	    copyin(new, &newstart, sizeof (char)))
61 		return (set_errno(EFAULT));
62 
63 	if (fromfd == AT_FDCWD || tofd == AT_FDCWD) {
64 		proc_t *p = curproc;
65 
66 		mutex_enter(&p->p_lock);
67 		if (fromfd == AT_FDCWD) {
68 			fromvp = PTOU(p)->u_cdir;
69 			VN_HOLD(fromvp);
70 		}
71 		if (tofd == AT_FDCWD) {
72 			tovp = PTOU(p)->u_cdir;
73 			VN_HOLD(tovp);
74 		}
75 		mutex_exit(&p->p_lock);
76 	}
77 
78 	if (fromvp == NULL && oldstart != '/') {
79 		if ((fp = getf(fromfd)) == NULL) {
80 			if (tovp != NULL)
81 				VN_RELE(tovp);
82 			return (set_errno(EBADF));
83 		}
84 		fromvp = fp->f_vnode;
85 		VN_HOLD(fromvp);
86 		releasef(fromfd);
87 	}
88 
89 	if (tovp == NULL && newstart != '/') {
90 		if ((fp = getf(tofd)) == NULL) {
91 			if (fromvp != NULL)
92 				VN_RELE(fromvp);
93 			return (set_errno(EBADF));
94 		}
95 		tovp = fp->f_vnode;
96 		VN_HOLD(tovp);
97 		releasef(tofd);
98 	}
99 
100 	error = vn_renameat(fromvp, old, tovp, new, UIO_USERSPACE);
101 
102 	if (fromvp != NULL)
103 		VN_RELE(fromvp);
104 	if (tovp != NULL)
105 		VN_RELE(tovp);
106 	if (error)
107 		return (set_errno(error));
108 	return (0);
109 }
110 
111 int
112 rename(char *old, char *new)
113 {
114 	return (renameat(AT_FDCWD, old, AT_FDCWD, new));
115 }
116