From 16495b8fc729b53f3af870be6003068c5ce8cf49 Mon Sep 17 00:00:00 2001
From: Jeremy Soller <jackpot51@gmail.com>
Date: Thu, 3 Mar 2016 15:32:15 -0700
Subject: [PATCH] Automagically create initfs from initfs directory, update
 redoxfs

---
 .gitignore               |  1 +
 Makefile                 | 25 ++++++++++++++++++-------
 crates/redoxfs           |  2 +-
 filesystem/etc/init.rc   |  6 +++---
 kernel/schemes/initfs.rs |  9 ++++-----
 5 files changed, 27 insertions(+), 16 deletions(-)

diff --git a/.gitignore b/.gitignore
index 904e3b4..047a5a4 100644
--- a/.gitignore
+++ b/.gitignore
@@ -18,6 +18,7 @@ libc/build/
 setup/rust/
 build
 filesystem/bin/
+initfs/
 target
 *.swp
 doc
diff --git a/Makefile b/Makefile
index 031685b..d6b64bf 100644
--- a/Makefile
+++ b/Makefile
@@ -233,10 +233,6 @@ filesystem/bin/launcher: crates/orbutils/src/launcher/main.rs crates/orbutils/sr
 	$(RUSTC) $(RUSTCFLAGS) --crate-type bin -o $@ $<
 
 
-filesystem/bin/redoxfsd: crates/redoxfs/scheme/main.rs crates/redoxfs/scheme/*.rs $(BUILD)/crt0.o $(BUILD)/libstd.rlib $(BUILD)/libredoxfs.rlib
-	mkdir -p filesystem/bin
-	$(RUSTC) $(RUSTCFLAGS) --crate-type bin -o $@ $<
-
 bins: \
 	coreutils \
 	extrautils \
@@ -251,7 +247,6 @@ bins: \
   	filesystem/bin/lua \
   	filesystem/bin/login \
   	filesystem/bin/orbital \
-  	filesystem/bin/redoxfsd \
   	filesystem/bin/sdl-test \
   	filesystem/bin/sdl-ttf-test \
   	filesystem/bin/sh \
@@ -260,6 +255,22 @@ bins: \
 	filesystem/bin/zfs
 	#TODO: binutils
 
+initfs/redoxfsd: crates/redoxfs/scheme/main.rs crates/redoxfs/scheme/*.rs $(BUILD)/crt0.o $(BUILD)/libstd.rlib $(BUILD)/libredoxfs.rlib
+	mkdir -p initfs/
+	$(RUSTC) $(RUSTCFLAGS) --crate-type bin -o $@ $<
+
+initfs: \
+	initfs/redoxfsd
+
+build/initfs.gen: initfs FORCE
+	echo 'use collections::BTreeMap;' > $@
+	echo 'pub fn gen() -> BTreeMap<&'"'"'static str, &'"'"'static [u8]> {' >> $@
+	echo '    let mut files: BTreeMap<&'"'"'static str, &'"'"'static [u8]> = BTreeMap::new();' >> $@
+	$(FIND) initfs -not -path '*/\.*' -type f -o -type l | $(CUT) -d '/' -f2- | $(SORT) \
+		| $(AWK) '{printf("    files.insert(\"%s\", include_bytes!(\"../initfs/%s\"));\n", $$0, $$0)}' \
+		>> $@
+	echo '    files' >> $@
+	echo '}' >> $@
 
 test: kernel/main.rs \
 	  rust/src/libtest/lib.rs \
@@ -270,7 +281,7 @@ test: kernel/main.rs \
 	$(RUSTC) $(RUSTCFLAGS) --test $<
 
 clean:
-	$(RM) -rf build doc filesystem/bin/ filesystem/apps/*/*.bin filesystem/apps/*/*.list
+	$(RM) -rf build doc filesystem/bin/ initfs/bin/ filesystem/apps/*/*.bin filesystem/apps/*/*.list
 
 FORCE:
 
@@ -356,7 +367,7 @@ $(BUILD)/libsystem.rlib: crates/system/lib.rs crates/system/*.rs crates/system/*
 $(BUILD)/libredoxfs.rlib: crates/redoxfs/src/lib.rs crates/redoxfs/src/*.rs $(BUILD)/libsystem.rlib $(BUILD)/liballoc.rlib $(BUILD)/libcollections.rlib
 	$(RUSTC) $(RUSTCFLAGS) -o $@ $<
 
-$(BUILD)/kernel.rlib: kernel/main.rs kernel/*.rs kernel/*/*.rs kernel/*/*/*.rs  $(BUILD)/libio.rlib $(BUILD)/libredoxfs.rlib filesystem/bin/redoxfsd
+$(BUILD)/kernel.rlib: kernel/main.rs kernel/*.rs kernel/*/*.rs kernel/*/*/*.rs  $(BUILD)/libio.rlib build/initfs.gen
 	$(RUSTC) $(RUSTCFLAGS) -C lto -o $@ $<
 
 $(BUILD)/kernel.bin: $(BUILD)/kernel.rlib kernel/kernel.ld
diff --git a/crates/redoxfs b/crates/redoxfs
index 51e0371..ff0106b 160000
--- a/crates/redoxfs
+++ b/crates/redoxfs
@@ -1 +1 @@
-Subproject commit 51e037150c6c92d5535f49e23cc787c1e6c76866
+Subproject commit ff0106b212f6d67b30cf24fbf940f3a5a7b61ecd
diff --git a/filesystem/etc/init.rc b/filesystem/etc/init.rc
index ed9bc9d..374bd5f 100644
--- a/filesystem/etc/init.rc
+++ b/filesystem/etc/init.rc
@@ -1,9 +1,9 @@
+# Redox FS, to be moved into initfs
+initfs:redoxfsd /etc/redoxfs.bin
+
 # Example scheme
 example
 
-# New file scheme
-redoxfsd /etc/redoxfs.bin
-
 # Orbital scheme, handles displays
 echo `orbital` to start GUI
 
diff --git a/kernel/schemes/initfs.rs b/kernel/schemes/initfs.rs
index f82597b..634dc09 100644
--- a/kernel/schemes/initfs.rs
+++ b/kernel/schemes/initfs.rs
@@ -6,6 +6,9 @@ use fs::{KScheme, Resource, Url, VecResource};
 
 use system::error::{Error, Result, ENOENT};
 
+#[path="../../build/initfs.gen"]
+pub mod gen;
+
 /// A memory scheme
 pub struct InitFsScheme {
     pub files: BTreeMap<&'static str, &'static [u8]>
@@ -13,12 +16,8 @@ pub struct InitFsScheme {
 
 impl InitFsScheme {
     pub fn new() -> Box<InitFsScheme> {
-        let mut files: BTreeMap<&'static str, &'static [u8]> = BTreeMap::new();
-
-        files.insert("redoxfsd", include_bytes!("../../filesystem/bin/redoxfsd"));
-
         Box::new(InitFsScheme {
-            files: files
+            files: gen::gen()
         })
     }
 }
