xref: /minix3/minix/commands/backup/backup.c (revision 433d6423c39e34ec4b79c950597bb2d236f886be)
1*433d6423SLionel Sambuc /* backup - backup a directory		Author: Andy Tanenbaum */
2*433d6423SLionel Sambuc 
3*433d6423SLionel Sambuc /* This program recursively backs up a directory.  It has two typical uses:
4*433d6423SLionel Sambuc  *
5*433d6423SLionel Sambuc  * 1. Backing up a directory to 1 or more diskettes
6*433d6423SLionel Sambuc  * 2. Backing up RAM disk to a shadow directory on hard disk
7*433d6423SLionel Sambuc  *
8*433d6423SLionel Sambuc  * The backup directory or medium may be empty, in which case, the entire
9*433d6423SLionel Sambuc  * source directory is copied, or it may contain an old backup, in which
10*433d6423SLionel Sambuc  * case only those files that are new or out of date are copied.  In this
11*433d6423SLionel Sambuc  * respect, 'backup' resembles 'make', except that no 'makefile' is needed.
12*433d6423SLionel Sambuc  * The backed up copy may optionally be compressed to save space.
13*433d6423SLionel Sambuc  *
14*433d6423SLionel Sambuc  * The following flags exist:
15*433d6423SLionel Sambuc  *
16*433d6423SLionel Sambuc  *	-d  At the top level, only back up directories (not loose files)
17*433d6423SLionel Sambuc  *	-j  Don't copy junk: *.Z, *.bak, *.log, a.out, and core
18*433d6423SLionel Sambuc  *	-m  If ENOSPC encountered, ask for another diskette
19*433d6423SLionel Sambuc  *	-n  No directories, only loose files are backed up
20*433d6423SLionel Sambuc  *	-o  Don't copy *.o files
21*433d6423SLionel Sambuc  *	-r  Restore files (ie. uncompress if necessary)
22*433d6423SLionel Sambuc  *	-s  Don't copy *.s files
23*433d6423SLionel Sambuc  *	-t  Set creation date of target-file equal to cdate of source-file
24*433d6423SLionel Sambuc  *	-v  Verbose (announce what is being done)
25*433d6423SLionel Sambuc  *	-z  Compress on backup/uncompress on restore
26*433d6423SLionel Sambuc  *
27*433d6423SLionel Sambuc  * Patches:
28*433d6423SLionel Sambuc  *	30 Mar 91.	Added restore option.  cwr.
29*433d6423SLionel Sambuc  *	 9 Sep 91.	Changed user interface.  cwr.
30*433d6423SLionel Sambuc  *	21 Jan 93.	Revised error messages.  cwr.
31*433d6423SLionel Sambuc  *	29 Mar 95.	Added -o, NARROW define.  cwr.
32*433d6423SLionel Sambuc  *      16 Nov 2013.    Changed the functions to ANSI style.
33*433d6423SLionel Sambuc  *			Author: Alexandre Beletti (rhiguita@gmail.com)
34*433d6423SLionel Sambuc  */
35*433d6423SLionel Sambuc 
36*433d6423SLionel Sambuc #include <sys/types.h>
37*433d6423SLionel Sambuc #include <sys/stat.h>
38*433d6423SLionel Sambuc #include <sys/syslimits.h>
39*433d6423SLionel Sambuc #include <errno.h>
40*433d6423SLionel Sambuc #include <fcntl.h>
41*433d6423SLionel Sambuc #include <utime.h>
42*433d6423SLionel Sambuc #include <stdlib.h>
43*433d6423SLionel Sambuc #include <string.h>
44*433d6423SLionel Sambuc #include <unistd.h>
45*433d6423SLionel Sambuc #include <sys/wait.h>
46*433d6423SLionel Sambuc #include <stdio.h>
47*433d6423SLionel Sambuc #include <dirent.h>
48*433d6423SLionel Sambuc 
49*433d6423SLionel Sambuc #define NAME_SIZE NAME_MAX
50*433d6423SLionel Sambuc 
51*433d6423SLionel Sambuc #undef NARROW			/* Width of verbose output */
52*433d6423SLionel Sambuc #define COPY_SIZE 4096
53*433d6423SLionel Sambuc #define MAX_ENTRIES 512
54*433d6423SLionel Sambuc #define MAX_PATH 256
55*433d6423SLionel Sambuc #define NONFATAL 0
56*433d6423SLionel Sambuc #define FATAL 1
57*433d6423SLionel Sambuc #define NO_SAVINGS 512		/* compress can return code 2 */
58*433d6423SLionel Sambuc #define OUT_OF_SPACE 2
59*433d6423SLionel Sambuc 
60*433d6423SLionel Sambuc struct dirent_full {
61*433d6423SLionel Sambuc 	struct dirent de;
62*433d6423SLionel Sambuc 	char d_name[MAXNAMLEN+1];
63*433d6423SLionel Sambuc } dir_ent[MAX_ENTRIES];
64*433d6423SLionel Sambuc int entries = 0;
65*433d6423SLionel Sambuc 
66*433d6423SLionel Sambuc struct sorted {
67*433d6423SLionel Sambuc   int mode;			/* file mode */
68*433d6423SLionel Sambuc   char *namep;			/* pointer to name in dir_buf */
69*433d6423SLionel Sambuc   long acctime;			/* time of last access */
70*433d6423SLionel Sambuc   long modtime;			/* time of last modification */
71*433d6423SLionel Sambuc } sorted[MAX_ENTRIES];
72*433d6423SLionel Sambuc 
73*433d6423SLionel Sambuc char copybuf[COPY_SIZE];
74*433d6423SLionel Sambuc char *pname;
75*433d6423SLionel Sambuc int dflag, jflag, mflag, nflag, oflag, rflag, sflag, tflag, vflag, zflag;
76*433d6423SLionel Sambuc 
77*433d6423SLionel Sambuc extern int errno;
78*433d6423SLionel Sambuc extern char **environ;
79*433d6423SLionel Sambuc 
80*433d6423SLionel Sambuc int main(int argc, char *argv[]);
81*433d6423SLionel Sambuc void maketarget(char *dir2);
82*433d6423SLionel Sambuc int make_dir(char *dir);
83*433d6423SLionel Sambuc int stat_all(char *dir1, int n);
84*433d6423SLionel Sambuc void sort_dir(int m);
85*433d6423SLionel Sambuc void process(int m, char *dir1, char *dir2);
86*433d6423SLionel Sambuc void swap(struct sorted *sp1, struct sorted *sp2);
87*433d6423SLionel Sambuc int copy(char *dir1, struct sorted *sp, char *cbuf2);
88*433d6423SLionel Sambuc int zcopy(char *src, char *targ);
89*433d6423SLionel Sambuc void copydir(char *dir1, char *dir2, char *namep);
90*433d6423SLionel Sambuc void newdisk(char *dir);
91*433d6423SLionel Sambuc void usage(void);
92*433d6423SLionel Sambuc void error(int type, char *s1, char *s2, char *s3);
93*433d6423SLionel Sambuc 
main(int argc,char * argv[])94*433d6423SLionel Sambuc int main(int argc, char *argv[])
95*433d6423SLionel Sambuc {
96*433d6423SLionel Sambuc   int n, m;
97*433d6423SLionel Sambuc   char *dir1, *dir2, *cp, c;
98*433d6423SLionel Sambuc   struct stat s;
99*433d6423SLionel Sambuc   struct dirent *e;
100*433d6423SLionel Sambuc   DIR *DIR1;
101*433d6423SLionel Sambuc 
102*433d6423SLionel Sambuc   (void) sync();
103*433d6423SLionel Sambuc 
104*433d6423SLionel Sambuc   /* Get the flags */
105*433d6423SLionel Sambuc   if ((pname = strrchr(argv[0], '/')) == (char *)NULL)
106*433d6423SLionel Sambuc 	pname = argv[0];
107*433d6423SLionel Sambuc   else
108*433d6423SLionel Sambuc 	pname++;
109*433d6423SLionel Sambuc   if (argc < 3 || argc > 4) usage();
110*433d6423SLionel Sambuc   if (argc == 4) {
111*433d6423SLionel Sambuc 	cp = argv[1];
112*433d6423SLionel Sambuc 	if (*cp++ != '-') usage();
113*433d6423SLionel Sambuc 	while ((c = *cp++) != '\0') {
114*433d6423SLionel Sambuc 		switch (c) {
115*433d6423SLionel Sambuc 		    case 'd':	dflag++;	break;
116*433d6423SLionel Sambuc 		    case 'j':	jflag++;	break;
117*433d6423SLionel Sambuc 		    case 'm':	mflag++;	break;
118*433d6423SLionel Sambuc 		    case 'n':	nflag++;	break;
119*433d6423SLionel Sambuc 		    case 'o':	oflag++;	break;
120*433d6423SLionel Sambuc 		    case 's':	sflag++;	break;
121*433d6423SLionel Sambuc 		    case 'r':	rflag++;	break;
122*433d6423SLionel Sambuc 		    case 't':	tflag++;	break;
123*433d6423SLionel Sambuc 		    case 'v':	vflag++;	break;
124*433d6423SLionel Sambuc 		    case 'z':	zflag++;	break;
125*433d6423SLionel Sambuc 		    default:	usage();
126*433d6423SLionel Sambuc 		}
127*433d6423SLionel Sambuc 	}
128*433d6423SLionel Sambuc 	dir1 = argv[2];
129*433d6423SLionel Sambuc 	dir2 = argv[3];
130*433d6423SLionel Sambuc   } else {
131*433d6423SLionel Sambuc 	dir1 = argv[1];
132*433d6423SLionel Sambuc 	dir2 = argv[2];
133*433d6423SLionel Sambuc   }
134*433d6423SLionel Sambuc   if (!strcmp(pname, "restore") && !rflag) rflag++;
135*433d6423SLionel Sambuc 
136*433d6423SLionel Sambuc   /* Check for a valid source */
137*433d6423SLionel Sambuc   if (stat(dir1, &s) < 0) error(FATAL, "cannot stat ", dir1, "");
138*433d6423SLionel Sambuc   if ((s.st_mode & S_IFMT) != S_IFDIR) error(FATAL, "non-directory ", dir1, "");
139*433d6423SLionel Sambuc 
140*433d6423SLionel Sambuc   /* Read in the source directory */
141*433d6423SLionel Sambuc   if(!(DIR1 = opendir(dir1))) {
142*433d6423SLionel Sambuc   	perror(dir1);
143*433d6423SLionel Sambuc   	return 1;
144*433d6423SLionel Sambuc   }
145*433d6423SLionel Sambuc   for(entries = 0; entries < MAX_ENTRIES && (e=readdir(DIR1)); entries++) {
146*433d6423SLionel Sambuc 	memcpy(&dir_ent[entries].de, e, sizeof(*e));
147*433d6423SLionel Sambuc 	snprintf(dir_ent[entries].d_name, MAXNAMLEN, "%s", e->d_name);
148*433d6423SLionel Sambuc   }
149*433d6423SLionel Sambuc   closedir(DIR1);
150*433d6423SLionel Sambuc   if (entries == MAX_ENTRIES)
151*433d6423SLionel Sambuc 	error(FATAL, "directory ", dir1, " is too large");
152*433d6423SLionel Sambuc 
153*433d6423SLionel Sambuc   /* Create the target directory. */
154*433d6423SLionel Sambuc   maketarget(dir2);
155*433d6423SLionel Sambuc 
156*433d6423SLionel Sambuc   /* Stat all the entries. */
157*433d6423SLionel Sambuc   n = entries;
158*433d6423SLionel Sambuc   m = stat_all(dir1, n);
159*433d6423SLionel Sambuc 
160*433d6423SLionel Sambuc   /* Remove non-entries and sort what's left. */
161*433d6423SLionel Sambuc   sort_dir(m);
162*433d6423SLionel Sambuc 
163*433d6423SLionel Sambuc   /* Process each of the m entries one at a time. */
164*433d6423SLionel Sambuc   process(m, dir1, dir2);
165*433d6423SLionel Sambuc   return(0);
166*433d6423SLionel Sambuc }
167*433d6423SLionel Sambuc 
168*433d6423SLionel Sambuc 
maketarget(char * dir2)169*433d6423SLionel Sambuc void maketarget(char *dir2)
170*433d6423SLionel Sambuc {
171*433d6423SLionel Sambuc /* The target directory is created if it does not already exist. */
172*433d6423SLionel Sambuc 
173*433d6423SLionel Sambuc   char *p, c, dbuf[MAX_PATH];
174*433d6423SLionel Sambuc 
175*433d6423SLionel Sambuc   if (access(dir2, 6) == 0)
176*433d6423SLionel Sambuc 	return;			/* if target exists, we're done */
177*433d6423SLionel Sambuc   if (make_dir(dir2) == 0) return;	/* we just made it */
178*433d6423SLionel Sambuc 
179*433d6423SLionel Sambuc   /* We have to try creating all the higher level directories. */
180*433d6423SLionel Sambuc   strcpy(dbuf, dir2);
181*433d6423SLionel Sambuc   p = dbuf + 1;
182*433d6423SLionel Sambuc   while (1) {
183*433d6423SLionel Sambuc 	while (*p != '/' && *p != '\0') p++;
184*433d6423SLionel Sambuc 	c = *p;			/* either / or \0 */
185*433d6423SLionel Sambuc 	*p = 0;
186*433d6423SLionel Sambuc 	make_dir(dbuf);
187*433d6423SLionel Sambuc 	if (c == '\0') return;
188*433d6423SLionel Sambuc 	*p = c;
189*433d6423SLionel Sambuc 	p++;
190*433d6423SLionel Sambuc   }
191*433d6423SLionel Sambuc }
192*433d6423SLionel Sambuc 
make_dir(char * dir)193*433d6423SLionel Sambuc int make_dir(char *dir)
194*433d6423SLionel Sambuc {
195*433d6423SLionel Sambuc /* Create a directory. */
196*433d6423SLionel Sambuc   int pid, status;
197*433d6423SLionel Sambuc 
198*433d6423SLionel Sambuc   if ((pid = fork()) < 0)
199*433d6423SLionel Sambuc 	error(FATAL, "cannot fork off mkdir to create ", dir, "");
200*433d6423SLionel Sambuc   if (pid > 0) {
201*433d6423SLionel Sambuc 	/* Parent process waits for child (mkdir). */
202*433d6423SLionel Sambuc 	wait(&status);
203*433d6423SLionel Sambuc 	return(status);
204*433d6423SLionel Sambuc   } else {
205*433d6423SLionel Sambuc 	/* Child process executes mkdir */
206*433d6423SLionel Sambuc 	close(2);		/* don't want mkdir's error messages */
207*433d6423SLionel Sambuc 	execle("/bin/mkdir", "mkdir", dir, (char *) 0, environ);
208*433d6423SLionel Sambuc 	execle("/usr/bin/mkdir", "mkdir", dir, (char *) 0, environ);
209*433d6423SLionel Sambuc 	error(FATAL, "cannot execute mkdir", "", "");
210*433d6423SLionel Sambuc   }
211*433d6423SLionel Sambuc   return(0);
212*433d6423SLionel Sambuc }
213*433d6423SLionel Sambuc 
214*433d6423SLionel Sambuc 
stat_all(char * dir1,int n)215*433d6423SLionel Sambuc int stat_all(char *dir1, int n)
216*433d6423SLionel Sambuc {
217*433d6423SLionel Sambuc /* Stat all the directory entries.  By doing this all at once, the disk
218*433d6423SLionel Sambuc  * head can stay in the inode area.
219*433d6423SLionel Sambuc  */
220*433d6423SLionel Sambuc 
221*433d6423SLionel Sambuc   int i, j;
222*433d6423SLionel Sambuc   char cbuf[MAX_PATH];
223*433d6423SLionel Sambuc   struct stat s;
224*433d6423SLionel Sambuc 
225*433d6423SLionel Sambuc   for (i = 0; i < n; i++) {
226*433d6423SLionel Sambuc 	/* Mark "." and ".." as null entries, as well as unstatable ones. */
227*433d6423SLionel Sambuc 	if (strcmp(dir_ent[i].d_name, ".") == 0) dir_ent[i].de.d_ino = 0;
228*433d6423SLionel Sambuc 	if (strcmp(dir_ent[i].d_name, "..") == 0) dir_ent[i].de.d_ino = 0;
229*433d6423SLionel Sambuc 	if (dir_ent[i].de.d_ino == 0) continue;
230*433d6423SLionel Sambuc 
231*433d6423SLionel Sambuc 	/* Stat the file. */
232*433d6423SLionel Sambuc 	snprintf(cbuf, sizeof(cbuf), "%s/%s", dir1, dir_ent[i].d_name);
233*433d6423SLionel Sambuc 	if (stat(cbuf, &s) < 0) {
234*433d6423SLionel Sambuc 		error(NONFATAL, "cannot stat ", cbuf, "");
235*433d6423SLionel Sambuc 		dir_ent[i].de.d_ino = 0;	/* mark as unusable */
236*433d6423SLionel Sambuc 		continue;
237*433d6423SLionel Sambuc 	}
238*433d6423SLionel Sambuc 	sorted[i].mode = s.st_mode;
239*433d6423SLionel Sambuc 	sorted[i].acctime = s.st_atime;
240*433d6423SLionel Sambuc 	sorted[i].modtime = s.st_mtime;
241*433d6423SLionel Sambuc 	sorted[i].namep = dir_ent[i].d_name;
242*433d6423SLionel Sambuc 	sorted[i].namep[NAME_SIZE-1] = '\0';
243*433d6423SLionel Sambuc   }
244*433d6423SLionel Sambuc 
245*433d6423SLionel Sambuc   /* Squeeze out all the entries whose ino field is 0. */
246*433d6423SLionel Sambuc   j = 0;
247*433d6423SLionel Sambuc   for (i = 0; i < n; i++) {
248*433d6423SLionel Sambuc 	if (dir_ent[i].de.d_ino != 0) {
249*433d6423SLionel Sambuc 		sorted[j] = sorted[i];
250*433d6423SLionel Sambuc 		j++;
251*433d6423SLionel Sambuc 	}
252*433d6423SLionel Sambuc   }
253*433d6423SLionel Sambuc   return(j);
254*433d6423SLionel Sambuc }
255*433d6423SLionel Sambuc 
256*433d6423SLionel Sambuc 
sort_dir(int m)257*433d6423SLionel Sambuc void sort_dir(int m)
258*433d6423SLionel Sambuc {
259*433d6423SLionel Sambuc /* Sort the directory using bubble sort. */
260*433d6423SLionel Sambuc 
261*433d6423SLionel Sambuc   struct sorted *sp1, *sp2;
262*433d6423SLionel Sambuc 
263*433d6423SLionel Sambuc   for (sp1 = &sorted[0]; sp1 < &sorted[m - 1]; sp1++) {
264*433d6423SLionel Sambuc 	for (sp2 = sp1 + 1; sp2 < &sorted[m]; sp2++) {
265*433d6423SLionel Sambuc 		if (strcmp(sp1->namep, sp2->namep) > 0)
266*433d6423SLionel Sambuc 			swap(sp1, sp2);
267*433d6423SLionel Sambuc 	}
268*433d6423SLionel Sambuc   }
269*433d6423SLionel Sambuc }
270*433d6423SLionel Sambuc 
271*433d6423SLionel Sambuc 
process(int m,char * dir1,char * dir2)272*433d6423SLionel Sambuc void process(int m, char *dir1, char *dir2)
273*433d6423SLionel Sambuc {
274*433d6423SLionel Sambuc /* Process each entry in sorted[].  If it is a regular file, stat the target
275*433d6423SLionel Sambuc  * file.  The the source is newer, copy it.  If the entry is a directory,
276*433d6423SLionel Sambuc  * recursively call the entire program to process the directory.
277*433d6423SLionel Sambuc  */
278*433d6423SLionel Sambuc 
279*433d6423SLionel Sambuc   int er, fmode, res;
280*433d6423SLionel Sambuc   struct sorted *sp;
281*433d6423SLionel Sambuc   struct stat s;
282*433d6423SLionel Sambuc   char cbuf[MAX_PATH];
283*433d6423SLionel Sambuc 
284*433d6423SLionel Sambuc   for (sp = &sorted[0]; sp < &sorted[m]; sp++) {
285*433d6423SLionel Sambuc   	int namlen;
286*433d6423SLionel Sambuc 	fmode = sp->mode & S_IFMT;
287*433d6423SLionel Sambuc 	if (fmode == S_IFREG) {
288*433d6423SLionel Sambuc 		/* Regular file.  Construct target name and stat it. */
289*433d6423SLionel Sambuc 		snprintf(cbuf, sizeof(cbuf), "%s/%s", dir2, sp->namep);
290*433d6423SLionel Sambuc 		namlen = strlen(sp->namep);
291*433d6423SLionel Sambuc 		/* Switch between compressed and uncompressed file names */
292*433d6423SLionel Sambuc 		if (zflag && !rflag && strncmp((sp->namep + namlen - 2), ".Z", (size_t)2)
293*433d6423SLionel Sambuc 				&& (namlen <= (NAME_SIZE - 2)))
294*433d6423SLionel Sambuc 			strncat(cbuf, ".Z", (size_t)2);
295*433d6423SLionel Sambuc 		if (zflag && rflag && !strncmp((sp->namep + namlen - 2), ".Z", (size_t)2))
296*433d6423SLionel Sambuc 			cbuf[strlen(cbuf) - 2] = '\0';
297*433d6423SLionel Sambuc 		er = stat(cbuf, &s);
298*433d6423SLionel Sambuc 		if (er < 0 || sp->modtime > s.st_mtime) {
299*433d6423SLionel Sambuc 			res = copy(dir1, sp, cbuf);
300*433d6423SLionel Sambuc 		} else {
301*433d6423SLionel Sambuc 			res = NONFATAL;
302*433d6423SLionel Sambuc 		}
303*433d6423SLionel Sambuc 
304*433d6423SLionel Sambuc 		/* Check status of the copy. */
305*433d6423SLionel Sambuc 		if (res == OUT_OF_SPACE) {
306*433d6423SLionel Sambuc 			printf("Out of space while copying to %s\n", cbuf);
307*433d6423SLionel Sambuc 			/* We ran out of space copying a regular file. */
308*433d6423SLionel Sambuc 			if (mflag == 0)
309*433d6423SLionel Sambuc 				error(FATAL, "Quitting, disk full", "", "");
310*433d6423SLionel Sambuc 
311*433d6423SLionel Sambuc 			/* If -m, ask for new diskette and continue. */
312*433d6423SLionel Sambuc 			newdisk(dir2);
313*433d6423SLionel Sambuc 			sp--;
314*433d6423SLionel Sambuc 			continue;
315*433d6423SLionel Sambuc 		}
316*433d6423SLionel Sambuc 	} else if (fmode == S_IFDIR) {
317*433d6423SLionel Sambuc 		/* Directory.  Execute this program recursively. */
318*433d6423SLionel Sambuc 		copydir(dir1, dir2, sp->namep);
319*433d6423SLionel Sambuc 	} else if (fmode == S_IFBLK || fmode == S_IFCHR) {
320*433d6423SLionel Sambuc 		/* Special file. */
321*433d6423SLionel Sambuc 		strncpy(cbuf, sp->namep, sizeof(cbuf));
322*433d6423SLionel Sambuc 		printf("%s is special file.  Not backed up.\n", cbuf);
323*433d6423SLionel Sambuc 	}
324*433d6423SLionel Sambuc   }
325*433d6423SLionel Sambuc }
326*433d6423SLionel Sambuc 
327*433d6423SLionel Sambuc 
328*433d6423SLionel Sambuc 
329*433d6423SLionel Sambuc 
swap(struct sorted * sp1,struct sorted * sp2)330*433d6423SLionel Sambuc void swap(struct sorted *sp1, struct sorted *sp2)
331*433d6423SLionel Sambuc {
332*433d6423SLionel Sambuc /* Swap two directory entries. */
333*433d6423SLionel Sambuc 
334*433d6423SLionel Sambuc   struct sorted d;
335*433d6423SLionel Sambuc 
336*433d6423SLionel Sambuc   d = *sp1;
337*433d6423SLionel Sambuc   *sp1 = *sp2;
338*433d6423SLionel Sambuc   *sp2 = d;
339*433d6423SLionel Sambuc }
340*433d6423SLionel Sambuc 
341*433d6423SLionel Sambuc 
copy(char * dir1,struct sorted * sp,char * cbuf2)342*433d6423SLionel Sambuc int copy(char *dir1, struct sorted *sp, char *cbuf2)
343*433d6423SLionel Sambuc {
344*433d6423SLionel Sambuc /* Copy a regular file. */
345*433d6423SLionel Sambuc 
346*433d6423SLionel Sambuc   int fd1, fd2, nr, nw, res, n;
347*433d6423SLionel Sambuc   char cbuf1[MAX_PATH], *p;
348*433d6423SLionel Sambuc #ifdef NARROW
349*433d6423SLionel Sambuc   char *msg = (rflag || strcmp(pname, "backup")) ? "Restored" : "Backing up";
350*433d6423SLionel Sambuc #endif
351*433d6423SLionel Sambuc 
352*433d6423SLionel Sambuc   /* If the -j or -o or -s flags were given, suppress certain files. */
353*433d6423SLionel Sambuc   p = sp->namep;
354*433d6423SLionel Sambuc   n = strlen(p);
355*433d6423SLionel Sambuc   if (n > NAME_SIZE) n = NAME_SIZE;
356*433d6423SLionel Sambuc   if (jflag) {
357*433d6423SLionel Sambuc 	if (strcmp(p, "a.out") == 0) return(0);
358*433d6423SLionel Sambuc 	if (strcmp(p, "core") == 0) return (0);
359*433d6423SLionel Sambuc 	if (strcmp(p + n - 2, ".Z") == 0) return (0);
360*433d6423SLionel Sambuc 	if (strcmp(p + n - 4, ".bak") == 0) return (0);
361*433d6423SLionel Sambuc 	if (strcmp(p + n - 4, ".log") == 0) return (0);
362*433d6423SLionel Sambuc   }
363*433d6423SLionel Sambuc   if (oflag) {
364*433d6423SLionel Sambuc 	if (strcmp(p + n - 2, ".o") == 0) return(0);
365*433d6423SLionel Sambuc   }
366*433d6423SLionel Sambuc   if (sflag) {
367*433d6423SLionel Sambuc 	if (strcmp(p + n - 2, ".s") == 0) return(0);
368*433d6423SLionel Sambuc   }
369*433d6423SLionel Sambuc   res = 0;
370*433d6423SLionel Sambuc   if (dflag) return(0);		/* backup -d means only directories */
371*433d6423SLionel Sambuc   strcpy(cbuf1, dir1);
372*433d6423SLionel Sambuc   strncat(cbuf1, "/", (size_t)1);
373*433d6423SLionel Sambuc   strncat(cbuf1, sp->namep, (size_t)NAME_SIZE);	/* cbuf1 = source file name */
374*433d6423SLionel Sambuc 
375*433d6423SLionel Sambuc   /* At this point, cbuf1 contains the source file name, cbuf2 the target. */
376*433d6423SLionel Sambuc   fd1 = open(cbuf1, O_RDONLY);
377*433d6423SLionel Sambuc   if (fd1 < 0) {
378*433d6423SLionel Sambuc 	error(NONFATAL, "cannot open ", cbuf1, "");
379*433d6423SLionel Sambuc 	return(res);
380*433d6423SLionel Sambuc   }
381*433d6423SLionel Sambuc   fd2 = creat(cbuf2, (sp->mode | S_IWUSR) & 07777);
382*433d6423SLionel Sambuc   if (fd2 < 0) {
383*433d6423SLionel Sambuc 	if (errno == ENFILE) {
384*433d6423SLionel Sambuc 		close(fd1);
385*433d6423SLionel Sambuc 		return(OUT_OF_SPACE);
386*433d6423SLionel Sambuc 	}
387*433d6423SLionel Sambuc 	error(NONFATAL, "cannot create ", cbuf2, "");
388*433d6423SLionel Sambuc 	close(fd1);
389*433d6423SLionel Sambuc 	return(res);
390*433d6423SLionel Sambuc   }
391*433d6423SLionel Sambuc 
392*433d6423SLionel Sambuc   /* Both files are now open.  Do the copying. */
393*433d6423SLionel Sambuc   if ((!rflag && strncmp((sp->namep + n - 2), ".Z", (size_t)2)) ||
394*433d6423SLionel Sambuc 		(rflag && !strncmp((sp->namep + n - 2), ".Z", (size_t)2))) {
395*433d6423SLionel Sambuc 	if (zflag && (rflag || (n <= (NAME_SIZE - 2)))) {
396*433d6423SLionel Sambuc 		close(fd1);
397*433d6423SLionel Sambuc 		close(fd2);
398*433d6423SLionel Sambuc 		res = zcopy(cbuf1, cbuf2);
399*433d6423SLionel Sambuc 		if (tflag) utime(cbuf2, (struct utimbuf *) & (sp->acctime));
400*433d6423SLionel Sambuc 		if (res != 0) unlink(cbuf2); /* if error, get rid of the corpse */
401*433d6423SLionel Sambuc #ifdef NARROW
402*433d6423SLionel Sambuc 		if (vflag && res == 0) printf("%s %s\n", msg, cbuf1);
403*433d6423SLionel Sambuc #else
404*433d6423SLionel Sambuc 		if (vflag && res == 0) {
405*433d6423SLionel Sambuc 			printf("%-37.37s -> %-37.37s\n", cbuf1, cbuf2);
406*433d6423SLionel Sambuc 			if (strlen(cbuf1) > 37 || strlen(cbuf2) > 37)
407*433d6423SLionel Sambuc 				printf("%37.37s    %37.37s\n",
408*433d6423SLionel Sambuc 				(strlen(cbuf1) > 37) ? (cbuf1 + 37) : "",
409*433d6423SLionel Sambuc 				(strlen(cbuf2) > 37) ? (cbuf2 + 37) : "");
410*433d6423SLionel Sambuc 		}
411*433d6423SLionel Sambuc #endif
412*433d6423SLionel Sambuc 		return(res);
413*433d6423SLionel Sambuc 	}
414*433d6423SLionel Sambuc   }
415*433d6423SLionel Sambuc   while (1) {
416*433d6423SLionel Sambuc 	nr = read(fd1, copybuf, COPY_SIZE);
417*433d6423SLionel Sambuc 	if (nr == 0) break;
418*433d6423SLionel Sambuc 	if (nr < 0) {
419*433d6423SLionel Sambuc 		error(NONFATAL, "read error on ", cbuf1, "");
420*433d6423SLionel Sambuc 		res = EIO;
421*433d6423SLionel Sambuc 		break;
422*433d6423SLionel Sambuc 	}
423*433d6423SLionel Sambuc 	nw = write(fd2, copybuf, nr);
424*433d6423SLionel Sambuc 	if (nw < 0) {
425*433d6423SLionel Sambuc 		if (errno == ENOSPC) {
426*433d6423SLionel Sambuc 			/* Device is full. */
427*433d6423SLionel Sambuc 			res = OUT_OF_SPACE;
428*433d6423SLionel Sambuc 			break;
429*433d6423SLionel Sambuc 		}
430*433d6423SLionel Sambuc 
431*433d6423SLionel Sambuc 		/* True write error. */
432*433d6423SLionel Sambuc 		error(NONFATAL, "write error on ", cbuf2, "");
433*433d6423SLionel Sambuc 		res = EIO;
434*433d6423SLionel Sambuc 		break;
435*433d6423SLionel Sambuc 	}
436*433d6423SLionel Sambuc   }
437*433d6423SLionel Sambuc   if (res == 0) {
438*433d6423SLionel Sambuc #ifdef NARROW
439*433d6423SLionel Sambuc  	if (vflag) printf("%s %s\n", msg, cbuf1);
440*433d6423SLionel Sambuc #else
441*433d6423SLionel Sambuc 	if (vflag) {
442*433d6423SLionel Sambuc 		printf("%-37.37s -> %-37.37s\n", cbuf1, cbuf2);
443*433d6423SLionel Sambuc 		if (strlen(cbuf1) > 37 || strlen(cbuf2) > 37)
444*433d6423SLionel Sambuc 			printf("%37.37s    %37.37s\n",
445*433d6423SLionel Sambuc 			(strlen(cbuf1) > 37) ? (cbuf1 + 37) : "",
446*433d6423SLionel Sambuc 			(strlen(cbuf2) > 37) ? (cbuf2 + 37) : "");
447*433d6423SLionel Sambuc 	}
448*433d6423SLionel Sambuc #endif
449*433d6423SLionel Sambuc   } else {
450*433d6423SLionel Sambuc 	unlink(cbuf2);
451*433d6423SLionel Sambuc   }
452*433d6423SLionel Sambuc   close(fd1);
453*433d6423SLionel Sambuc   close(fd2);
454*433d6423SLionel Sambuc   if (tflag) utime(cbuf2, (struct utimbuf *) & (sp->acctime));
455*433d6423SLionel Sambuc   return(res);
456*433d6423SLionel Sambuc }
457*433d6423SLionel Sambuc 
458*433d6423SLionel Sambuc 
zcopy(char * src,char * targ)459*433d6423SLionel Sambuc int zcopy(char *src, char *targ)
460*433d6423SLionel Sambuc {
461*433d6423SLionel Sambuc 
462*433d6423SLionel Sambuc   int pid, status, res, s;
463*433d6423SLionel Sambuc   char fbuf[20];
464*433d6423SLionel Sambuc 
465*433d6423SLionel Sambuc   /* These flags go for compress and gzip. */
466*433d6423SLionel Sambuc   strcpy(fbuf, "-c");
467*433d6423SLionel Sambuc   if (rflag)
468*433d6423SLionel Sambuc 	strcat(fbuf, "d");
469*433d6423SLionel Sambuc   else
470*433d6423SLionel Sambuc 	strcat(fbuf, "f");
471*433d6423SLionel Sambuc 
472*433d6423SLionel Sambuc   if ((pid = fork()) < 0) error(FATAL, "cannot fork", "", "");
473*433d6423SLionel Sambuc   if (pid > 0) {
474*433d6423SLionel Sambuc 	wait(&status);
475*433d6423SLionel Sambuc 
476*433d6423SLionel Sambuc 	/* Error codes 0 and 2 are ok, assume others mean disk is full. */
477*433d6423SLionel Sambuc 	res = (status == 0 || status == NO_SAVINGS ? 0 : OUT_OF_SPACE);
478*433d6423SLionel Sambuc 	return(res);
479*433d6423SLionel Sambuc   } else {
480*433d6423SLionel Sambuc 	/* Child must execute compress. */
481*433d6423SLionel Sambuc 	close(1);
482*433d6423SLionel Sambuc 	s = open(targ, O_RDWR);
483*433d6423SLionel Sambuc 	if (s < 0) error(FATAL, "cannot write on ", "targ", "");
484*433d6423SLionel Sambuc 	execle("/usr/bin/gzip", "gzip", fbuf, src, (char *)0, environ);
485*433d6423SLionel Sambuc 	execle("/usr/local/bin/gzip", "gzip", fbuf, src, (char *)0, environ);
486*433d6423SLionel Sambuc 	execle("/bin/compress", "compress", fbuf, src, (char *)0, environ);
487*433d6423SLionel Sambuc 	execle("/usr/bin/compress", "compress", fbuf, src, (char *)0, environ);
488*433d6423SLionel Sambuc 	error(FATAL, "cannot exec gzip or compress", "", "");
489*433d6423SLionel Sambuc   }
490*433d6423SLionel Sambuc   return(0);
491*433d6423SLionel Sambuc }
492*433d6423SLionel Sambuc 
493*433d6423SLionel Sambuc 
copydir(char * dir1,char * dir2,char * namep)494*433d6423SLionel Sambuc void copydir(char *dir1, char *dir2, char *namep)
495*433d6423SLionel Sambuc {
496*433d6423SLionel Sambuc /* Copy a directory. */
497*433d6423SLionel Sambuc 
498*433d6423SLionel Sambuc   int pid, status;
499*433d6423SLionel Sambuc   char fbuf[20], d1buf[MAX_PATH], d2buf[MAX_PATH];
500*433d6423SLionel Sambuc 
501*433d6423SLionel Sambuc   if (nflag) return;	/* backup -n means no directories */
502*433d6423SLionel Sambuc 
503*433d6423SLionel Sambuc   fbuf[0] = '\0';
504*433d6423SLionel Sambuc 
505*433d6423SLionel Sambuc   /* Handle directory copy by forking off 'backup' ! */
506*433d6423SLionel Sambuc   if (jflag || mflag || oflag || rflag || sflag || tflag || vflag || zflag)
507*433d6423SLionel Sambuc 	strcpy(fbuf, "-");
508*433d6423SLionel Sambuc   if (jflag) strcat(fbuf, "j");
509*433d6423SLionel Sambuc   if (mflag) strcat(fbuf, "m");
510*433d6423SLionel Sambuc   if (oflag) strcat(fbuf, "o");
511*433d6423SLionel Sambuc   if (rflag) strcat(fbuf, "r");
512*433d6423SLionel Sambuc   if (sflag) strcat(fbuf, "s");
513*433d6423SLionel Sambuc   if (tflag) strcat(fbuf, "t");
514*433d6423SLionel Sambuc   if (vflag) strcat(fbuf, "v");
515*433d6423SLionel Sambuc   if (zflag) strcat(fbuf, "z");
516*433d6423SLionel Sambuc   snprintf(d1buf, sizeof(d1buf), "%s/%s", dir1, namep);
517*433d6423SLionel Sambuc   snprintf(d2buf, sizeof(d2buf), "%s/%s", dir2, namep);
518*433d6423SLionel Sambuc 
519*433d6423SLionel Sambuc   if ((pid = fork()) < 0) error(FATAL, "cannot fork", "", "");
520*433d6423SLionel Sambuc   if (pid > 0) {
521*433d6423SLionel Sambuc 	/* Parent waits for child, then returns. */
522*433d6423SLionel Sambuc 	wait(&status);
523*433d6423SLionel Sambuc 	return;
524*433d6423SLionel Sambuc   }
525*433d6423SLionel Sambuc 
526*433d6423SLionel Sambuc   if (fbuf[0] == '-') {
527*433d6423SLionel Sambuc 	execle(pname, pname, fbuf, d1buf, d2buf, (char *) 0, environ);
528*433d6423SLionel Sambuc 	execle("/bin/backup", "backup", fbuf, d1buf, d2buf, (char *)0,environ);
529*433d6423SLionel Sambuc 	execle("/usr/bin/backup","backup",fbuf,d1buf,d2buf,(char *)0,environ);
530*433d6423SLionel Sambuc 	error(FATAL, "cannot recursively exec backup", "", "");
531*433d6423SLionel Sambuc   } else {
532*433d6423SLionel Sambuc 	execle(pname, pname, d1buf, d2buf, (char *) 0, environ);
533*433d6423SLionel Sambuc 	execle("/bin/backup", "backup", d1buf, d2buf, (char *)0,environ);
534*433d6423SLionel Sambuc 	execle("/usr/bin/backup","backup", d1buf, d2buf, (char *)0,environ);
535*433d6423SLionel Sambuc 	error(FATAL, "cannot recursively exec backup", "", "");
536*433d6423SLionel Sambuc   }
537*433d6423SLionel Sambuc }
538*433d6423SLionel Sambuc 
newdisk(char * dir)539*433d6423SLionel Sambuc void newdisk(char *dir)
540*433d6423SLionel Sambuc {
541*433d6423SLionel Sambuc /* Ask for a new diskette. A big problem is that this program does not
542*433d6423SLionel Sambuc  * know which device is being used and where it is mounted on.  As an
543*433d6423SLionel Sambuc  * emergency solution, fork off a shell and ask the user to do the work.
544*433d6423SLionel Sambuc  */
545*433d6423SLionel Sambuc 
546*433d6423SLionel Sambuc   int pid, status;
547*433d6423SLionel Sambuc 
548*433d6423SLionel Sambuc   printf("\nDiskette full. Please do the following:\n");
549*433d6423SLionel Sambuc   printf("   1. Unmount the diskette using /etc/umount\n");
550*433d6423SLionel Sambuc   printf("   2. Physically replace the diskette by the next one.\n");
551*433d6423SLionel Sambuc   printf("   3. Mount the new diskette using /etc/mount\n");
552*433d6423SLionel Sambuc   printf("   4. Type CTRL-D to return to the backup/restore program\n");
553*433d6423SLionel Sambuc 
554*433d6423SLionel Sambuc   if ((pid = fork()) < 0) error(FATAL, "cannot fork", "", "");
555*433d6423SLionel Sambuc   if (pid > 0) {
556*433d6423SLionel Sambuc 	wait(&status);
557*433d6423SLionel Sambuc 	maketarget(dir);	/* make the directory */
558*433d6423SLionel Sambuc   } else {
559*433d6423SLionel Sambuc 	execle("/bin/sh", "sh", "-i", (char *) 0, environ);
560*433d6423SLionel Sambuc 	execle("/usr/bin/sh", "sh", "-i", (char *) 0, environ);
561*433d6423SLionel Sambuc 	error(FATAL, "cannot execute shell to ask for new diskette", "", "");
562*433d6423SLionel Sambuc   }
563*433d6423SLionel Sambuc }
564*433d6423SLionel Sambuc 
usage()565*433d6423SLionel Sambuc void usage()
566*433d6423SLionel Sambuc {
567*433d6423SLionel Sambuc   fprintf(stderr, "Usage: %s [-djmnorstvz] dir1 dir2\n", pname);
568*433d6423SLionel Sambuc   exit(2);
569*433d6423SLionel Sambuc }
570*433d6423SLionel Sambuc 
571*433d6423SLionel Sambuc 
error(int type,char * s1,char * s2,char * s3)572*433d6423SLionel Sambuc void error(int type, char *s1, char *s2, char *s3)
573*433d6423SLionel Sambuc {
574*433d6423SLionel Sambuc   fprintf(stderr, "%s: %s%s%s\n", pname, s1, s2, s3);
575*433d6423SLionel Sambuc 
576*433d6423SLionel Sambuc   if (type == NONFATAL)
577*433d6423SLionel Sambuc 	return;
578*433d6423SLionel Sambuc   else
579*433d6423SLionel Sambuc 	exit(type);
580*433d6423SLionel Sambuc }
581