xref: /illumos-gate/usr/src/cmd/oamuser/user/movedir.c (revision 2c5ec7a875dcd76853e6618614e990f1e8cdd56d)
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 #include <sys/types.h>
33 #include <stdio.h>
34 #include <userdefs.h>
35 #include "messages.h"
36 #include <unistd.h>
37 #include <sys/stat.h>
38 #include <utime.h>
39 
40 #define	SBUFSZ	256
41 
42 extern int access(), rm_files();
43 
44 static char cmdbuf[SBUFSZ];	/* buffer for system call */
45 
46 /*
47  *	Move directory contents from one place to another
48  */
49 int
50 move_dir(char *from, char *to, char *login, int flags)
51 		/* directory to move files from */
52 		/* dirctory to move files to */
53 		/* login id of owner */
54 		/* miscellaneous flags */
55 {
56 	size_t len = 0;
57 	int rc = EX_SUCCESS;
58 	struct stat statbuf;
59 	struct utimbuf times;
60 	/*
61 	 * ***** THIS IS WHERE SUFFICIENT SPACE CHECK GOES
62 	 */
63 
64 	if (access(from, F_OK) == 0) {	/* home dir exists */
65 		/* move all files */
66 		(void) sprintf(cmdbuf,
67 			"cd %s && find . -print | cpio -m -pd %s",
68 			from, to);
69 
70 		if (system(cmdbuf) != 0) {
71 			errmsg(M_NOSPACE, from, to);
72 			return (EX_NOSPACE);
73 		}
74 
75 		/*
76 		 * Check that to dir is not a subdirectory of from
77 		 */
78 		len = strlen(from);
79 		if (strncmp(from, to, len) == 0 &&
80 		    strncmp(to+len, "/", 1) == 0) {
81 			errmsg(M_RMFILES);
82 			return (EX_HOMEDIR);
83 		}
84 		/* Retain the original permission and modification time */
85 		if (stat(from, &statbuf) == 0) {
86 			chmod(to, statbuf.st_mode);
87 			times.actime = statbuf.st_atime;
88 			times.modtime = statbuf.st_mtime;
89 			(void) utime(to, &times);
90 
91 		}
92 
93 		/* Remove the files in the old place */
94 		rc = rm_files(from, login, flags);
95 
96 	}
97 
98 	return (rc);
99 }
100