aboutsummaryrefslogtreecommitdiff
path: root/makefs.c
diff options
context:
space:
mode:
authorWojtek Kosior <kwojtus@protonmail.com>2019-12-28 12:29:08 +0100
committerWojtek Kosior <kwojtus@protonmail.com>2019-12-28 12:29:08 +0100
commit1168c080af2f2f901a08886b670228a05ce74b77 (patch)
tree80090ba324ee1208526e70e6abca391cc33cd365 /makefs.c
parentb2dd2b0507571399723b3fe74bb469bc7e24f8a0 (diff)
downloadrpi-MMU-example-1168c080af2f2f901a08886b670228a05ce74b77.tar.gz
rpi-MMU-example-1168c080af2f2f901a08886b670228a05ce74b77.zip
add comments in makefs.c
Diffstat (limited to 'makefs.c')
-rw-r--r--makefs.c30
1 files changed, 25 insertions, 5 deletions
diff --git a/makefs.c b/makefs.c
index 31831a3..c122267 100644
--- a/makefs.c
+++ b/makefs.c
@@ -1,3 +1,12 @@
+// Take files given on stdin and make them into a ramfs image of our
+// own, (stupid) simple format.
+// In the format: for each file comes the null-terminated string
+// with filename, then null-padding until a 4-aligned offset, then
+// 4-byte little-endian size of the file and then the contents
+// of the file and then another null-padding until a 4-aligned offset.
+// Files encoded this way go one after another (so it's easy to add
+// something at the end or at the beginning).
+
#include <stdint.h>
#include <sys/types.h>
#include <sys/wait.h>
@@ -13,6 +22,7 @@
int main(int argc, char **argv)
{
+ // process files in the order they are provided on the command line
for (int i = 1; i < argc; i++)
{
struct stat fileinfo;
@@ -25,26 +35,35 @@ int main(int argc, char **argv)
errx(-1, ANSI_FG_RED "%s" ANSI_FG_DEFAULT
" is not a regular file.", argv[i]);
+ // don't allow files with size so big, that it can't be encoded
+ // in a 4-byte unsigned int... In practice even smaller files
+ // won't fit on the rpi.
if (fileinfo.st_size > UINT32_MAX)
errx(-1, ANSI_FG_RED "%s" ANSI_FG_DEFAULT
" is too big.", argv[i]);
uint32_t file_size = fileinfo.st_size;
- uint32_t name_size = strlen(argv[i]) + 1;
+ uint32_t name_size = strlen(argv[i]) + 1; // 1 for null-byte
if (fwrite(argv[i], 1, name_size, stdout) != name_size)
errx(-1, "error writing to stdout");
- for (int j = 0; (j + (name_size & 0b11)) & 0b11; j++)
+ // pad with null-bytes until a 4-aligned offset
+ for (unsigned int j = 0; (j + (name_size & 0b11)) & 0b11; j++)
if (putchar('\0'))
errx(-1, "error writing to stdout");
-
+
+ // TODO convert file_size to little endian first (in case our
+ // host is be).
if (fwrite(&file_size, 4, 1, stdout) != 1)
errx(-1, "error writing to stdout");
+ // flush b4 running cat, so that stuff we've written comes
+ // b4 the actual file contents in the output
if (fflush(stdout))
err(-1, "couldn't flush stdout");
-
+
+ // we don't copy the actual file ourselves - we run cat for that
pid_t pid;
int wstatus;
switch (pid = fork())
@@ -62,9 +81,10 @@ int main(int argc, char **argv)
exit(-1);
}
+ // again, pad with null-bytes until a 4-aligned offset
for (int j = 0; (j + (file_size & 0b11)) & 0b11; j++)
if (putchar('\0'))
- errx(-1, "error writing to stdout");
+ errx(-1, "error writing to stdout");
}
return 0;