/* This file contains the file recovery program */
void recover(basename
, outname
)
char *basename
; /* the name of the file to recover */
char *outname
; /* the name of the file to write to */
char pathname
[500]; /* full pathname of the file to recover */
char line
[600]; /* a line from the /usr/preserve/Index file */
int ch
; /* a character from the text being recovered */
FILE *from
; /* the /usr/preserve file, or /usr/preserve/Index */
FILE *to
; /* the user's text file */
/* convert basename to a full pathname */
if (!basename
[0] || basename
[1] != ':')
if (basename
[0] != SLASH
)
ptr
= getcwd(pathname
, sizeof pathname
);
ptr
= pathname
+ strlen(pathname
);
strcpy(pathname
, basename
);
exit(_errmsg(errno
, "Can't set uid\n"));
/* scan the /usr/preserve/Index file, for the *oldest* unrecovered
from
= fopen(PRSVINDEX
, "r");
while (from
&& fgets(line
, sizeof line
, from
))
/* strip off the newline from the end of the string */
line
[strlen(line
) - 1] = '\0';
/* parse the line into a "preserve" name and a "text" name */
for (ptr
= line
; *ptr
!= ' '; ptr
++)
/* If the "preserve" file is missing, then ignore this line
* because it describes a file that has already been recovered.
/* are we looking for a specific file? */
/* quit if we found it */
if (!strcmp(ptr
, pathname
))
/* list this file as "available for recovery" */
if (!basename
|| !from
|| feof(from
))
if (from
!= NULL
) fclose(from
);
fprintf(stderr
, "%s: no recovered file has that exact name\n", pathname
);
if (from
!= NULL
) fclose(from
);
/* copy the recovered text back into the user's file... */
/* open the /usr/preserve file for reading */
/* Be careful about user-id. We want to be running under the user's
* real id when we open/create the user's text file... but we want
* to be superuser when we delete the /usr/preserve file. For UNIX,
* we accomplish this by deleting the /usr/preserve file *now*,
* when it is open but before we've read it. Then we revert to the
if (outname
== NULL
) return;
/* open the user's file for writing */
to
= fopen(outname
, "w");
while ((ch
= getc(from
)) != EOF
)
/* delete the /usr/preserve file */
fprintf(stderr
, "usage: %s [preserved_file [recovery_file]]\n", argv
[0]);
/* recover the requested file, or list recoverable files */
/* recover the file, but write it to a different filename */
recover (argv
[1], argv
[2]);
recover(argv
[1], argv
[1]);
/* list the recoverable files */
recover((char *)0, (char *)0);