home *** CD-ROM | disk | FTP | other *** search
- /* This program archives and removes all files on /u0
- that have not been accessed in 180 days.
- */
-
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <sys/file.h>
- #include <strings.h>
-
- #define MAXLEN 10240
- #define USAGE "derez [-t days] [dir1 dir2 dir3 ...]\n"
-
- #include <stdio.h>
-
- main(argc, argv)
- int argc;
- char **argv;
- {
- FILE *fptr;
- struct stat statbuf;
- char filename[MAXLEN];
- char cmd[MAXLEN];
- char dirlist[MAXLEN];
- int since;
-
- dirlist[0] = NULL;
- since = 180;
-
-
- /* Check for the existence and access permissions on the MORGUE.
- */
- if(stat("MORGUE", &statbuf)) {
- perror("derez: MORGUE");
- exit(1);
- }
- if(!(statbuf.st_mode & S_IFDIR)) {
- fprintf(stderr, "derez: MORGUE is not a directory\n");
- exit(1);
- }
- if(access("MORGUE", R_OK | W_OK | X_OK)) {
- fprintf(stderr, "derez: The MORGUE must have read, write and execute perms\n");
- exit(1);
- }
-
- if(argc > 1) {
- if(strcmp(argv[1], "-t") == 0) {
- if(argc < 3) {
- fprintf(stderr, USAGE);
- exit(1);
- }
- if(sscanf(argv[2], "%d", &since) != 1) {
- fprintf(stderr, "derez: bad arg for -t option %s\n", argv[2]);
- exit(1);
- }
- if(since < 1 || since > 1000) {
- fprintf(stderr, "derez: bad value for -t argument\n");
- exit(1);
- }
- argv += 2;
- argc -= 2;
- }
- while(argc > 1) {
- if((strncmp(argv[1], "/", strlen("/")) == 0) || (strncmp(argv[1], "../", strlen("../")) == 0)) {
- fprintf(stderr, "derez: Directories must be relative to .\n");
- exit(1);
- }
- if(lstat(argv[1], &statbuf)) {
- fprintf(stderr, "The directory %s must exist\n", argv[1]);
- exit(1);
- }
- if(statbuf.st_mode & S_IFDIR) {
- if(dirlist[0] != NULL)
- strcat(dirlist, " ");
- strcat(dirlist, argv[1]);
- argv += 1;
- argc -= 1;
- }
- else {
- fprintf(stderr, "derez: %s is not a directory\n", argv[1]);
- exit(1);
- }
- }
- }
- /* We currently only derez files with 1 link as derez would
- break up two links to the same file into seperate files.
- We will have to steal code from tar to do an internal link table
- and drop the use of tar completely.
- */
- sprintf(cmd, "find %s -type f -links 1 -atime +%d -print > MORGUE/DEREZ",
- (dirlist[0] == NULL ? "." : dirlist), since);
-
- printf("%s\n", cmd);
- if(system(cmd) != 0) {
- fprintf(stderr, "derez: The attempt to create MORGUE/DEREZ failed\n");
- exit(1);
- }
- if((fptr = fopen("MORGUE/DEREZ", "r")) == NULL) {
- fprintf(stderr, "Open of MORGUE/DEREZ for reading failed\n");
- exit(1);
- }
- while(fgets(filename, MAXLEN, fptr) != NULL) {
- char *rightslash;
- /* Kill off the newline. */
- if(filename[strlen(filename) - 1] != '\n') {
- fprintf(stderr, "missing \\n on %s\n", filename);
- continue;
- }
- filename[strlen(filename) - 1] = NULL;
-
- /* If the file path name is too long we don't want
- to see the error message from tar.
- */
- if(strlen(filename) > 99)
- continue;
-
- /* If the file basename is of the form .* we want to
- leave it alone to prevent removal of someones login
- directory or setup files. Of course if you take this
- code out you get automatic removal of directories
- associated with inactive accounts.
- */
- if((rightslash = rindex(filename, '/')) != NULL) {
- if(rightslash[1] == '.')
- continue;
- }
-
- if(
- /* We don't want to mess with files already in the MORGUE.
- */
- (strncmp(filename, "./MORGUE/", strlen("./MORGUE/")) != 0)
- && (strncmp(filename, "MORGUE/", strlen("MORGUE/")) != 0)
- /* The lost+found directory is a sacred system item.
- */
- && (strncmp(filename, "./lost+found/", strlen("./lost+found/")) != 0)
- && (strncmp(filename, "lost+found/", strlen("lost+found/")) != 0)
- /* If we were to enmorgue a file named DEREZ, we
- would overwrite the list of files to move.
- */
- && (strncmp(filename, "./DEREZ") != 0)
- && (strcmp(filename, "DEREZ") != 0)
- ){
- sprintf(cmd, "tar cf - %s | (cd MORGUE; tar xpf -)", filename);
- if(system(cmd) == 0) {
- fprintf(stderr, "enmorgue of %s succeeded\n", filename);
- unlink(filename);
- /* If it was the last file in the directory
- we also want to remove the directory.
- */
- while(strlen(filename) > 0 && filename[strlen(filename) - 1] != '/') {
- filename[strlen(filename) - 1] = NULL;
- }
- filename[strlen(filename) - 1] = NULL;
- rmdir(filename);
- }
- else {
- fprintf(stderr, "enmorgue of %s FAILED, file removal was inhibited\n", filename);
- }
- }
- }
- fclose(fptr);
- }
-