xref: /illumos-gate/usr/src/cmd/oamuser/user/movedir.c (revision 49335bde)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate 
237c478bd9Sstevel@tonic-gate /*
24*49335bdeSbasabi  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
28*49335bdeSbasabi /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
29*49335bdeSbasabi /*	  All Rights Reserved  	*/
30*49335bdeSbasabi 
31*49335bdeSbasabi 
327c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include <sys/types.h>
357c478bd9Sstevel@tonic-gate #include <stdio.h>
367c478bd9Sstevel@tonic-gate #include <userdefs.h>
377c478bd9Sstevel@tonic-gate #include "messages.h"
387c478bd9Sstevel@tonic-gate #include <unistd.h>
397c478bd9Sstevel@tonic-gate #include <sys/stat.h>
407c478bd9Sstevel@tonic-gate #include <utime.h>
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #define	SBUFSZ	256
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate extern int access(), rm_files();
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate static char cmdbuf[SBUFSZ];	/* buffer for system call */
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate /*
497c478bd9Sstevel@tonic-gate  *	Move directory contents from one place to another
507c478bd9Sstevel@tonic-gate  */
517c478bd9Sstevel@tonic-gate int
52*49335bdeSbasabi move_dir(char *from, char *to, char *login)
53*49335bdeSbasabi 		/* directory to move files from */
54*49335bdeSbasabi 		/* dirctory to move files to */
55*49335bdeSbasabi 		/* login id of owner */
567c478bd9Sstevel@tonic-gate {
577c478bd9Sstevel@tonic-gate 	size_t len = 0;
58*49335bdeSbasabi 	int rc = EX_SUCCESS;
597c478bd9Sstevel@tonic-gate 	struct stat statbuf;
607c478bd9Sstevel@tonic-gate 	struct utimbuf times;
617c478bd9Sstevel@tonic-gate 	/*
627c478bd9Sstevel@tonic-gate 	 * ***** THIS IS WHERE SUFFICIENT SPACE CHECK GOES
637c478bd9Sstevel@tonic-gate 	 */
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate 	if (access(from, F_OK) == 0) {	/* home dir exists */
667c478bd9Sstevel@tonic-gate 		/* move all files */
677c478bd9Sstevel@tonic-gate 		(void) sprintf(cmdbuf,
687c478bd9Sstevel@tonic-gate 			"cd %s && find . -print | cpio -m -pd %s",
697c478bd9Sstevel@tonic-gate 			from, to);
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 		if (system(cmdbuf) != 0) {
727c478bd9Sstevel@tonic-gate 			errmsg(M_NOSPACE, from, to);
737c478bd9Sstevel@tonic-gate 			return (EX_NOSPACE);
747c478bd9Sstevel@tonic-gate 		}
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 		/*
777c478bd9Sstevel@tonic-gate 		 * Check that to dir is not a subdirectory of from
787c478bd9Sstevel@tonic-gate 		 */
797c478bd9Sstevel@tonic-gate 		len = strlen(from);
807c478bd9Sstevel@tonic-gate 		if (strncmp(from, to, len) == 0 &&
817c478bd9Sstevel@tonic-gate 		    strncmp(to+len, "/", 1) == 0) {
827c478bd9Sstevel@tonic-gate 			errmsg(M_RMFILES);
837c478bd9Sstevel@tonic-gate 			return (EX_HOMEDIR);
847c478bd9Sstevel@tonic-gate 		}
857c478bd9Sstevel@tonic-gate 		/* Retain the original permission and modification time */
867c478bd9Sstevel@tonic-gate 		if (stat(from, &statbuf) == 0) {
877c478bd9Sstevel@tonic-gate 			chmod(to, statbuf.st_mode);
887c478bd9Sstevel@tonic-gate 			times.actime = statbuf.st_atime;
897c478bd9Sstevel@tonic-gate 			times.modtime = statbuf.st_mtime;
907c478bd9Sstevel@tonic-gate 			(void) utime(to, &times);
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate 		}
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 		/* Remove the files in the old place */
957c478bd9Sstevel@tonic-gate 		rc = rm_files(from, login);
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	}
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	return (rc);
1007c478bd9Sstevel@tonic-gate }
101