home *** CD-ROM | disk | FTP | other *** search
- From: friedl@mtndew.UUCP (Steve Friedl)
- Newsgroups: alt.sources
- Subject: [comp.unix.questions] Re: Tip about lost+found
- Message-ID: <12144@stag.math.lsa.umich.edu>
- Date: 21 May 90 19:53:57 GMT
-
- Archive-name: mklostandfound/21-May-90
- Original-posting-by: friedl@mtndew.UUCP (Steve Friedl)
- Original-subject: Re: Tip about lost+found
- Reposted-by: emv@math.lsa.umich.edu (Edward Vielmetti)
-
- [This is an experimental alt.sources re-posting from the newsgroup(s)
- comp.unix.questions. Comments on this service to emv@math.lsa.umich.edu
- (Edward Vielmetti).]
-
-
- > Otherwise you'll need
- > to increase the number of slots in your lost+found directory (easily
- > done by writing a script that creates a whole bunch of empty files in
- > that directory and then deletes them again).
-
- I make filesystems so often that I got tired of writing the
- traditional shell script to make the lost+found directory, and in
- the process I ended up writing a C program to do it faster.
-
- It turns out that rather than creating a bunch of files, which
- involves making lots of inodes, it is much faster to make one
- file and then make lots of links to it. When done this way, the
- dominant operation is where it should be: writing directory
- entries to extend lost+found.
-
- I put this program (included shortly) in /etc/mkl+f and it has
- proved to be really handy. What I'd like to know is why in the
- world doesn't mkfs build a populated lost+found when it makes
- the filesystem? It seems to me to be the ideal place for it
- (at least by request with some command-line option).
-
- Another tip: make your lost+found directory drwx------.
- Otherwise, secret files that previously relied on protected
- parent directories suddenly can find themselves out in the open.
- A few packages (CrystalWriter, for instance), stick their trash
- files in lost+found, so do this only if you know you can.
-
- The program follows:
-
- #---------------------------------- cut here -------------------------
-
- /*
- * mkl+f.c
- *
- * written by : Stephen J. Friedl
- * Software Consultant
- * Tustin, CA 92680
- * uunet!mtndew!friedl
- *
- * Description:
- *
- * This makes a lot of directory entries in the current directory.
- * It starts by creat()ing the file ..000 and linking all the rest
- * to it. This method is over twice as fast as creat()ing the files
- * one at a time. This is because a creat() has to make an entire
- * inode, while a link just updates the directory and one inode's
- * link count. All the files are removed when done. The program
- * exits on errors.
- *
- * This program probably won't work on a BSD or SVR4.0 system.
- *
- * ===NOTE=== This program is in the public domain and can be
- * used by any person for any reason with no restrictions
- * whatsoever. Have fun, folks.
- *
- * HISTORY
- * Tue Jan 28 23:22:53 PST 1986 by SJF
- * > created
- */
- #include <stdio.h>
- #include <string.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <sys/types.h>
- #include <sys/stat.h>
-
- /*----------------------------------------------------------------------
- * this is the desired size of the lost+found directory
- */
- #define L_F_SIZE (4*1024) /* size of directory */
-
- #ifdef __STDC__
- # include <stdlib.h>
- # include <stdarg.h>
- # define Void void
- # define PROTO(name,args) name ( args )
- #else
- # include <varargs.h>
- # define Void char
- # define const /*nothing*/
- # define PROTO(name,args) name ( )
-
- extern char *malloc();
- extern void exit();
- #endif
-
- #ifndef EXIT_SUCCESS
- # define EXIT_SUCCESS 0
- # define EXIT_FAILURE 1
- #endif
-
- #define fpr (void)fprintf
- #define spr (void)sprintf
-
- /*----------------------------------------------------------------------
- * We keep a list of all files created here so we can remove them later.
- * We don't do any bounds checking when adding a new name to the list
- * because we statically allocate enough up front.
- */
- #define savefile(fname) (FileTbl[Nfiles++] = strsave(fname))
-
- static char const *FileTbl[L_F_SIZE]; /* list of files */
- static int Nfiles = 0; /* # of elements in above */
-
-
-
- static char const *ProgName, /* program name */
- *LinkFile; /* file to link to */
- static int dot_fd; /* fildes of current dir */
-
- static PROTO(long dot_size, ( void ) );
- static PROTO(char *strsave, ( char const * ) );
- static PROTO(void die, ( char const *, ... ) );
-
- /*ARGSUSED*/
- main(argc, argv)
- int argc;
- char *argv[];
- {
- int i, fd;
-
- ProgName = argv[0];
-
- /*--------------------------------------------------------------
- * open the current directory so we can get the size quickly
- * without having to know the size of a directory entry (feature).
- */
- if (dot_fd = open(".", O_RDONLY), dot_fd < 0)
- die("can't open current directory (errno = %d)", errno);
-
- /*--------------------------------------------------------------
- * are we already done?
- */
- if (dot_size() >= L_F_SIZE)
- die("current directory is already large enough");
-
- /*--------------------------------------------------------------
- * first touch the first file and die on error.
- */
- LinkFile = "..000";
-
- if (fd = creat(LinkFile, 0), fd < 0)
- die("can't create initial file %s (errno = %d)", LinkFile);
- (void)close(fd);
- savefile(LinkFile);
-
- /*--------------------------------------------------------------
- * now loop while the size is not enough. Before we make a
- * new link, make sure we're not trashing some current file.
- */
- while (dot_size() < L_F_SIZE)
- {
- char buf[100];
- static int counter = 0;
-
- spr(buf, "..%03d", counter++);
- if (file_exists(buf))
- continue;
- if (link(LinkFile, buf) != 0)
- die("can't link(\"%s\", \"%s\") (errno=%d)", LinkFile,
- buf, errno);
- savefile(buf);
- }
- (void)close(dot_fd);
-
- /*--------------------------------------------------------------
- * now clean up all the files we previously created.
- */
- for (i = 0; i < Nfiles; i++)
- {
- char const *fname = FileTbl[i];
-
- if (unlink(fname) != 0)
- die("can't unlink(\"%s\") - errno=%d", fname, errno);
- }
-
- exit(EXIT_SUCCESS);
- /* NOTREACHED */
- }
-
- /*
- * exists()
- *
- * Given a filename, return TRUE if the file exists and
- * FALSE if not. If we're able to stat the file, we assume
- * that the file exists...
- */
- static int
- file_exists(fname)
- char const *fname;
- {
- struct stat stbuf;
-
- return(stat(fname, &stbuf) == 0);
- }
-
- /*
- * strsave()
- *
- * Given a string, save it into managed memory
- */
- static char *
- strsave(str)
- char const *str;
- {
- char *p;
-
- if (p = malloc(strlen(str)+1), p == NULL)
- die("out of memory in strsave()");
- return(strcpy(p, str));
- }
-
- /*
- * dot_size()
- *
- * Return the size of the current directory. Die
- * if we can't find it (this should never happen).
- */
- static long
- dot_size()
- {
- struct stat stbuf;
-
- if (fstat(dot_fd, &stbuf) < 0)
- die("cannot stat current dir (errno = %d)", errno);
- return(stbuf.st_size);
- }
-
- /*
- * die()
- *
- * Given a printf-style argument list, format a message to the
- * standard error stream including the program name, then exit
- * with a failure status
- */
-
- #ifdef __STDC__
-
- static void
- die(char const *format, ...)
- {
- va_list args;
-
- (void)fprintf(stderr, "%s: ", ProgName);
-
- va_start(args, format);
- (void)vfprintf(stderr, format, args);
- va_end(args);
-
- (void)fputc('\n', stderr);
- exit(EXIT_FAILURE);
- }
-
- #else /*__STDC__*/
-
- /*VARARGS*/
- static void
- die(va_alist)
- va_dcl
- {
- va_list args;
- char *format;
-
- (void)fprintf(stderr, "%s: ", ProgName);
-
- va_start(args);
- format = va_arg(args, char *);
- (void)vfprintf(stderr, format, args);
- va_end(args);
-
- (void)fputc('\n', stderr);
- exit(EXIT_FAILURE);
- }
-
- #endif /* __STDC__ */
-
- #---------------------------------- cut here -------------------------
-
-
- --
- Stephen J. Friedl, KA8CMY / Software Consultant / Tustin, CA / 3B2-kind-of-guy
- +1 714 544 6561 voice / friedl@vsi.com / {uunet,attmail}!mtndew!friedl
-
- What's Spaf gonna do if Purdue hires RTM?
-