xref: /titanic_52/usr/src/uts/common/syscall/rename.c (revision ac88567a7a5bb7f01cf22cf366bc9d6203e24d7a)
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 2010 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 /*
31  * Portions of this source code were derived from Berkeley 4.3 BSD
32  * under license from the Regents of the University of California.
33  */
34 
35 #include <sys/param.h>
36 #include <sys/isa_defs.h>
37 #include <sys/types.h>
38 #include <sys/sysmacros.h>
39 #include <sys/systm.h>
40 #include <sys/errno.h>
41 #include <sys/vnode.h>
42 #include <sys/uio.h>
43 #include <sys/debug.h>
44 #include <sys/file.h>
45 #include <sys/fcntl.h>
46 
47 /*
48  * Rename a file relative to a given directory
49  */
50 int
51 renameat(int fromfd, char *old, int tofd, char *new)
52 {
53 	file_t *fromfp;
54 	file_t *tofp;
55 	vnode_t	*fromvp, *tovp;
56 	int error;
57 	proc_t *p = curproc;
58 	char oldstart, newstart;
59 
60 	tovp = fromvp = NULL;
61 
62 	if ((fromfd == AT_FDCWD && old == NULL) ||
63 	    (tofd == AT_FDCWD && new == NULL))
64 		return (set_errno(EFAULT));
65 
66 	if (fromfd == AT_FDCWD || tofd == AT_FDCWD) {
67 		mutex_enter(&p->p_lock);
68 		if (fromfd == AT_FDCWD) {
69 			fromvp = PTOU(p)->u_cdir;
70 			VN_HOLD(fromvp);
71 		}
72 		if (tofd == AT_FDCWD) {
73 			tovp = PTOU(p)->u_cdir;
74 			VN_HOLD(tovp);
75 		}
76 		mutex_exit(&p->p_lock);
77 	}
78 
79 	if (copyin(old, &oldstart, sizeof (char)))
80 		return (set_errno(EFAULT));
81 
82 	if (copyin(new, &newstart, sizeof (char)))
83 		return (set_errno(EFAULT));
84 
85 	if (fromvp == NULL) {
86 		if (oldstart != '/') {
87 			if ((fromfp = getf(fromfd)) == NULL) {
88 				if (tovp != NULL)
89 					VN_RELE(tovp);
90 				return (set_errno(EBADF));
91 			}
92 			fromvp = fromfp->f_vnode;
93 			VN_HOLD(fromvp);
94 			releasef(fromfd);
95 		} else {
96 			fromvp = NULL;
97 		}
98 	}
99 
100 	if (tovp == NULL) {
101 		if (newstart != '/') {
102 			if ((tofp = getf(tofd)) == NULL) {
103 				if (fromvp != NULL)
104 					VN_RELE(fromvp);
105 				return (set_errno(EBADF));
106 			}
107 			tovp = tofp->f_vnode;
108 			VN_HOLD(tovp);
109 			releasef(tofd);
110 		} else {
111 			tovp = NULL;
112 		}
113 	}
114 
115 	error = vn_renameat(fromvp, old, tovp, new, UIO_USERSPACE);
116 
117 	if (fromvp != NULL)
118 		VN_RELE(fromvp);
119 	if (tovp != NULL)
120 		VN_RELE(tovp);
121 	if (error != 0)
122 		return (set_errno(error));
123 	return (error);
124 }
125 
126 int
127 rename(char *old, char *new)
128 {
129 	return (renameat(AT_FDCWD, old, AT_FDCWD, new));
130 }
131