Skip to contentSkip to conversation

erofs-utils: dump: Add --cat flag to show file contents

PATCH In review Juan Hernandez 3 participants 5 messages Jan 9, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 erofs-utils: dump: Add --cat flag to show file contents

This patch adds a new '--cat' flag to the 'dump.erofs' command. When used it will write to the standard output the content of the file indicated by the '--path' or '--nid' options. For example, if there is a '/mydir/myfile.txt' file containg the text 'mytext':

    $ dump.erofs --cat --path=/mydir/myfile.txt myimage.erofs
    mytext
---
 dump/main.c      | 72 ++++++++++++++++++++++++++++++++++++++++++++++++
 man/dump.erofs.1 | 13 +++++++--
 2 files changed, 83 insertions(+), 2 deletions(-)
dump/main.c +72 -0
index 372162e..ed8d44d 100644
@@ -26,6 +26,7 @@ struct erofsdump_cfg {
 	bool show_superblock;
 	bool show_statistics;
 	bool show_subdirectories;
+	bool show_file_content;
 	erofs_nid_t nid;
 	const char *inode_path;
 };
@@ -80,6 +81,7 @@ static struct option long_options[] = {
 	{"path", required_argument, NULL, 4},
 	{"ls", no_argument, NULL, 5},
 	{"offset", required_argument, NULL, 6},
+	{"cat", no_argument, NULL, 7},
 	{0, 0, 0, 0},
 };
 
@@ -123,6 +125,7 @@ static void usage(int argc, char **argv)
 		" -s              show information about superblock\n"
 		" --device=X      specify an extra device to be used together\n"
 		" --ls            show directory contents (INODE required)\n"
+		" --cat           show file contents (INODE required)\n"
 		" --nid=#         show the target inode info of nid #\n"
 		" --offset=#      skip # bytes at the beginning of IMAGE\n"
 		" --path=X        show the target inode info of path X\n",
@@ -186,6 +189,9 @@ static int erofsdump_parse_options_cfg(int argc, char **argv)
 				return -EINVAL;
 			}
 			break;
+		case 7:
+			dumpcfg.show_file_content = true;
+			break;
 		default:
 			return -EINVAL;
 		}
@@ -672,6 +678,63 @@ static void erofsdump_show_superblock(void)
 			uuid_str);
 }
 
+static void erofsdump_show_file_content(void)
+{
+	int err;
+	struct erofs_inode inode = { .sbi = &g_sbi, .nid = dumpcfg.nid };
+	size_t buffer_size;
+	char *buffer_ptr;
+	erofs_off_t pending_size;
+	erofs_off_t read_offset;
+	erofs_off_t read_size;
+
+	if (dumpcfg.inode_path) {
+		err = erofs_ilookup(dumpcfg.inode_path, &inode);
+		if (err) {
+			erofs_err("read inode failed @ %s", dumpcfg.inode_path);
+			return;
+		}
+	} else {
+		err = erofs_read_inode_from_disk(&inode);
+		if (err) {
+			erofs_err("read inode failed @ nid %llu", inode.nid | 0ULL);
+			return;
+		}
+	}
+
+	if (!S_ISREG(inode.i_mode)) {
+		erofs_err("not a regular file @ nid %llu", inode.nid | 0ULL);
+		return;
+	}
+
+	buffer_size = erofs_blksiz(inode.sbi);
+	buffer_ptr = malloc(buffer_size);
+	if (!buffer_ptr) {
+		erofs_err("buffer allocation failed @ nid %llu", inode.nid | 0ULL);
+		return;
+	}
+
+	pending_size = inode.i_size;
+	read_offset = 0;
+	while (pending_size > 0) {
+		read_size = pending_size > buffer_size? buffer_size: pending_size;
+		err = erofs_pread(&inode, buffer_ptr, read_size, read_offset);
+		if (err) {
+			erofs_err("read file failed @ nid %llu", inode.nid | 0ULL);
+			goto out;
+		}
+		pending_size -= read_size;
+		read_offset += read_size;
+		fwrite(buffer_ptr, read_size, 1, stdout);
+	}
+	fflush(stdout);
+
+out:
+	free(buffer_ptr);
+}
+
+
+
 int main(int argc, char **argv)
 {
 	int err;
@@ -696,6 +759,15 @@ int main(int argc, char **argv)
 		goto exit_dev_close;
 	}
 
+	if (dumpcfg.show_file_content) {
+		if (dumpcfg.show_superblock || dumpcfg.show_statistics || dumpcfg.show_subdirectories) {
+			fprintf(stderr, "The '--cat' flag is incompatible with '-S', '-e', '-s' and '--ls'.\n");
+			goto exit_dev_close;
+		}
+		erofsdump_show_file_content();
+		goto exit_dev_close;
+	}
+
 	if (!dumpcfg.totalshow) {
 		dumpcfg.show_superblock = true;
 		dumpcfg.totalshow = 1;
man/dump.erofs.1 +11 -2
index 6237ead..ec823ec 100644
@@ -8,7 +8,7 @@ or overall disk statistics information from an EROFS-formatted image.
 \fBdump.erofs\fR [\fIOPTIONS\fR] \fIIMAGE\fR
 .SH DESCRIPTION
 .B dump.erofs
-is used to retrieve erofs metadata from \fIIMAGE\fP and demonstrate
+is used to retrieve erofs metadata and data from \fIIMAGE\fP and demonstrate
 .br
 1) overall disk statistics,
 .br
@@ -16,7 +16,9 @@ is used to retrieve erofs metadata from \fIIMAGE\fP and demonstrate
 .br
 3) file information of the given inode NID,
 .br
-4) file extent information of the given inode NID.
+4) file extent information of the given inode NID,
+.br
+5) file content for the given inode NID.
 .SH OPTIONS
 .TP
 .BI "\-\-device=" path
@@ -32,6 +34,13 @@ or
 .I path
 required.
 .TP
+.BI "\-\-cat"
+Write file content to standard output.
+.I NID
+or
+.I path
+required.
+.TP
 .BI "\-\-nid=" NID
 Specify an inode NID in order to print its file information.
 .TP
Signature · 1 line
2.47.1
#1 lore raw

Hi Juan,

Quoted 9 lines · Juan Hernandez

This patch adds a new '--cat' flag to the 'dump.erofs' command. When used it will write to the standard output the content of the file indicated by the '--path' or '--nid' options. For example, if there is a '/mydir/myfile.txt' file containg the text 'mytext':

     $ dump.erofs --cat --path=/mydir/myfile.txt myimage.erofs
     mytext

Thanks for the patch!
It looks good to me, just minor nits..

Quoted 62 lines
---
  dump/main.c      | 72 ++++++++++++++++++++++++++++++++++++++++++++++++
  man/dump.erofs.1 | 13 +++++++--
  2 files changed, 83 insertions(+), 2 deletions(-)
dump/main.c +21 -0
index 372162e..ed8d44d 100644
@@ -26,6 +26,7 @@ struct erofsdump_cfg {
  	bool show_superblock;
  	bool show_statistics;
  	bool show_subdirectories;
+	bool show_file_content;
  	erofs_nid_t nid;
  	const char *inode_path;
  };
@@ -80,6 +81,7 @@ static struct option long_options[] = {
  	{"path", required_argument, NULL, 4},
  	{"ls", no_argument, NULL, 5},
  	{"offset", required_argument, NULL, 6},
+	{"cat", no_argument, NULL, 7},
  	{0, 0, 0, 0},
  };
 
@@ -123,6 +125,7 @@ static void usage(int argc, char **argv)
  		" -s              show information about superblock\n"
  		" --device=X      specify an extra device to be used together\n"
  		" --ls            show directory contents (INODE required)\n"
+		" --cat           show file contents (INODE required)\n"
  		" --nid=#         show the target inode info of nid #\n"
  		" --offset=#      skip # bytes at the beginning of IMAGE\n"
  		" --path=X        show the target inode info of path X\n",
@@ -186,6 +189,9 @@ static int erofsdump_parse_options_cfg(int argc, char **argv)
  				return -EINVAL;
  			}
  			break;
+		case 7:
+			dumpcfg.show_file_content = true;
+			break;
  		default:
  			return -EINVAL;
  		}
@@ -672,6 +678,63 @@ static void erofsdump_show_superblock(void)
  			uuid_str);
  }
 
+static void erofsdump_show_file_content(void)
+{
+	int err;
+	struct erofs_inode inode = { .sbi = &g_sbi, .nid = dumpcfg.nid };
+	size_t buffer_size;
+	char *buffer_ptr;
+	erofs_off_t pending_size;
+	erofs_off_t read_offset;
+	erofs_off_t read_size;
+
+	if (dumpcfg.inode_path) {
+		err = erofs_ilookup(dumpcfg.inode_path, &inode);
+		if (err) {
+			erofs_err("read inode failed @ %s", dumpcfg.inode_path);
+			return;

dump/main.c static void erofsdump_show_superblock(void) · line 705

Quoted 10 lines
dump/main.c +10 -0
+		}
+	} else {
+		err = erofs_read_inode_from_disk(&inode);
+		if (err) {
+			erofs_err("read inode failed @ nid %llu", inode.nid | 0ULL);
+			return;
+		}
+	}
+
+	if (!S_ISREG(inode.i_mode)) {

I think we could dump raw directory content too.

Quoted 22 lines
dump/main.c +22 -0
+		erofs_err("not a regular file @ nid %llu", inode.nid | 0ULL);
+		return;
+	}
+
+	buffer_size = erofs_blksiz(inode.sbi);
+	buffer_ptr = malloc(buffer_size);
+	if (!buffer_ptr) {
+		erofs_err("buffer allocation failed @ nid %llu", inode.nid | 0ULL);
+		return;
+	}
+
+	pending_size = inode.i_size;
+	read_offset = 0;
+	while (pending_size > 0) {
+		read_size = pending_size > buffer_size? buffer_size: pending_size;
+		err = erofs_pread(&inode, buffer_ptr, read_size, read_offset);
+		if (err) {
+			erofs_err("read file failed @ nid %llu", inode.nid | 0ULL);
+			goto out;
+		}
+		pending_size -= read_size;
+		read_offset += read_size;

dump/main.c static void erofsdump_show_superblock(void) · line 737

Quoted 10 lines
dump/main.c +10 -0
+		fwrite(buffer_ptr, read_size, 1, stdout);
+	}
+	fflush(stdout);
+
+out:
+	free(buffer_ptr);
+}
+
+
+

Reduntant new lines..

Thanks,
Gao Xiang

#2 lore raw

Thanks for your feedback Gao! I will address your concerns.

Quoted 131 lines · Gao Xiang

Hi Juan,

Quoted 9 lines · Juan Hernandez

This patch adds a new '--cat' flag to the 'dump.erofs' command. When used it will write to the standard output the content of the file indicated by the '--path' or '--nid' options. For example, if there is a '/mydir/myfile.txt' file containg the text 'mytext':

     $ dump.erofs --cat --path=/mydir/myfile.txt myimage.erofs
     mytext

Thanks for the patch!
It looks good to me, just minor nits..

Quoted 62 lines
---
  dump/main.c      | 72 ++++++++++++++++++++++++++++++++++++++++++++++++
  man/dump.erofs.1 | 13 +++++++--
  2 files changed, 83 insertions(+), 2 deletions(-)
dump/main.c +21 -0
index 372162e..ed8d44d 100644
@@ -26,6 +26,7 @@ struct erofsdump_cfg {
      bool show_superblock;
      bool show_statistics;
      bool show_subdirectories;
+     bool show_file_content;
      erofs_nid_t nid;
      const char *inode_path;
  };
@@ -80,6 +81,7 @@ static struct option long_options[] = {
      {"path", required_argument, NULL, 4},
      {"ls", no_argument, NULL, 5},
      {"offset", required_argument, NULL, 6},
+     {"cat", no_argument, NULL, 7},
      {0, 0, 0, 0},
  };
 
@@ -123,6 +125,7 @@ static void usage(int argc, char **argv)
              " -s              show information about superblock\n"
              " --device=X      specify an extra device to be used together\n"
              " --ls            show directory contents (INODE required)\n"
+             " --cat           show file contents (INODE required)\n"
              " --nid=#         show the target inode info of nid #\n"
              " --offset=#      skip # bytes at the beginning of IMAGE\n"
              " --path=X        show the target inode info of path X\n",
@@ -186,6 +189,9 @@ static int erofsdump_parse_options_cfg(int argc, char **argv)
                              return -EINVAL;
                      }
                      break;
+             case 7:
+                     dumpcfg.show_file_content = true;
+                     break;
              default:
                      return -EINVAL;
              }
@@ -672,6 +678,63 @@ static void erofsdump_show_superblock(void)
                      uuid_str);
  }
 
+static void erofsdump_show_file_content(void)
+{
+     int err;
+     struct erofs_inode inode = { .sbi = &g_sbi, .nid = dumpcfg.nid };
+     size_t buffer_size;
+     char *buffer_ptr;
+     erofs_off_t pending_size;
+     erofs_off_t read_offset;
+     erofs_off_t read_size;
+
+     if (dumpcfg.inode_path) {
+             err = erofs_ilookup(dumpcfg.inode_path, &inode);
+             if (err) {
+                     erofs_err("read inode failed @ %s", dumpcfg.inode_path);
+                     return;

dump/main.c static void erofsdump_show_superblock(void) · line 705

Quoted 10 lines
dump/main.c +10 -0
+             }
+     } else {
+             err = erofs_read_inode_from_disk(&inode);
+             if (err) {
+                     erofs_err("read inode failed @ nid %llu", inode.nid | 0ULL);
+                     return;
+             }
+     }
+
+     if (!S_ISREG(inode.i_mode)) {

I think we could dump raw directory content too.

Quoted 22 lines
dump/main.c +22 -0
+             erofs_err("not a regular file @ nid %llu", inode.nid | 0ULL);
+             return;
+     }
+
+     buffer_size = erofs_blksiz(inode.sbi);
+     buffer_ptr = malloc(buffer_size);
+     if (!buffer_ptr) {
+             erofs_err("buffer allocation failed @ nid %llu", inode.nid | 0ULL);
+             return;
+     }
+
+     pending_size = inode.i_size;
+     read_offset = 0;
+     while (pending_size > 0) {
+             read_size = pending_size > buffer_size? buffer_size: pending_size;
+             err = erofs_pread(&inode, buffer_ptr, read_size, read_offset);
+             if (err) {
+                     erofs_err("read file failed @ nid %llu", inode.nid | 0ULL);
+                     goto out;
+             }
+             pending_size -= read_size;
+             read_offset += read_size;

dump/main.c static void erofsdump_show_superblock(void) · line 737

Quoted 10 lines
dump/main.c +10 -0
+             fwrite(buffer_ptr, read_size, 1, stdout);
+     }
+     fflush(stdout);
+
+out:
+     free(buffer_ptr);
+}
+
+
+

Reduntant new lines..

Thanks,
Gao Xiang

#3 lore raw

PATCH 1/1 erofs-utils: dump: Add --cat flag to show file contents

This patch adds a new '--cat' flag to the 'dump.erofs' command. When used it will write to the standard output the content of the file indicated by the '--path' or '--nid' options. For example, if there is a '/mydir/myfile.txt' file containg the text 'mytext':

    $ dump.erofs --cat --path=/mydir/myfile.txt myimage.erofs
    mytext
---
 dump/main.c      | 65 ++++++++++++++++++++++++++++++++++++++++++++++++
 man/dump.erofs.1 | 13 ++++++++--
 2 files changed, 76 insertions(+), 2 deletions(-)
dump/main.c +65 -0
index 372162e..860ee5a 100644
@@ -26,6 +26,7 @@ struct erofsdump_cfg {
 	bool show_superblock;
 	bool show_statistics;
 	bool show_subdirectories;
+	bool show_file_content;
 	erofs_nid_t nid;
 	const char *inode_path;
 };
@@ -80,6 +81,7 @@ static struct option long_options[] = {
 	{"path", required_argument, NULL, 4},
 	{"ls", no_argument, NULL, 5},
 	{"offset", required_argument, NULL, 6},
+	{"cat", no_argument, NULL, 7},
 	{0, 0, 0, 0},
 };
 
@@ -123,6 +125,7 @@ static void usage(int argc, char **argv)
 		" -s              show information about superblock\n"
 		" --device=X      specify an extra device to be used together\n"
 		" --ls            show directory contents (INODE required)\n"
+		" --cat           show file contents (INODE required)\n"
 		" --nid=#         show the target inode info of nid #\n"
 		" --offset=#      skip # bytes at the beginning of IMAGE\n"
 		" --path=X        show the target inode info of path X\n",
@@ -186,6 +189,9 @@ static int erofsdump_parse_options_cfg(int argc, char **argv)
 				return -EINVAL;
 			}
 			break;
+		case 7:
+			dumpcfg.show_file_content = true;
+			break;
 		default:
 			return -EINVAL;
 		}
@@ -672,6 +678,56 @@ static void erofsdump_show_superblock(void)
 			uuid_str);
 }
 
+static void erofsdump_show_file_content(void)
+{
+	int err;
+	struct erofs_inode inode = { .sbi = &g_sbi, .nid = dumpcfg.nid };
+	size_t buffer_size;
+	char *buffer_ptr;
+	erofs_off_t pending_size;
+	erofs_off_t read_offset;
+	erofs_off_t read_size;
+
+	if (dumpcfg.inode_path) {
+		err = erofs_ilookup(dumpcfg.inode_path, &inode);
+		if (err) {
+			erofs_err("read inode failed @ %s", dumpcfg.inode_path);
+			return;
+		}
+	} else {
+		err = erofs_read_inode_from_disk(&inode);
+		if (err) {
+			erofs_err("read inode failed @ nid %llu", inode.nid | 0ULL);
+			return;
+		}
+	}
+
+	buffer_size = erofs_blksiz(inode.sbi);
+	buffer_ptr = malloc(buffer_size);
+	if (!buffer_ptr) {
+		erofs_err("buffer allocation failed @ nid %llu", inode.nid | 0ULL);
+		return;
+	}
+
+	pending_size = inode.i_size;
+	read_offset = 0;
+	while (pending_size > 0) {
+		read_size = pending_size > buffer_size? buffer_size: pending_size;
+		err = erofs_pread(&inode, buffer_ptr, read_size, read_offset);
+		if (err) {
+			erofs_err("read file failed @ nid %llu", inode.nid | 0ULL);
+			goto out;
+		}
+		pending_size -= read_size;
+		read_offset += read_size;
+		fwrite(buffer_ptr, read_size, 1, stdout);
+	}
+	fflush(stdout);
+
+out:
+	free(buffer_ptr);
+}
+
 int main(int argc, char **argv)
 {
 	int err;
@@ -696,6 +752,15 @@ int main(int argc, char **argv)
 		goto exit_dev_close;
 	}
 
+	if (dumpcfg.show_file_content) {
+		if (dumpcfg.show_superblock || dumpcfg.show_statistics || dumpcfg.show_subdirectories) {
+			fprintf(stderr, "The '--cat' flag is incompatible with '-S', '-e', '-s' and '--ls'.\n");
+			goto exit_dev_close;
+		}
+		erofsdump_show_file_content();
+		goto exit_dev_close;
+	}
+
 	if (!dumpcfg.totalshow) {
 		dumpcfg.show_superblock = true;
 		dumpcfg.totalshow = 1;
man/dump.erofs.1 +11 -2
index 6237ead..ec823ec 100644
@@ -8,7 +8,7 @@ or overall disk statistics information from an EROFS-formatted image.
 \fBdump.erofs\fR [\fIOPTIONS\fR] \fIIMAGE\fR
 .SH DESCRIPTION
 .B dump.erofs
-is used to retrieve erofs metadata from \fIIMAGE\fP and demonstrate
+is used to retrieve erofs metadata and data from \fIIMAGE\fP and demonstrate
 .br
 1) overall disk statistics,
 .br
@@ -16,7 +16,9 @@ is used to retrieve erofs metadata from \fIIMAGE\fP and demonstrate
 .br
 3) file information of the given inode NID,
 .br
-4) file extent information of the given inode NID.
+4) file extent information of the given inode NID,
+.br
+5) file content for the given inode NID.
 .SH OPTIONS
 .TP
 .BI "\-\-device=" path
@@ -32,6 +34,13 @@ or
 .I path
 required.
 .TP
+.BI "\-\-cat"
+Write file content to standard output.
+.I NID
+or
+.I path
+required.
+.TP
 .BI "\-\-nid=" NID
 Specify an inode NID in order to print its file information.
 .TP
Signature · 1 line
2.47.1
#4 lore raw
Quoted 9 lines · Juan Hernandez

This patch adds a new '--cat' flag to the 'dump.erofs' command. When used it will write to the standard output the content of the file indicated by the '--path' or '--nid' options. For example, if there is a '/mydir/myfile.txt' file containg the text 'mytext':

    $ dump.erofs --cat --path=/mydir/myfile.txt myimage.erofs
    mytext

Thanks! Applied now for testing.

Thanks,
Gao Xiang