xref: /illumos-gate/usr/src/cmd/oamuser/user/movedir.c (revision 7fc68ddf)
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 /*
2449335bdeSbasabi  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
2849335bdeSbasabi /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
2949335bdeSbasabi /*	  All Rights Reserved  	*/
3049335bdeSbasabi 
3149335bdeSbasabi 
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate #include <stdio.h>
347c478bd9Sstevel@tonic-gate #include <userdefs.h>
357c478bd9Sstevel@tonic-gate #include "messages.h"
367c478bd9Sstevel@tonic-gate #include <unistd.h>
377c478bd9Sstevel@tonic-gate #include <sys/stat.h>
387c478bd9Sstevel@tonic-gate #include <utime.h>
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #define	SBUFSZ	256
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate extern int access(), rm_files();
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate static char cmdbuf[SBUFSZ];	/* buffer for system call */
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate /*
477c478bd9Sstevel@tonic-gate  *	Move directory contents from one place to another
487c478bd9Sstevel@tonic-gate  */
497c478bd9Sstevel@tonic-gate int
move_dir(char * from,char * to,char * login,int flags)50*7fc68ddfSAlbert Lee move_dir(char *from, char *to, char *login, int flags)
5149335bdeSbasabi 		/* directory to move files from */
5249335bdeSbasabi 		/* dirctory to move files to */
5349335bdeSbasabi 		/* login id of owner */
54*7fc68ddfSAlbert Lee 		/* miscellaneous flags */
557c478bd9Sstevel@tonic-gate {
567c478bd9Sstevel@tonic-gate 	size_t len = 0;
5749335bdeSbasabi 	int rc = EX_SUCCESS;
587c478bd9Sstevel@tonic-gate 	struct stat statbuf;
597c478bd9Sstevel@tonic-gate 	struct utimbuf times;
607c478bd9Sstevel@tonic-gate 	/*
617c478bd9Sstevel@tonic-gate 	 * ***** THIS IS WHERE SUFFICIENT SPACE CHECK GOES
627c478bd9Sstevel@tonic-gate 	 */
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate 	if (access(from, F_OK) == 0) {	/* home dir exists */
657c478bd9Sstevel@tonic-gate 		/* move all files */
667c478bd9Sstevel@tonic-gate 		(void) sprintf(cmdbuf,
677c478bd9Sstevel@tonic-gate 			"cd %s && find . -print | cpio -m -pd %s",
687c478bd9Sstevel@tonic-gate 			from, to);
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate 		if (system(cmdbuf) != 0) {
717c478bd9Sstevel@tonic-gate 			errmsg(M_NOSPACE, from, to);
727c478bd9Sstevel@tonic-gate 			return (EX_NOSPACE);
737c478bd9Sstevel@tonic-gate 		}
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 		/*
767c478bd9Sstevel@tonic-gate 		 * Check that to dir is not a subdirectory of from
777c478bd9Sstevel@tonic-gate 		 */
787c478bd9Sstevel@tonic-gate 		len = strlen(from);
797c478bd9Sstevel@tonic-gate 		if (strncmp(from, to, len) == 0 &&
807c478bd9Sstevel@tonic-gate 		    strncmp(to+len, "/", 1) == 0) {
817c478bd9Sstevel@tonic-gate 			errmsg(M_RMFILES);
827c478bd9Sstevel@tonic-gate 			return (EX_HOMEDIR);
837c478bd9Sstevel@tonic-gate 		}
847c478bd9Sstevel@tonic-gate 		/* Retain the original permission and modification time */
857c478bd9Sstevel@tonic-gate 		if (stat(from, &statbuf) == 0) {
867c478bd9Sstevel@tonic-gate 			chmod(to, statbuf.st_mode);
877c478bd9Sstevel@tonic-gate 			times.actime = statbuf.st_atime;
887c478bd9Sstevel@tonic-gate 			times.modtime = statbuf.st_mtime;
897c478bd9Sstevel@tonic-gate 			(void) utime(to, &times);
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 		}
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 		/* Remove the files in the old place */
94*7fc68ddfSAlbert Lee 		rc = rm_files(from, login, flags);
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	}
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	return (rc);
997c478bd9Sstevel@tonic-gate }
100