Whamcloud - gitweb
ChangeLog:
[tools/e2fsprogs.git] / misc / mklost+found.c
1 /*
2  * mklost+found.c       - Creates a directory lost+found on a mounted second
3  *                        extended file system
4  *
5  * Copyright (C) 1992, 1993  Remy Card <card@masi.ibp.fr>
6  *
7  * This file can be redistributed under the terms of the GNU General
8  * Public License
9  */
10
11 /*
12  * History:
13  * 93/04/22     - Creation
14  */
15
16 #include <errno.h>
17 #include <fcntl.h>
18 #include <stdio.h>
19 #include <string.h>
20 #include <unistd.h>
21 #include <stdlib.h>
22 #include <sys/param.h>
23 #include <sys/stat.h>
24
25 #include <linux/ext2_fs.h>
26
27 #include "../version.h"
28 #include "nls-enable.h"
29
30 #define LPF "lost+found"
31
32 int main (int argc, char ** argv)
33 {
34         char name [EXT2_NAME_LEN];
35         char path [sizeof (LPF) + 1 + 256];
36         struct stat st;
37         int i, j;
38         int d;
39
40 #ifdef ENABLE_NLS
41         setlocale(LC_MESSAGES, "");
42         bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
43         textdomain(NLS_CAT_NAME);
44 #endif
45         fprintf (stderr, _("mklost+found %s, %s for EXT2 FS %s, %s\n"),
46                  E2FSPROGS_VERSION, E2FSPROGS_DATE,
47                  EXT2FS_VERSION, EXT2FS_DATE);
48         if (argc != 1) {
49                 fprintf (stderr, _("Usage: mklost+found\n"));
50                 exit(1);
51         }
52         if (mkdir (LPF, 0755) == -1) {
53                 perror ("mkdir");
54                 exit(1);
55         }
56         
57         i = 0;
58         memset (name, 'x', 252);
59         do {
60                 sprintf (name + 252, "%02d", i);
61                 strcpy (path, LPF);
62                 strcat (path, "/");
63                 strcat (path, name);
64                 if ((d = creat (path, 0644)) == -1) {
65                         perror ("creat");
66                         exit (1);
67                 }
68                 i++;
69                 close (d);
70                 if (stat (LPF, &st) == -1) {
71                         perror ("stat");
72                         exit (1);
73                 }
74         } while (st.st_size <= (EXT2_NDIR_BLOCKS - 1) * st.st_blksize);
75         for (j = 0; j < i; j++) {
76                 sprintf (name + 252, "%02d", j);
77                 strcpy (path, LPF);
78                 strcat (path, "/");
79                 strcat (path, name);
80                 if (unlink (path) == -1) {
81                         perror ("unlink");
82                         exit (1);
83                 }
84         }
85         exit (0);
86 }