[Tux3] [PATCH] Factor out getting an inode from a path to get_inode, with a flag

Shapor Naghibzadeh shapor at gmail.com
Sun Sep 7 16:19:28 PDT 2008


# HG changeset patch
# User shapor at yzf.shapor.com
# Date 1220829560 25200
# Node ID cdec871e49740f30c1b7432b7e38e07d21201772
# Parent  072b198daa7753aa795e8d0e475e8e6da2df5240
Factor out getting an inode from a path to get_inode, with a flag
to allow creation if file does not exist.

diff -r 072b198daa77 -r cdec871e4974 user/test/tux3fs.c
--- a/user/test/tux3fs.c	Sun Sep 07 15:44:28 2008 -0700
+++ b/user/test/tux3fs.c	Sun Sep 07 16:19:20 2008 -0700
@@ -54,6 +54,29 @@ static struct sb *sb;
 static struct sb *sb;
 static struct dev *dev;
 
+int resolve_path(const char *path, struct inode **dir, char **filename)
+{
+	*dir = sb->rootdir; // !!! subdirectories not supported
+	*filename = (char *)path + 1;
+	warn("resolved '%s': dir inode 0x%Lx, file: '%s'", path, (*dir)->inum, *filename);
+	return 0;
+}
+	
+struct inode *get_inode(const char *path, int create)
+{
+	if (!strcmp(path, "/"))
+		return sb->rootdir;
+	struct inode *inode = tuxopen(sb->rootdir, path + 1, strlen(path + 1));
+	if (inode || !create)
+		return inode;
+	printf("---- create file ----\n");
+	char *filename;
+	resolve_path(path, &inode, &filename);
+	return tuxcreate(sb->rootdir, filename, strlen(filename),
+		&(struct iattr){ .mode = S_IFREG | 0666 });
+	
+}
+
 static int tux3_open(const char *path, struct fuse_file_info *fi)
 {
 	printf("---- open file ----\n");
@@ -66,8 +89,7 @@ static int tux3_read(const char *path, c
 	size_t size, off_t offset, struct fuse_file_info *fi)
 {
 	printf("---- read file ----\n");
-	const char *filename = path + 1;
-	struct inode *inode = tuxopen(sb->rootdir, filename, strlen(filename));
+	struct inode *inode = get_inode(path, 0);
 	struct file *file = &(struct file){ .f_inode = inode };
 	printf("userspace tries to seek to %Li\n", (L)offset);
 	if (offset >= inode->i_size)
@@ -93,28 +115,15 @@ eek:
 
 static int tux3_create(const char *path, mode_t mode, struct fuse_file_info *fi)
 {
-	const char *filename = path + 1;
-	struct inode *inode = tuxopen(sb->rootdir, filename, strlen(filename));
-	if (!inode) {
-		printf("---- create file ----\n");
-		inode = tuxcreate(sb->rootdir, filename, strlen(filename),
-			&(struct iattr){ .mode = S_IFREG | 0666 });
-		if (!inode) return -1;
-	}
-	return 0;
+	struct inode *inode = get_inode(path, 1);
+	return inode ? 0 : -1;
 }
 
 static int tux3_write(const char *path, const char *buf, size_t size,
 	off_t offset, struct fuse_file_info *fi)
 {
 	warn("---- write file ----");
-	const char *filename = path + 1;
-	struct inode *inode = tuxopen(sb->rootdir, filename, strlen(filename));
-	if (!inode) {
-		printf("---- create file ----\n");
-		inode = tuxcreate(sb->rootdir, filename, strlen(filename),
-			&(struct iattr){ .mode = S_IFREG | S_IRWXU });
-	}
+	struct inode *inode = get_inode(path, 1);
 	struct file *file = &(struct file){ .f_inode = inode };
 	if (offset) {
 		u64 seek = offset;
@@ -140,10 +149,8 @@ eek:
 
 static int tux3_getattr(const char *path, struct stat *stat)
 {
-	const char *filename = path + 1;
 	printf("---- get attr for '%s' ----\n", path);
-	struct inode *inode = !strcmp(path, "/") ? sb->rootdir :
-		tuxopen(sb->rootdir, filename, strlen(filename));
+	struct inode *inode = get_inode(path, 0);
 	if (!inode)
 		return -ENOENT;
 	*stat = (struct stat){

_______________________________________________
Tux3 mailing list
Tux3 at tux3.org
http://tux3.org/cgi-bin/mailman/listinfo/tux3



More information about the Tux3 mailing list