3 /* This file contains the file recovery program */
7 * 14407 SW Teal Blvd. #C
18 void recover(basename
, outname
)
19 char *basename
; /* the name of the file to recover */
20 char *outname
; /* the name of the file to write to */
22 char pathname
[500]; /* full pathname of the file to recover */
23 char line
[600]; /* a line from the /usr/preserve/Index file */
24 int ch
; /* a character from the text being recovered */
25 FILE *from
; /* the /usr/preserve file, or /usr/preserve/Index */
26 FILE *to
; /* the user's text file */
33 /* convert basename to a full pathname */
38 if (!basename
[0] || basename
[1] != ':')
40 if (basename
[0] != SLASH
)
43 ptr
= getcwd(pathname
, sizeof pathname
);
46 strcpy(pathname
, ptr
);
48 ptr
= pathname
+ strlen(pathname
);
50 strcpy(ptr
, basename
);
55 strcpy(pathname
, basename
);
62 exit(_errmsg(errno
, "Can't set uid\n"));
64 /* scan the /usr/preserve/Index file, for the *oldest* unrecovered
65 * version of this file.
67 from
= fopen(PRSVINDEX
, "r");
68 while (from
&& fgets(line
, sizeof line
, from
))
70 /* strip off the newline from the end of the string */
71 line
[strlen(line
) - 1] = '\0';
73 /* parse the line into a "preserve" name and a "text" name */
74 for (ptr
= line
; *ptr
!= ' '; ptr
++)
79 /* If the "preserve" file is missing, then ignore this line
80 * because it describes a file that has already been recovered.
82 if (stat(line
, &st
) < 0)
87 /* are we looking for a specific file? */
90 /* quit if we found it */
91 if (!strcmp(ptr
, pathname
))
98 /* list this file as "available for recovery" */
103 /* file not found? */
104 if (!basename
|| !from
|| feof(from
))
106 if (from
!= NULL
) fclose(from
);
109 fprintf(stderr
, "%s: no recovered file has that exact name\n", pathname
);
113 if (from
!= NULL
) fclose(from
);
115 /* copy the recovered text back into the user's file... */
117 /* open the /usr/preserve file for reading */
118 from
= fopen(line
, "r");
126 /* Be careful about user-id. We want to be running under the user's
127 * real id when we open/create the user's text file... but we want
128 * to be superuser when we delete the /usr/preserve file. For UNIX,
129 * we accomplish this by deleting the /usr/preserve file *now*,
130 * when it is open but before we've read it. Then we revert to the
140 if (outname
== NULL
) return;
142 /* open the user's file for writing */
143 to
= fopen(outname
, "w");
151 while ((ch
= getc(from
)) != EOF
)
161 /* delete the /usr/preserve file */
173 /* check arguments */
176 fprintf(stderr
, "usage: %s [preserved_file [recovery_file]]\n", argv
[0]);
180 /* recover the requested file, or list recoverable files */
183 /* recover the file, but write it to a different filename */
184 recover (argv
[1], argv
[2]);
188 /* recover the file */
189 recover(argv
[1], argv
[1]);
193 /* list the recoverable files */
194 recover((char *)0, (char *)0);