[OpenWrt-Devel] [PATCH fstools 1/2] libfstools: accept volume as argument in most calls
Rafał Miłecki
zajec5 at gmail.com
Sun Dec 14 12:58:16 EST 2014
This optimizes code: drops unneeded calls to the volume_find and checks.
Signed-off-by: Rafał Miłecki <zajec5 at gmail.com>
---
libfstools/jffs2.c | 44 ++++++++------------------------------------
libfstools/libfstools.h | 8 +++++---
libfstools/overlay.c | 18 ++++--------------
libfstools/snapshot.c | 10 +++-------
mount_root.c | 6 +++---
5 files changed, 23 insertions(+), 63 deletions(-)
diff --git a/libfstools/jffs2.c b/libfstools/jffs2.c
index 134e09d..1b8e1d9 100644
--- a/libfstools/jffs2.c
+++ b/libfstools/jffs2.c
@@ -52,21 +52,13 @@ foreachdir(const char *dir, int (*cb)(const char*))
}
static int
-jffs2_mount(void)
+jffs2_mount(struct volume *v)
{
- struct volume *v;
-
if (mkdir("/tmp/overlay", 0755)) {
fprintf(stderr, "failed to mkdir /tmp/overlay: %s\n", strerror(errno));
return -1;
}
- v = volume_find("rootfs_data");
- if (!v) {
- fprintf(stderr, "rootfs_data does not exist\n");
- return -1;
- }
-
if (mount(v->blk, "/tmp/overlay", "jffs2", MS_NOATIME, NULL)) {
fprintf(stderr, "failed to mount -t jffs2 %s /tmp/overlay: %s\n", v->blk, strerror(errno));
return -1;
@@ -76,9 +68,8 @@ jffs2_mount(void)
}
static int
-switch2jffs(void)
+switch2jffs(struct volume *v)
{
- struct volume *v = volume_find("rootfs_data");
struct stat s;
int ret;
@@ -87,11 +78,6 @@ switch2jffs(void)
return -1;
}
- if (!v) {
- fprintf(stderr, "no rootfs_data was found\n");
- return -1;
- }
-
creat("/tmp/.switch_jffs2", 0600);
ret = mount(v->blk, "/rom/overlay", "jffs2", MS_NOATIME, NULL);
unlink("/tmp/.switch_jffs2");
@@ -157,9 +143,8 @@ handle_whiteout(const char *dir)
}
int
-jffs2_switch(int argc, char **argv)
+jffs2_switch(struct volume *v)
{
- struct volume *v;
char *mp;
int ret = -1;
@@ -171,7 +156,6 @@ jffs2_switch(int argc, char **argv)
return ret;
}
- v = volume_find("rootfs_data");
mp = find_mount_point(v->blk, 0);
if (mp) {
fprintf(stderr, "rootfs_data:%s is already mounted as %s\n", v->blk, mp);
@@ -184,7 +168,7 @@ jffs2_switch(int argc, char **argv)
/* fall through */
case FS_DEADCODE:
- ret = switch2jffs();
+ ret = switch2jffs(v);
if (!ret) {
fprintf(stderr, "doing fo cleanup\n");
umount2("/tmp/root", MNT_DETACH);
@@ -193,7 +177,7 @@ jffs2_switch(int argc, char **argv)
break;
case FS_JFFS2:
- ret = jffs2_mount();
+ ret = jffs2_mount(v);
if (ret)
break;
if (mount_move("/tmp", "", "/overlay") || fopivot("/overlay", "/rom")) {
@@ -206,21 +190,13 @@ jffs2_switch(int argc, char **argv)
return ret;
}
-static int mount_overlay_fs(void)
+static int mount_overlay_fs(struct volume *v)
{
- struct volume *v;
-
if (mkdir("/tmp/overlay", 0755)) {
fprintf(stderr, "failed to mkdir /tmp/overlay: %s\n", strerror(errno));
return -1;
}
- v = volume_find("rootfs_data");
- if (!v) {
- fprintf(stderr, "rootfs_data does not exist\n");
- return -1;
- }
-
if (mount(v->blk, "/tmp/overlay", "jffs2", MS_NOATIME, NULL)) {
fprintf(stderr, "failed to mount -t jffs2 %s /tmp/overlay: %s\n",
v->blk, strerror(errno));
@@ -232,21 +208,17 @@ static int mount_overlay_fs(void)
return -1;
}
-int mount_overlay(void)
+int mount_overlay(struct volume *v)
{
- struct volume *v = volume_find("rootfs_data");;
char *mp;
- if (!v)
- return -1;
-
mp = find_mount_point(v->blk, 0);
if (mp) {
fprintf(stderr, "rootfs_data:%s is already mounted as %s\n", v->blk, mp);
return -1;
}
- mount_overlay_fs();
+ mount_overlay_fs(v);
extroot_prefix = "/tmp/overlay";
if (!mount_extroot()) {
diff --git a/libfstools/libfstools.h b/libfstools/libfstools.h
index 70220c2..df7e48b 100644
--- a/libfstools/libfstools.h
+++ b/libfstools/libfstools.h
@@ -17,6 +17,8 @@
#include <libubox/list.h>
#include <libubox/blob.h>
+struct volume;
+
enum {
FS_NONE,
FS_SNAPSHOT,
@@ -27,8 +29,8 @@ enum {
extern char const *extroot_prefix;
extern int mount_extroot(void);
-extern int mount_snapshot(void);
-extern int mount_overlay(void);
+extern int mount_snapshot(struct volume *v);
+extern int mount_overlay(struct volume *v);
extern int mount_move(char *oldroot, char *newroot, char *dir);
extern int pivot(char *new, char *old);
@@ -43,7 +45,7 @@ extern int find_mtd_block(char *name, char *part, int plen);
extern int find_mtd_char(char *name, char *part, int plen);
extern int jffs2_ready(char *mtd);
-extern int jffs2_switch(int argc, char **argv);
+extern int jffs2_switch(struct volume *v);
extern int handle_whiteout(const char *dir);
extern void foreachdir(const char *dir, int (*cb)(const char*));
diff --git a/libfstools/overlay.c b/libfstools/overlay.c
index dc350b5..9e830a1 100644
--- a/libfstools/overlay.c
+++ b/libfstools/overlay.c
@@ -143,9 +143,8 @@ handle_whiteout(const char *dir)
}
int
-jffs2_switch(int argc, char **argv)
+jffs2_switch(struct volume *v)
{
- struct volume *v;
char *mp;
int ret = -1;
@@ -157,7 +156,6 @@ jffs2_switch(int argc, char **argv)
return ret;
}
- v = volume_find("rootfs_data");
mp = find_mount_point(v->blk, 0);
if (mp) {
fprintf(stderr, "rootfs_data:%s is already mounted as %s\n", v->blk, mp);
@@ -201,9 +199,8 @@ jffs2_switch(int argc, char **argv)
return ret;
}
-static int overlay_mount_fs(void)
+static int overlay_mount_fs(struct volume *v)
{
- struct volume *v;
char *fstype;
if (mkdir("/tmp/overlay", 0755)) {
@@ -211,12 +208,6 @@ static int overlay_mount_fs(void)
return -1;
}
- v = volume_find("rootfs_data");
- if (!v) {
- fprintf(stderr, "rootfs_data does not exist\n");
- return -1;
- }
-
fstype = "jffs2";
switch (volume_identify(v)) {
@@ -236,9 +227,8 @@ static int overlay_mount_fs(void)
return -1;
}
-int mount_overlay(void)
+int mount_overlay(struct volume *v)
{
- struct volume *v = volume_find("rootfs_data");;
char *mp;
if (!v)
@@ -250,7 +240,7 @@ int mount_overlay(void)
return -1;
}
- overlay_mount_fs();
+ overlay_mount_fs(v);
extroot_prefix = "/tmp/overlay";
if (!mount_extroot()) {
diff --git a/libfstools/snapshot.c b/libfstools/snapshot.c
index 3289a9d..f2ee0cd 100644
--- a/libfstools/snapshot.c
+++ b/libfstools/snapshot.c
@@ -273,16 +273,12 @@ volatile_write(struct volume *v, uint32_t _seq)
}
static int
-snapshot_sync(void)
+snapshot_sync(struct volume *v)
{
- struct volume *v = volume_find("rootfs_data");
struct file_header sentinel, conf;
int next, block = 0;
uint32_t seq;
- if (!v)
- return -1;
-
next = snapshot_next_free(v, &seq);
block = config_find(v, &conf, &sentinel);
if (is_config(&conf) && conf.seq != seq) {
@@ -328,9 +324,9 @@ _ramoverlay(char *rom, char *overlay)
}
int
-mount_snapshot(void)
+mount_snapshot(struct volume *v)
{
- snapshot_sync();
+ snapshot_sync(v);
setenv("SNAPSHOT", "magic", 1);
_ramoverlay("/rom", "/overlay");
system("/sbin/snapshot unpack");
diff --git a/mount_root.c b/mount_root.c
index 1df4723..c1524a4 100644
--- a/mount_root.c
+++ b/mount_root.c
@@ -47,11 +47,11 @@ start(int argc, char *argv[1])
case FS_JFFS2:
case FS_UBIFS:
- mount_overlay();
+ mount_overlay(v);
break;
case FS_SNAPSHOT:
- mount_snapshot();
+ mount_snapshot(v);
break;
}
@@ -78,7 +78,7 @@ done(int argc, char *argv[1])
switch (volume_identify(v)) {
case FS_NONE:
case FS_DEADCODE:
- return jffs2_switch(argc, argv);
+ return jffs2_switch(v);
}
return 0;
--
1.8.4.5
_______________________________________________
openwrt-devel mailing list
openwrt-devel at lists.openwrt.org
https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-devel
More information about the openwrt-devel
mailing list