lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 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 "config.h" |
| 17 | #include <errno.h> |
| 18 | #include <fcntl.h> |
| 19 | #include <stdio.h> |
| 20 | #include <string.h> |
| 21 | #include <unistd.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <sys/param.h> |
| 24 | #include <sys/stat.h> |
| 25 | |
| 26 | #include "ext2fs/ext2_fs.h" |
| 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 | setlocale(LC_CTYPE, ""); |
| 43 | bindtextdomain(NLS_CAT_NAME, LOCALEDIR); |
| 44 | textdomain(NLS_CAT_NAME); |
| 45 | #endif |
| 46 | fprintf (stderr, "mklost+found %s (%s)\n", E2FSPROGS_VERSION, |
| 47 | E2FSPROGS_DATE); |
| 48 | if (argc != 1) { |
| 49 | (void)argv; /* avoid unused argument warning */ |
| 50 | fprintf (stderr, "%s", _("Usage: mklost+found\n")); |
| 51 | exit(1); |
| 52 | } |
| 53 | if (mkdir (LPF, 0700) == -1) { |
| 54 | perror ("mkdir"); |
| 55 | exit(1); |
| 56 | } |
| 57 | |
| 58 | i = 0; |
| 59 | memset (name, 'x', 246); |
| 60 | do { |
| 61 | sprintf (name + 246, "%08d", i); |
| 62 | strcpy (path, LPF); |
| 63 | strcat (path, "/"); |
| 64 | strcat (path, name); |
| 65 | if ((d = creat (path, 0644)) == -1) { |
| 66 | perror ("creat"); |
| 67 | exit (1); |
| 68 | } |
| 69 | i++; |
| 70 | close (d); |
| 71 | if (stat (LPF, &st) == -1) { |
| 72 | perror ("stat"); |
| 73 | exit (1); |
| 74 | } |
| 75 | } while (st.st_size <= (EXT2_NDIR_BLOCKS - 1) * st.st_blksize); |
| 76 | for (j = 0; j < i; j++) { |
| 77 | sprintf (name + 246, "%08d", j); |
| 78 | strcpy (path, LPF); |
| 79 | strcat (path, "/"); |
| 80 | strcat (path, name); |
| 81 | if (unlink (path) == -1) { |
| 82 | perror ("unlink"); |
| 83 | exit (1); |
| 84 | } |
| 85 | } |
| 86 | exit (0); |
| 87 | } |