⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 backup.c

📁 操作系统源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
			cbuf[strlen(cbuf) - 2] = '\0';		er = stat(cbuf, &s);		if (er < 0 || sp->modtime > s.st_mtime) {			res = copy(dir1, sp, cbuf);		} else {			res = NONFATAL;		}		/* Check status of the copy. */		if (res == OUT_OF_SPACE) {			printf("Out of space while copying to %s\n", cbuf);			/* We ran out of space copying a regular file. */			if (mflag == 0)				error(FATAL, "Quitting, disk full", "", "");			/* If -m, ask for new diskette and continue. */			newdisk(dir2);			sp--;			continue;		}	} else if (fmode == S_IFDIR) {		/* Directory.  Execute this program recursively. */		copydir(dir1, dir2, sp->namep);	} else if (fmode == S_IFBLK || fmode == S_IFCHR) {		/* Special file. */		strncpy(cbuf, sp->namep, (size_t)NAME_SIZE);		printf("%s is special file.  Not backed up.\n", cbuf);	}  }}void swap(sp1, sp2)struct sorted *sp1, *sp2;{/* Swap two directory entries. */  struct sorted d;  d = *sp1;  *sp1 = *sp2;  *sp2 = d;}int copy(dir1, sp, cbuf2)struct sorted *sp;char *dir1, *cbuf2;{/* Copy a regular file. */  int fd1, fd2, nr, nw, res, n;  char cbuf1[MAX_PATH], *p;#ifdef NARROW  char *msg = (rflag || strcmp(pname, "backup")) ? "Restored" : "Backing up";#endif  /* If the -j or -o or -s flags were given, suppress certain files. */  p = sp->namep;  n = strlen(p);  if (n > NAME_SIZE) n = NAME_SIZE;  if (jflag) {	if (strcmp(p, "a.out") == 0) return(0);	if (strcmp(p, "core") == 0) return (0);	if (strcmp(p + n - 2, ".Z") == 0) return (0);	if (strcmp(p + n - 4, ".bak") == 0) return (0);	if (strcmp(p + n - 4, ".log") == 0) return (0);  }  if (oflag) {	if (strcmp(p + n - 2, ".o") == 0) return(0);  }  if (sflag) {	if (strcmp(p + n - 2, ".s") == 0) return(0);  }  res = 0;  if (dflag) return(0);		/* backup -d means only directories */  strcpy(cbuf1, dir1);  strncat(cbuf1, "/", (size_t)1);  strncat(cbuf1, sp->namep, (size_t)NAME_SIZE);	/* cbuf1 = source file name */  /* At this point, cbuf1 contains the source file name, cbuf2 the target. */  fd1 = open(cbuf1, O_RDONLY);  if (fd1 < 0) {	error(NONFATAL, "cannot open ", cbuf1, "");	return(res);  }  fd2 = creat(cbuf2, (sp->mode | S_IWUSR) & 07777);  if (fd2 < 0) {	if (errno == ENFILE) {		close(fd1);		return(OUT_OF_SPACE);	}	error(NONFATAL, "cannot create ", cbuf2, "");	close(fd1);	return(res);  }  /* Both files are now open.  Do the copying. */  if (!rflag && strncmp((sp->namep + n - 2), ".Z", (size_t)2) ||		rflag && !strncmp((sp->namep + n - 2), ".Z", (size_t)2)) {	if (zflag && (rflag || (n <= (NAME_SIZE - 2)))) {		close(fd1);		close(fd2);		res = zcopy(cbuf1, cbuf2);		if (tflag) utime(cbuf2, (struct utimbuf *) & (sp->acctime));		if (res != 0) unlink(cbuf2); /* if error, get rid of the corpse */#ifdef NARROW		if (vflag && res == 0) printf("%s %s\n", msg, cbuf1);#else		if (vflag && res == 0) {			printf("%-37.37s -> %-37.37s\n", cbuf1, cbuf2);			if (strlen(cbuf1) > 37 || strlen(cbuf2) > 37)				printf("%37.37s    %37.37s\n",				(strlen(cbuf1) > 37) ? (cbuf1 + 37) : "",				(strlen(cbuf2) > 37) ? (cbuf2 + 37) : "");		}#endif		return(res);	}  }  while (1) {	nr = read(fd1, copybuf, COPY_SIZE);	if (nr == 0) break;	if (nr < 0) {		error(NONFATAL, "read error on ", cbuf1, "");		res = EIO;		break;	}	nw = write(fd2, copybuf, nr);	if (nw < 0) {		if (errno == ENOSPC) {			/* Device is full. */			res = OUT_OF_SPACE;			break;		}		/* True write error. */		error(NONFATAL, "write error on ", cbuf2, "");		res = EIO;		break;	}  }  if (res == 0) {#ifdef NARROW 	if (vflag) printf("%s %s\n", msg, cbuf1);#else	if (vflag) {		printf("%-37.37s -> %-37.37s\n", cbuf1, cbuf2);		if (strlen(cbuf1) > 37 || strlen(cbuf2) > 37)			printf("%37.37s    %37.37s\n",			(strlen(cbuf1) > 37) ? (cbuf1 + 37) : "",			(strlen(cbuf2) > 37) ? (cbuf2 + 37) : "");	}#endif  } else {	unlink(cbuf2);  }  close(fd1);  close(fd2);  if (tflag) utime(cbuf2, (struct utimbuf *) & (sp->acctime));  return(res);}int zcopy(src, targ)char *src, *targ;{  int pid, status, res, s;  char fbuf[20];  strcpy(fbuf, "-c");  if (rflag)	strcat(fbuf, "d");  else	strcat(fbuf, "f");  if ((pid = fork()) < 0) error(FATAL, "cannot fork", "", "");  if (pid > 0) {	wait(&status);	/* Error codes 0 and 2 are ok, assume others mean disk is full. */	res = (status == 0 || status == NO_SAVINGS ? 0 : OUT_OF_SPACE);	return(res);  } else {	/* Child must execute compress. */	close(1);	s = open(targ, O_RDWR);	if (s < 0) error(FATAL, "cannot write on ", "targ", "");	execle("/bin/compress", "compress", fbuf, src, (char *)0, environ);	execle("/usr/bin/compress", "compress", fbuf, src, (char *)0, environ);	error(FATAL, "cannot exec compress", "", "");  }  return(0);}void copydir(dir1, dir2, namep)char *dir1, *dir2, *namep;{/* Copy a directory. */  int pid, status;  char fbuf[20], d1buf[MAX_PATH], d2buf[MAX_PATH];  if (nflag) return;	/* backup -n means no directories */  fbuf[0] = '\0';  /* Handle directory copy by forking off 'backup' ! */  if (jflag || mflag || oflag || rflag || sflag || tflag || vflag || zflag)	strcpy(fbuf, "-");  if (jflag) strcat(fbuf, "j");  if (mflag) strcat(fbuf, "m");  if (oflag) strcat(fbuf, "o");  if (rflag) strcat(fbuf, "r");  if (sflag) strcat(fbuf, "s");  if (tflag) strcat(fbuf, "t");  if (vflag) strcat(fbuf, "v");  if (zflag) strcat(fbuf, "z");  strcpy(d1buf, dir1);  strcat(d1buf, "/");  strncat(d1buf, namep, (size_t)NAME_SIZE);  strcpy(d2buf, dir2);  strcat(d2buf, "/");  strncat(d2buf, namep, (size_t)NAME_SIZE);  if ((pid = fork()) < 0) error(FATAL, "cannot fork", "", "");  if (pid > 0) {	/* Parent waits for child, then returns. */	wait(&status);	return;  }  if (fbuf[0] == '-') {	execle(pname, pname, fbuf, d1buf, d2buf, (char *) 0, environ);	execle("/bin/backup", "backup", fbuf, d1buf, d2buf, (char *)0,environ);	execle("/usr/bin/backup","backup",fbuf,d1buf,d2buf,(char *)0,environ);	error(FATAL, "cannot recursively exec backup", "", "");  } else {	execle(pname, pname, d1buf, d2buf, (char *) 0, environ);	execle("/bin/backup", "backup", d1buf, d2buf, (char *)0,environ);	execle("/usr/bin/backup","backup", d1buf, d2buf, (char *)0,environ);	error(FATAL, "cannot recursively exec backup", "", "");  }}void newdisk(dir)char *dir;{/* Ask for a new diskette. A big problem is that this program does not * know which device is being used and where it is mounted on.  As an * emergency solution, fork off a shell and ask the user to do the work. */  int pid, status;  printf("\nDiskette full. Please do the following:\n");  printf("   1. Unmount the diskette using /etc/umount\n");  printf("   2. Physically replace the diskette by the next one.\n");  printf("   3. Mount the new diskette using /etc/mount\n");  printf("   4. Type CTRL-D to return to the backup/restore program\n");  if ((pid = fork()) < 0) error(FATAL, "cannot fork", "", "");  if (pid > 0) {	wait(&status);	maketarget(dir);	/* make the directory */  } else {	execle("/bin/sh", "sh", "-i", (char *) 0, environ);	execle("/usr/bin/sh", "sh", "-i", (char *) 0, environ);	error(FATAL, "cannot execute shell to ask for new diskette", "", "");  }}void usage(){  fprintf(stderr, "Usage: %s [-djmnorstvz] dir1 dir2\n", pname);  exit(2);}void error(type, s1, s2, s3)int type;char *s1, *s2, *s3;{  fprintf(stderr, "%s: %s%s%s\n", pname, s1, s2, s3);  if (type == NONFATAL)	return;  else	exit(type);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -