Contents of /tags/mkinitrd-6_1_3/loadinitrd/loadinitrd.c
Parent Directory | Revision Log
Revision 846 -
(show annotations)
(download)
Mon May 4 18:51:23 2009 UTC (15 years, 4 months ago) by niro
File MIME type: text/plain
File size: 3617 byte(s)
Mon May 4 18:51:23 2009 UTC (15 years, 4 months ago) by niro
File MIME type: text/plain
File size: 3617 byte(s)
tagged 'mkinitrd-6_1_3'
1 | #include <dirent.h> |
2 | #include <errno.h> |
3 | #include <fcntl.h> |
4 | #include <newt.h> |
5 | #include <stdio.h> |
6 | #include <stdlib.h> |
7 | #include <string.h> |
8 | #include <sys/mount.h> |
9 | #include <sys/types.h> |
10 | |
11 | #define _(foo) (foo) |
12 | |
13 | void winStatus(int width, int height, char * title, |
14 | char * text, ...) { |
15 | newtComponent t, f; |
16 | char * buf = NULL; |
17 | int size = 0; |
18 | int i = 0; |
19 | va_list args; |
20 | |
21 | va_start(args, text); |
22 | |
23 | do { |
24 | size += 1000; |
25 | if (buf) free(buf); |
26 | buf = malloc(size); |
27 | i = vsnprintf(buf, size, text, args); |
28 | } while (i == size); |
29 | |
30 | va_end(args); |
31 | |
32 | newtCenteredWindow(width, height, title); |
33 | |
34 | t = newtTextbox(1, 1, width - 2, height - 2, NEWT_TEXTBOX_WRAP); |
35 | newtTextboxSetText(t, buf); |
36 | f = newtForm(NULL, NULL, 0); |
37 | |
38 | free(buf); |
39 | |
40 | newtFormAddComponent(f, t); |
41 | |
42 | newtDrawForm(f); |
43 | newtRefresh(); |
44 | newtFormDestroy(f); |
45 | } |
46 | |
47 | /* Recursive */ |
48 | int copyDirectory(char * from, char * to) { |
49 | DIR * dir; |
50 | struct dirent * ent; |
51 | int fd, outfd; |
52 | char buf[4096]; |
53 | int i; |
54 | int total; |
55 | struct stat sb; |
56 | char filespec[256]; |
57 | char filespec2[256]; |
58 | char link[1024]; |
59 | |
60 | mkdir(to, 0755); |
61 | |
62 | if (!(dir = opendir(from))) { |
63 | newtWinMessage(_("Error"), _("OK"), |
64 | _("Failed to read directory %s: %s"), |
65 | from, strerror(errno)); |
66 | return 1; |
67 | } |
68 | |
69 | errno = 0; |
70 | while ((ent = readdir(dir))) { |
71 | if (ent->d_name[0] == '.') continue; |
72 | |
73 | sprintf(filespec, "%s/%s", from, ent->d_name); |
74 | sprintf(filespec2, "%s/%s", to, ent->d_name); |
75 | |
76 | lstat(filespec, &sb); |
77 | |
78 | if (S_ISDIR(sb.st_mode)) { |
79 | if (copyDirectory(filespec, filespec2)) return 1; |
80 | } else if (S_ISLNK(sb.st_mode)) { |
81 | i = readlink(filespec, link, sizeof(link) - 1); |
82 | link[i] = '\0'; |
83 | unlink(filespec2); |
84 | if (symlink(link, filespec2)) { |
85 | fprintf(stderr, "failed to symlink %s to %s: %s", |
86 | filespec2, link, strerror(errno)); |
87 | } |
88 | } else { |
89 | fd = open(filespec, O_RDONLY); |
90 | if (fd < 0) { |
91 | fprintf(stderr, "failed to open %s: %s", filespec, |
92 | strerror(errno)); |
93 | return 1; |
94 | } |
95 | unlink(filespec2); |
96 | outfd = open(filespec2, O_RDWR | O_TRUNC | O_CREAT, 0644); |
97 | if (outfd < 0) { |
98 | fprintf(stderr, "failed to create %s: %s", filespec2, |
99 | strerror(errno)); |
100 | } else { |
101 | fchmod(outfd, sb.st_mode & 07777); |
102 | |
103 | total = 0; |
104 | |
105 | while ((i = read(fd, buf, sizeof(buf))) > 0) { |
106 | write(outfd, buf, i); |
107 | total += i; |
108 | } |
109 | |
110 | close(outfd); |
111 | printf("%s %d\n", filespec2, total); |
112 | } |
113 | |
114 | close(fd); |
115 | } |
116 | |
117 | errno = 0; |
118 | } |
119 | |
120 | closedir(dir); |
121 | |
122 | return 0; |
123 | } |
124 | |
125 | int main(int argc, char ** argv) { |
126 | int fd; |
127 | |
128 | if (mount("/", "/", "ext2", MS_REMOUNT | MS_MGC_VAL, NULL)) { |
129 | fprintf(stderr, "failed to remount root read-write: %s\n", |
130 | strerror(errno)); |
131 | while(1); |
132 | } |
133 | |
134 | newtInit(); |
135 | newtCls(); |
136 | |
137 | while (1) { |
138 | newtWinMessage(_("Insert Disk"), _("OK"), |
139 | _("Insert bootdisk #2 into your floppy drive and press " |
140 | "Enter to continue.")); |
141 | |
142 | fd = open("/dev/fd0", O_RDONLY); |
143 | if (fd >= 0) { |
144 | close(fd); |
145 | if (mount("/dev/fd0", "/mnt", "ext2", |
146 | (0xC0ED << 16) | MS_RDONLY, NULL)) { |
147 | newtWinMessage(_("Error"), _("OK"), |
148 | _("Error mounting floppy device.")); |
149 | } else if (access("/mnt/linuxrc", X_OK)) { |
150 | newtWinMessage(_("Error"), _("OK"), |
151 | _("Invalid #2 bootdisk.")); |
152 | } else { |
153 | break; |
154 | } |
155 | } else { |
156 | newtWinMessage(_("Error"), _("OK"), |
157 | _("Error opening floppy device.")); |
158 | } |
159 | } |
160 | |
161 | |
162 | winStatus(50, 3, _("Loading"), _("Loading bootdisk...")); |
163 | copyDirectory("/mnt", "/"); |
164 | umount("/mnt"); |
165 | newtPopWindow(); |
166 | |
167 | newtFinished(); |
168 | |
169 | execv(argv[0], argv); |
170 | |
171 | return 0; |
172 | } |