changeset 97:4c81e6375719

Add parent pointer to dirtree, more work on mke2fs (populate dirtree, count index blocks).
author Rob Landley <rob@landley.net>
date Sun, 04 Feb 2007 19:14:58 -0500
parents 05d109d4acb9
children 5174f1459c91
files lib/lib.c lib/lib.h toys/e2fs.h toys/mke2fs.c toys/toylist.h
diffstat 5 files changed, 41 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/lib/lib.c	Sun Feb 04 19:06:07 2007 -0500
+++ b/lib/lib.c	Sun Feb 04 19:14:58 2007 -0500
@@ -534,7 +534,7 @@
 // Given a directory (in a writeable PATH_MAX buffer), recursively read in a
 // directory tree.
 
-struct dirtree *read_dirtree(char *path)
+struct dirtree *read_dirtree(char *path, struct dirtree *parent)
 {
 	struct dirtree *dt = NULL, **ddt = &dt;
 	DIR *dir;
@@ -554,7 +554,8 @@
 
 		snprintf(path+len, sizeof(toybuf)-len, "/%s", entry->d_name);
 		*ddt = read_dirtree_node(path);
-		if (entry->d_type == DT_DIR) (*ddt)->child = read_dirtree(path);
+		(*ddt)->parent = parent;
+		if (entry->d_type == DT_DIR) (*ddt)->child = read_dirtree(path, *ddt);
 		ddt = &((*ddt)->next);
 		path[len]=0;
 	}
--- a/lib/lib.h	Sun Feb 04 19:06:07 2007 -0500
+++ b/lib/lib.h	Sun Feb 04 19:14:58 2007 -0500
@@ -22,8 +22,7 @@
 };
 
 struct dirtree {
-	struct dirtree *next;
-	struct dirtree *child;
+	struct dirtree *next, *child, *parent;
 	struct stat st;
 	char name[];
 };
@@ -68,7 +67,7 @@
 char *itoa(int n);
 off_t fdlength(int fd);
 struct dirtree *read_dirtree_node(char *path);
-struct dirtree *read_dirtree(char *path);
+struct dirtree *read_dirtree(char *path, struct dirtree *parent);
 
 // getmountlist.c
 struct mtab_list {
--- a/toys/e2fs.h	Sun Feb 04 19:06:07 2007 -0500
+++ b/toys/e2fs.h	Sun Feb 04 19:14:58 2007 -0500
@@ -80,7 +80,7 @@
     uint16_t rec_len;       // Directory entry length
     uint8_t  name_len;      // Name length
     uint8_t  file_type;
-	char     name[255];     // File name
+	char     name[0];     // File name
 };
 
 struct ext2_inode {
@@ -109,8 +109,6 @@
 	uint32_t reserved2;
 };
 
-
-
 #define EXT2_FEATURE_COMPAT_DIR_PREALLOC	0x0001
 #define EXT2_FEATURE_COMPAT_IMAGIC_INODES	0x0002
 #define EXT3_FEATURE_COMPAT_HAS_JOURNAL		0x0004
--- a/toys/mke2fs.c	Sun Feb 04 19:06:07 2007 -0500
+++ b/toys/mke2fs.c	Sun Feb 04 19:14:58 2007 -0500
@@ -30,6 +30,26 @@
 
 #define INODES_RESERVED 10
 
+// Calculate data blocks plus index blocks needed to hold a file.
+uint32_t blocks_used(uint64_t size)
+{
+	uint32_t dblocks = (uint32_t)((size+(TT.blocksize-1))/TT.blocksize);
+	uint32_t idx=TT.blocksize/4, iblocks=0, diblocks=0, tiblocks=0;
+
+	// Account for direct, singly, doubly, and triply indiret index blocks
+
+	if (dblocks > 12) {
+		iblocks = ((dblocks-13)/idx)+1;
+		if (iblocks > 1) {
+			diblocks = ((iblocks-2)/idx)+1;
+			if (diblocks > 1)
+				tiblocks = ((diblocks-2)/idx)+1;
+		}
+	}
+	
+	return dblocks + iblocks + diblocks + tiblocks;
+}
+
 // According to http://www.opengroup.org/onlinepubs/9629399/apdxa.htm
 // we should generate a uuid structure by reading a clock with 100 nanosecond
 // precision, normalizing it to the start of the gregorian calendar in 1582,
@@ -187,7 +207,19 @@
 		temp = O_RDWR|O_CREAT;
 	} else temp = O_RDWR;
 
-	// TODO: collect gene2fs list/lost+found, calculate requirements.
+	// Collect gene2fs list or lost+found, calculate requirements.
+
+	if (TT.gendir) {
+		strncpy(toybuf, TT.gendir, sizeof(toybuf));
+		TT.dt = read_dirtree(toybuf, NULL);
+	} else {
+		TT.dt = xzalloc(sizeof(struct dirtree)+11);
+		strcpy(TT.dt->name, "lost+found");
+		TT.dt->st.st_mode = S_IFDIR|0755;
+		TT.dt->st.st_ctime = TT.dt->st.st_mtime = time(NULL);
+	}
+
+	// Calculate st_nlink for each node in tree.
 
 	// TODO: Check if filesystem is mounted here
 
--- a/toys/toylist.h	Sun Feb 04 19:06:07 2007 -0500
+++ b/toys/toylist.h	Sun Feb 04 19:14:58 2007 -0500
@@ -23,10 +23,12 @@
 	long bytes_per_inode;
 	long inodes;
 	long reserved_percent;
+	char *gendir;
 
 	unsigned blocks, groups;
 	int fsfd, noseek;
 	struct ext2_superblock sb;
+	struct dirtree *dt;
 };
 
 // "E:jJ:L:m:O:"