xref: /illumos-gate/usr/src/cmd/oamuser/user/movedir.c (revision 24fe0b3bf671e123467ce1df0b67cadd3614c8e4)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 
23 /*
24  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
25  * Use is subject to license terms.
26  */
27 
28 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
29 /*	  All Rights Reserved  	*/
30 
31 
32 #pragma ident	"%Z%%M%	%I%	%E% SMI"
33 
34 #include <sys/types.h>
35 #include <stdio.h>
36 #include <userdefs.h>
37 #include "messages.h"
38 #include <unistd.h>
39 #include <sys/stat.h>
40 #include <utime.h>
41 
42 #define	SBUFSZ	256
43 
44 extern int access(), rm_files();
45 
46 static char cmdbuf[SBUFSZ];	/* buffer for system call */
47 
48 /*
49  *	Move directory contents from one place to another
50  */
51 int
52 move_dir(char *from, char *to, char *login)
53 		/* directory to move files from */
54 		/* dirctory to move files to */
55 		/* login id of owner */
56 {
57 	size_t len = 0;
58 	int rc = EX_SUCCESS;
59 	struct stat statbuf;
60 	struct utimbuf times;
61 	/*
62 	 * ***** THIS IS WHERE SUFFICIENT SPACE CHECK GOES
63 	 */
64 
65 	if (access(from, F_OK) == 0) {	/* home dir exists */
66 		/* move all files */
67 		(void) sprintf(cmdbuf,
68 			"cd %s && find . -print | cpio -m -pd %s",
69 			from, to);
70 
71 		if (system(cmdbuf) != 0) {
72 			errmsg(M_NOSPACE, from, to);
73 			return (EX_NOSPACE);
74 		}
75 
76 		/*
77 		 * Check that to dir is not a subdirectory of from
78 		 */
79 		len = strlen(from);
80 		if (strncmp(from, to, len) == 0 &&
81 		    strncmp(to+len, "/", 1) == 0) {
82 			errmsg(M_RMFILES);
83 			return (EX_HOMEDIR);
84 		}
85 		/* Retain the original permission and modification time */
86 		if (stat(from, &statbuf) == 0) {
87 			chmod(to, statbuf.st_mode);
88 			times.actime = statbuf.st_atime;
89 			times.modtime = statbuf.st_mtime;
90 			(void) utime(to, &times);
91 
92 		}
93 
94 		/* Remove the files in the old place */
95 		rc = rm_files(from, login);
96 
97 	}
98 
99 	return (rc);
100 }
101