Skip to contentSkip to conversation

Add API to get full pathname of an inode

PATCH New Kelvin Zhang via Linux-erofs 3 participants 20 messages Dec 9, 2021 – Dec 15, 2021 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Add API to get full pathname of an inode

Change-Id Iba99e590f051638285d1d1949311a08f5a5f1a82
---
 dump/main.c              |  70 ++-----------------------
 include/erofs/internal.h |   5 ++
 lib/namei.c              | 108 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 116 insertions(+), 67 deletions(-)
dump/main.c +3 -67
index b7560ec..bcaaa21 100644
@@ -11,6 +11,7 @@
 #include "erofs/print.h"
 #include "erofs/inode.h"
 #include "erofs/io.h"
+#include "erofs/internal.h"
 
 #ifdef HAVE_LIBUUID
 #include <uuid.h>
@@ -357,71 +358,6 @@ static int erofs_read_dir(erofs_nid_t nid, erofs_nid_t parent_nid)
 	return 0;
 }
 
-static int erofs_get_pathname(erofs_nid_t nid, erofs_nid_t parent_nid,
-		erofs_nid_t target, char *path, unsigned int pos)
-{
-	int err;
-	erofs_off_t offset;
-	char buf[EROFS_BLKSIZ];
-	struct erofs_inode inode = { .nid = nid };
-
-	path[pos++] = '/';
-	if (target == sbi.root_nid)
-		return 0;
-
-	err = erofs_read_inode_from_disk(&inode);
-	if (err) {
-		erofs_err("read inode failed @ nid %llu", nid | 0ULL);
-		return err;
-	}
-
-	offset = 0;
-	while (offset < inode.i_size) {
-		erofs_off_t maxsize = min_t(erofs_off_t,
-					inode.i_size - offset, EROFS_BLKSIZ);
-		struct erofs_dirent *de = (void *)buf;
-		struct erofs_dirent *end;
-		unsigned int nameoff;
-
-		err = erofs_pread(&inode, buf, maxsize, offset);
-		if (err)
-			return err;
-
-		nameoff = le16_to_cpu(de->nameoff);
-		end = (void *)buf + nameoff;
-		while (de < end) {
-			const char *dname;
-			int len;
-
-			nameoff = le16_to_cpu(de->nameoff);
-			dname = (char *)buf + nameoff;
-			len = erofs_checkdirent(de, end, maxsize, dname);
-			if (len < 0)
-				return len;
-
-			if (de->nid == target) {
-				memcpy(path + pos, dname, len);
-				path[pos + len] = '\0';
-				return 0;
-			}
-
-			if (de->file_type == EROFS_FT_DIR &&
-					de->nid != parent_nid &&
-					de->nid != nid) {
-				memcpy(path + pos, dname, len);
-				err = erofs_get_pathname(de->nid, nid,
-						target, path, pos + len);
-				if (!err)
-					return 0;
-				memset(path + pos, 0, len);
-			}
-			++de;
-		}
-		offset += maxsize;
-	}
-	return -1;
-}
-
 static int erofsdump_map_blocks(struct erofs_inode *inode,
 		struct erofs_map_blocks *map, int flags)
 {
@@ -461,12 +397,12 @@ static void erofsdump_show_fileinfo(bool show_extent)
 		return;
 	}
 
-	err = erofs_get_pathname(sbi.root_nid, sbi.root_nid,
-				 inode.nid, path, 0);
+	err = erofs_get_inode_name(&sbi, inode.nid, path, PATH_MAX+1);
 	if (err < 0) {
 		erofs_err("file path not found @ nid %llu", inode.nid | 0ULL);
 		return;
 	}
+  printf("Path: %s\n", path);
 
 	strftime(timebuf, sizeof(timebuf),
 		 "%Y-%m-%d %H:%M:%S", localtime((time_t *)&inode.i_ctime));
include/erofs/internal.h +5 -0
index a68de32..632461e 100644
@@ -305,6 +305,11 @@ int erofs_read_superblock(void);
 int erofs_read_inode_from_disk(struct erofs_inode *vi);
 int erofs_ilookup(const char *path, struct erofs_inode *vi);
 int erofs_read_inode_from_disk(struct erofs_inode *vi);
+// Get full path name of an inode, return 0 if success
+int erofs_get_inode_name(const struct erofs_sb_info* sbi,
+                         erofs_nid_t target,
+                         char* output_buffer,
+                         size_t capacity);
 
 /* data.c */
 int erofs_pread(struct erofs_inode *inode, char *buf,
lib/namei.c +108 -0

111 lines not on this page · raw message

Signature · 1 line
2.34.1.173.g76aa8bc2d0-goog
#1 lore raw

PATCH 1/1 Add API to get full pathname of an inode

Change-Id Iba99e590f051638285d1d1949311a08f5a5f1a82
---
 dump/main.c              |  70 ++-----------------------
 include/erofs/internal.h |   5 ++
 lib/namei.c              | 108 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 116 insertions(+), 67 deletions(-)
dump/main.c +3 -67
index b7560ec..bcaaa21 100644
@@ -11,6 +11,7 @@
 #include "erofs/print.h"
 #include "erofs/inode.h"
 #include "erofs/io.h"
+#include "erofs/internal.h"
 
 #ifdef HAVE_LIBUUID
 #include <uuid.h>
@@ -357,71 +358,6 @@ static int erofs_read_dir(erofs_nid_t nid, erofs_nid_t parent_nid)
 	return 0;
 }
 
-static int erofs_get_pathname(erofs_nid_t nid, erofs_nid_t parent_nid,
-		erofs_nid_t target, char *path, unsigned int pos)
-{
-	int err;
-	erofs_off_t offset;
-	char buf[EROFS_BLKSIZ];
-	struct erofs_inode inode = { .nid = nid };
-
-	path[pos++] = '/';
-	if (target == sbi.root_nid)
-		return 0;
-
-	err = erofs_read_inode_from_disk(&inode);
-	if (err) {
-		erofs_err("read inode failed @ nid %llu", nid | 0ULL);
-		return err;
-	}
-
-	offset = 0;
-	while (offset < inode.i_size) {
-		erofs_off_t maxsize = min_t(erofs_off_t,
-					inode.i_size - offset, EROFS_BLKSIZ);
-		struct erofs_dirent *de = (void *)buf;
-		struct erofs_dirent *end;
-		unsigned int nameoff;
-
-		err = erofs_pread(&inode, buf, maxsize, offset);
-		if (err)
-			return err;
-
-		nameoff = le16_to_cpu(de->nameoff);
-		end = (void *)buf + nameoff;
-		while (de < end) {
-			const char *dname;
-			int len;
-
-			nameoff = le16_to_cpu(de->nameoff);
-			dname = (char *)buf + nameoff;
-			len = erofs_checkdirent(de, end, maxsize, dname);
-			if (len < 0)
-				return len;
-
-			if (de->nid == target) {
-				memcpy(path + pos, dname, len);
-				path[pos + len] = '\0';
-				return 0;
-			}
-
-			if (de->file_type == EROFS_FT_DIR &&
-					de->nid != parent_nid &&
-					de->nid != nid) {
-				memcpy(path + pos, dname, len);
-				err = erofs_get_pathname(de->nid, nid,
-						target, path, pos + len);
-				if (!err)
-					return 0;
-				memset(path + pos, 0, len);
-			}
-			++de;
-		}
-		offset += maxsize;
-	}
-	return -1;
-}
-
 static int erofsdump_map_blocks(struct erofs_inode *inode,
 		struct erofs_map_blocks *map, int flags)
 {
@@ -461,12 +397,12 @@ static void erofsdump_show_fileinfo(bool show_extent)
 		return;
 	}
 
-	err = erofs_get_pathname(sbi.root_nid, sbi.root_nid,
-				 inode.nid, path, 0);
+	err = erofs_get_inode_name(&sbi, inode.nid, path, PATH_MAX+1);
 	if (err < 0) {
 		erofs_err("file path not found @ nid %llu", inode.nid | 0ULL);
 		return;
 	}
+  printf("Path: %s\n", path);
 
 	strftime(timebuf, sizeof(timebuf),
 		 "%Y-%m-%d %H:%M:%S", localtime((time_t *)&inode.i_ctime));
include/erofs/internal.h +5 -0
index a68de32..632461e 100644
@@ -305,6 +305,11 @@ int erofs_read_superblock(void);
 int erofs_read_inode_from_disk(struct erofs_inode *vi);
 int erofs_ilookup(const char *path, struct erofs_inode *vi);
 int erofs_read_inode_from_disk(struct erofs_inode *vi);
+// Get full path name of an inode, return 0 if success
+int erofs_get_inode_name(const struct erofs_sb_info* sbi,
+                         erofs_nid_t target,
+                         char* output_buffer,
+                         size_t capacity);
 
 /* data.c */
 int erofs_pread(struct erofs_inode *inode, char *buf,
lib/namei.c +108 -0

111 lines not on this page · raw message

Signature · 1 line
2.34.1.173.g76aa8bc2d0-goog
#2 lore raw

PATCH v2 1/2 Add API to get full pathname of an inode

Change-Id Iba99e590f051638285d1d1949311a08f5a5f1a82
---
 dump/main.c              |  70 ++-----------------------
 include/erofs/internal.h |   5 ++
 lib/namei.c              | 108 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 116 insertions(+), 67 deletions(-)
dump/main.c +3 -67
index b7560ec..bcaaa21 100644
@@ -11,6 +11,7 @@
 #include "erofs/print.h"
 #include "erofs/inode.h"
 #include "erofs/io.h"
+#include "erofs/internal.h"
 
 #ifdef HAVE_LIBUUID
 #include <uuid.h>
@@ -357,71 +358,6 @@ static int erofs_read_dir(erofs_nid_t nid, erofs_nid_t parent_nid)
 	return 0;
 }
 
-static int erofs_get_pathname(erofs_nid_t nid, erofs_nid_t parent_nid,
-		erofs_nid_t target, char *path, unsigned int pos)
-{
-	int err;
-	erofs_off_t offset;
-	char buf[EROFS_BLKSIZ];
-	struct erofs_inode inode = { .nid = nid };
-
-	path[pos++] = '/';
-	if (target == sbi.root_nid)
-		return 0;
-
-	err = erofs_read_inode_from_disk(&inode);
-	if (err) {
-		erofs_err("read inode failed @ nid %llu", nid | 0ULL);
-		return err;
-	}
-
-	offset = 0;
-	while (offset < inode.i_size) {
-		erofs_off_t maxsize = min_t(erofs_off_t,
-					inode.i_size - offset, EROFS_BLKSIZ);
-		struct erofs_dirent *de = (void *)buf;
-		struct erofs_dirent *end;
-		unsigned int nameoff;
-
-		err = erofs_pread(&inode, buf, maxsize, offset);
-		if (err)
-			return err;
-
-		nameoff = le16_to_cpu(de->nameoff);
-		end = (void *)buf + nameoff;
-		while (de < end) {
-			const char *dname;
-			int len;
-
-			nameoff = le16_to_cpu(de->nameoff);
-			dname = (char *)buf + nameoff;
-			len = erofs_checkdirent(de, end, maxsize, dname);
-			if (len < 0)
-				return len;
-
-			if (de->nid == target) {
-				memcpy(path + pos, dname, len);
-				path[pos + len] = '\0';
-				return 0;
-			}
-
-			if (de->file_type == EROFS_FT_DIR &&
-					de->nid != parent_nid &&
-					de->nid != nid) {
-				memcpy(path + pos, dname, len);
-				err = erofs_get_pathname(de->nid, nid,
-						target, path, pos + len);
-				if (!err)
-					return 0;
-				memset(path + pos, 0, len);
-			}
-			++de;
-		}
-		offset += maxsize;
-	}
-	return -1;
-}
-
 static int erofsdump_map_blocks(struct erofs_inode *inode,
 		struct erofs_map_blocks *map, int flags)
 {
@@ -461,12 +397,12 @@ static void erofsdump_show_fileinfo(bool show_extent)
 		return;
 	}
 
-	err = erofs_get_pathname(sbi.root_nid, sbi.root_nid,
-				 inode.nid, path, 0);
+	err = erofs_get_inode_name(&sbi, inode.nid, path, PATH_MAX+1);
 	if (err < 0) {
 		erofs_err("file path not found @ nid %llu", inode.nid | 0ULL);
 		return;
 	}
+  printf("Path: %s\n", path);
 
 	strftime(timebuf, sizeof(timebuf),
 		 "%Y-%m-%d %H:%M:%S", localtime((time_t *)&inode.i_ctime));
include/erofs/internal.h +5 -0
index a68de32..632461e 100644
@@ -305,6 +305,11 @@ int erofs_read_superblock(void);
 int erofs_read_inode_from_disk(struct erofs_inode *vi);
 int erofs_ilookup(const char *path, struct erofs_inode *vi);
 int erofs_read_inode_from_disk(struct erofs_inode *vi);
+// Get full path name of an inode, return 0 if success
+int erofs_get_inode_name(const struct erofs_sb_info* sbi,
+                         erofs_nid_t target,
+                         char* output_buffer,
+                         size_t capacity);
 
 /* data.c */
 int erofs_pread(struct erofs_inode *inode, char *buf,
lib/namei.c +108 -0

111 lines not on this page · raw message

Signature · 1 line
2.34.1.173.g76aa8bc2d0-goog
#3 lore raw

PATCH v2 2/2 Add API to iterate over inodes in EROFS

In reply to Kelvin Zhang via Linux-erofs
Change-Id Ia35708080a72ee204eaaddfc670d3cb8023a078c
---
 include/erofs/iterate.h |  57 +++++++++++++
 lib/Makefile.am         |   2 +-
 lib/iterate.c           | 173 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 231 insertions(+), 1 deletion(-)
 create mode 100644 include/erofs/iterate.h
 create mode 100644 lib/iterate.c
include/erofs/iterate.h new file +57 -0
new file mode 100644
index 0000000..96171a7
@@ -0,0 +1,57 @@
+//
+// Copyright (C) 2021 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+#ifndef ITERATE_ITERATE
+#define ITERATE_ITERATE
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+
+#include "erofs/io.h"
+#include "erofs/print.h"
+
+
+struct erofs_inode_info {
+  uint64_t inode_id;
+  const char* name;
+  bool is_reg_file;
+  u64 compressed_size;
+  u64 uncompressed_size;
+  struct erofs_inode* inode;
+  void* arg;
+};
+// Callback function for iterating over inodes of EROFS
+
+typedef bool (*ErofsIterCallback)(struct erofs_inode_info);
+
+int erofs_iterate_dir(const struct erofs_sb_info* sbi,
+                   erofs_nid_t nid,
+                   erofs_nid_t parent_nid,
+                   ErofsIterCallback cb,
+                   void* arg);
+int erofs_iterate_root_dir(const struct erofs_sb_info* sbi,
+                        ErofsIterCallback cbg,
+                        void* arg);
+int erofs_get_occupied_size(struct erofs_inode* inode, erofs_off_t* size);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif  // ITERATE_ITERATE
lib/Makefile.am +1 -1
index 67ba798..20c0e4f 100644
@@ -27,7 +27,7 @@ noinst_HEADERS = $(top_srcdir)/include/erofs_fs.h \
 noinst_HEADERS += compressor.h
 liberofs_la_SOURCES = config.c io.c cache.c super.c inode.c xattr.c exclude.c \
 		      namei.c data.c compress.c compressor.c zmap.c decompress.c \
-		      compress_hints.c hashmap.c sha256.c blobchunk.c
+		      compress_hints.c hashmap.c sha256.c blobchunk.c iterate.c
 liberofs_la_CFLAGS = -Wall -Werror -I$(top_srcdir)/include
 if ENABLE_LZ4
 liberofs_la_CFLAGS += ${LZ4_CFLAGS}
lib/iterate.c new file +173 -0

173 lines not on this page · raw message

Signature · 1 line
2.34.1.173.g76aa8bc2d0-goog
#4 lore raw

Re: [PATCH v2 2/2] Add API to iterate over inodes in EROFS

Friendly ping

Quoted 277 lines · Kelvin Zhang
Change-Id Ia35708080a72ee204eaaddfc670d3cb8023a078c
---
 include/erofs/iterate.h |  57 +++++++++++++
 lib/Makefile.am         |   2 +-
 lib/iterate.c           | 173 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 231 insertions(+), 1 deletion(-)
 create mode 100644 include/erofs/iterate.h
 create mode 100644 lib/iterate.c
include/erofs/iterate.h new file +12 -0
new file mode 100644
index 0000000..96171a7
@@ -0,0 +1,57 @@
+//
+// Copyright (C) 2021 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or

implied.

include/erofs/iterate.h new file +45 -0
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+#ifndef ITERATE_ITERATE
+#define ITERATE_ITERATE
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+
+#include "erofs/io.h"
+#include "erofs/print.h"
+
+
+struct erofs_inode_info {
+  uint64_t inode_id;
+  const char* name;
+  bool is_reg_file;
+  u64 compressed_size;
+  u64 uncompressed_size;
+  struct erofs_inode* inode;
+  void* arg;
+};
+// Callback function for iterating over inodes of EROFS
+
+typedef bool (*ErofsIterCallback)(struct erofs_inode_info);
+
+int erofs_iterate_dir(const struct erofs_sb_info* sbi,
+                   erofs_nid_t nid,
+                   erofs_nid_t parent_nid,
+                   ErofsIterCallback cb,
+                   void* arg);
+int erofs_iterate_root_dir(const struct erofs_sb_info* sbi,
+                        ErofsIterCallback cbg,
+                        void* arg);
+int erofs_get_occupied_size(struct erofs_inode* inode, erofs_off_t* size);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif  // ITERATE_ITERATE
lib/Makefile.am +1 -1
index 67ba798..20c0e4f 100644
@@ -27,7 +27,7 @@ noinst_HEADERS = $(top_srcdir)/include/erofs_fs.h \
 noinst_HEADERS += compressor.h
 liberofs_la_SOURCES = config.c io.c cache.c super.c inode.c xattr.c
 exclude.c \
                      namei.c data.c compress.c compressor.c zmap.c
 decompress.c \
-                     compress_hints.c hashmap.c sha256.c blobchunk.c
+                     compress_hints.c hashmap.c sha256.c blobchunk.c

iterate.c
liberofs_la_CFLAGS = -Wall -Werror -I$(top_srcdir)/include if ENABLE_LZ4
liberofs_la_CFLAGS += ${LZ4_CFLAGS}

lib/iterate.c new file +12 -0
new file mode 100644
index 0000000..1a10ec1
@@ -0,0 +1,173 @@
+//
+// Copyright (C) 2021 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or

implied.

lib/iterate.c new file +60 -0
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+
+#include "erofs/internal.h"
+#include "erofs_fs.h"
+#include "erofs/print.h"
+#include "erofs/iterate.h"
+
+static int erofs_read_dirent(const struct erofs_sb_info* sbi,
+                             const struct erofs_dirent* de,
+                             erofs_nid_t nid,
+                             erofs_nid_t parent_nid,
+                             const char* dname,
+                             ErofsIterCallback cb,
+                             void* arg) {
+  int err;
+  erofs_off_t occupied_size = 0;
+  struct erofs_inode inode = {.nid = de->nid};
+  err = erofs_read_inode_from_disk(&inode);
+  if (err) {
+    erofs_err("read file inode from disk failed!");
+    return err;
+  }
+  err = erofs_get_occupied_size(&inode, &occupied_size);
+  if (err) {
+    erofs_err("get file size failed\n");
+    return err;
+  }
+  char buf[PATH_MAX + 1];
+  erofs_get_inode_name(sbi, de->nid, buf, PATH_MAX + 1);
+  struct erofs_inode_info info = {
+      .inode_id = de->nid,
+      .name = buf,
+      .is_reg_file = de->file_type == EROFS_FT_REG_FILE,
+      .compressed_size = occupied_size,
+      .uncompressed_size = inode.i_size,
+      .inode = &inode,
+      .arg = arg};
+  cb(info);
+  if ((de->file_type == EROFS_FT_DIR) && de->nid != nid &&
+      de->nid != parent_nid) {
+    err = erofs_iterate_dir(sbi, de->nid, nid, cb, arg);
+    if (err) {
+      erofs_err("parse dir nid %u error occurred\n",
+                (unsigned int)(de->nid));
+      return err;
+    }
+  }
+  return 0;
+}
+
+static inline int erofs_checkdirent(const struct erofs_dirent* de,
+                                    const struct erofs_dirent* last_de,
+                                    u32 maxsize,
+                                    const char* dname) {
+  int dname_len;
+  unsigned int nameoff = le16_to_cpu(de->nameoff);
+  if (nameoff < sizeof(struct erofs_dirent) || nameoff >= PAGE_SIZE) {
+    erofs_err("invalid de[0].nameoff %u @ nid %llu", nameoff, de->nid |

0ULL);

lib/iterate.c new file +46 -0
+    return -EFSCORRUPTED;
+  }
+  dname_len = (de + 1 >= last_de) ? strnlen(dname, maxsize - nameoff)
+                                  : le16_to_cpu(de[1].nameoff) - nameoff;
+  /* a corrupted entry is found */
+  if (nameoff + dname_len > maxsize || dname_len > EROFS_NAME_LEN) {
+    erofs_err("bogus dirent @ nid %llu", le64_to_cpu(de->nid) | 0ULL);
+    DBG_BUGON(1);
+    return -EFSCORRUPTED;
+  }
+  if (de->file_type >= EROFS_FT_MAX) {
+    erofs_err("invalid file type %u", (unsigned int)(de->nid));
+    return -EFSCORRUPTED;
+  }
+  return dname_len;
+}
+
+int erofs_iterate_dir(const struct erofs_sb_info* sbi,
+                   erofs_nid_t nid,
+                   erofs_nid_t parent_nid,
+                   ErofsIterCallback cb,
+                   void* arg) {
+  int err;
+  erofs_off_t offset;
+  char buf[EROFS_BLKSIZ];
+  struct erofs_inode vi = {.nid = nid};
+  err = erofs_read_inode_from_disk(&vi);
+  if (err)
+    return err;
+  struct erofs_inode_info inode_info = {
+      .inode_id = nid,
+      .name = buf,
+      .is_reg_file = false,
+      .compressed_size = vi.i_size,
+      .uncompressed_size = vi.i_size,
+      .inode = &vi,
+      .arg = arg,
+  };
+  err = erofs_get_inode_name(sbi, nid, buf, EROFS_BLKSIZ);
+  cb(inode_info);
+  if (err) {
+    return err;
+  }
+  offset = 0;
+  while (offset < vi.i_size) {
+    erofs_off_t maxsize = min_t(erofs_off_t, vi.i_size - offset,

EROFS_BLKSIZ);

lib/iterate.c new file +31 -0
+    const struct erofs_dirent* de = (const struct erofs_dirent*)(buf);
+    struct erofs_dirent* end;
+    unsigned int nameoff;
+    err = erofs_pread(&vi, buf, maxsize, offset);
+    if (err)
+      return err;
+    nameoff = le16_to_cpu(de->nameoff);
+    end = (struct erofs_dirent*)(buf + nameoff);
+    while (de < end) {
+      const char* dname;
+      int ret;
+      /* skip "." and ".." dentry */
+      if (de->nid == nid || de->nid == parent_nid) {
+        de++;
+        continue;
+      }
+      dname = (char*)buf + nameoff;
+      ret = erofs_checkdirent(de, end, maxsize, dname);
+      if (ret < 0)
+        return ret;
+      ret = erofs_read_dirent(sbi, de, nid, parent_nid, dname, cb, arg);
+      if (ret < 0)
+        return ret;
+      ++de;
+    }
+    offset += maxsize;
+  }
+  return 0;
+}
+
+int erofs_get_occupied_size(struct erofs_inode* inode, erofs_off_t* size)

{

lib/iterate.c new file +24 -1
+  *size = 0;
+  switch (inode->datalayout) {
+    case EROFS_INODE_FLAT_INLINE:
+    case EROFS_INODE_FLAT_PLAIN:
+    case EROFS_INODE_CHUNK_BASED:
+      *size = inode->i_size;
+      break;
+    case EROFS_INODE_FLAT_COMPRESSION_LEGACY:
+    case EROFS_INODE_FLAT_COMPRESSION:
+      *size = inode->u.i_blocks * EROFS_BLKSIZ;
+      break;
+    default:
+      erofs_err("unknown datalayout");
+      return -1;
+  }
+  return 0;
+}
+
+int erofs_iterate_root_dir(const struct erofs_sb_info* sbi,
+                        ErofsIterCallback cb,
+                        void* arg) {
+  return erofs_iterate_dir(sbi, sbi->root_nid, sbi->root_nid, cb, arg);
+}
+
--

2.34.1.173.g76aa8bc2d0-goog

Signature · 3 lines
Sincerely,

Kelvin Zhang
#5 lore raw

Re: [PATCH v2 2/2] Add API to iterate over inodes in EROFS

Hi Kelvin,

Quoted 1 line · Kelvin Zhang

Friendly ping

Sorry for the delay. I didn't mean to ignore this patch.

Yet actually as I once said to Daeho, it would be better to introduce a generic callback iterate function and convert fuse/dump/fsck in one patchset.

you could update some coding-style issues as erofs-utils follows Linux kernel style.

Quoted 27 lines · Kelvin Zhang
Quoted 25 lines
Change-Id Ia35708080a72ee204eaaddfc670d3cb8023a078c
---
 include/erofs/iterate.h |  57 +++++++++++++
 lib/Makefile.am         |   2 +-
 lib/iterate.c           | 173 ++++++++++++++++++++++++++++++++++++++++
 3 files changed, 231 insertions(+), 1 deletion(-)
 create mode 100644 include/erofs/iterate.h
 create mode 100644 lib/iterate.c
include/erofs/iterate.h new file +8 -0
new file mode 100644
index 0000000..96171a7
@@ -0,0 +1,57 @@
+//
+// Copyright (C) 2021 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
Quoted 8 lines

include/erofs/iterate.h line 15

Quoted 8 lines
include/erofs/iterate.h new file +4 -0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or

implied.

include/erofs/iterate.h new file +3 -0
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//

SPDX is enough, it'd be better to be licensed as GPL-2.0+ OR Apache-2.0, see other files.

Quoted 8 lines
Quoted 8 lines
include/erofs/iterate.h new file +8 -0
+
+#ifndef ITERATE_ITERATE
+#define ITERATE_ITERATE
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
Quoted 8 lines

include/erofs/iterate.h line 31

Quoted 8 lines
include/erofs/iterate.h new file +8 -0
+
+
+#include "erofs/io.h"
+#include "erofs/print.h"
+
+
+struct erofs_inode_info {
+  uint64_t inode_id;

erofs_nid_t nid;

Quoted 1 line
Quoted 1 line

+ const char* name;

one tab instead of 2 spaces.

Quoted 1 line
Quoted 1 line

+ bool is_reg_file;

ftype.

Quoted 2 lines

include/erofs/iterate.h line 35

Quoted 2 lines
include/erofs/iterate.h new file +2 -0
+  u64 compressed_size;
+  u64 uncompressed_size;

Could we get these directly from erofs_inode?
so getting rid of them is better.

Quoted 6 lines

include/erofs/iterate.h line 41

Quoted 6 lines
include/erofs/iterate.h new file +6 -0
+  struct erofs_inode* inode;
+  void* arg;
+};
+// Callback function for iterating over inodes of EROFS
+
+typedef bool (*ErofsIterCallback)(struct erofs_inode_info);

no camelcases, erofs_readdir_cb seems better.

and struct erofs_inode_info *.

Quoted 46 lines
Quoted 46 lines
include/erofs/iterate.h new file +16 -0
+
+int erofs_iterate_dir(const struct erofs_sb_info* sbi,
+                   erofs_nid_t nid,
+                   erofs_nid_t parent_nid,
+                   ErofsIterCallback cb,
+                   void* arg);
+int erofs_iterate_root_dir(const struct erofs_sb_info* sbi,
+                        ErofsIterCallback cbg,
+                        void* arg);
+int erofs_get_occupied_size(struct erofs_inode* inode, erofs_off_t* size);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif  // ITERATE_ITERATE
lib/Makefile.am +1 -1
index 67ba798..20c0e4f 100644
@@ -27,7 +27,7 @@ noinst_HEADERS = $(top_srcdir)/include/erofs_fs.h \
 noinst_HEADERS += compressor.h
 liberofs_la_SOURCES = config.c io.c cache.c super.c inode.c xattr.c
 exclude.c \
                      namei.c data.c compress.c compressor.c zmap.c
 decompress.c \
-                     compress_hints.c hashmap.c sha256.c blobchunk.c
+                     compress_hints.c hashmap.c sha256.c blobchunk.c

iterate.c
liberofs_la_CFLAGS = -Wall -Werror -I$(top_srcdir)/include if ENABLE_LZ4
liberofs_la_CFLAGS += ${LZ4_CFLAGS}

lib/iterate.c new file +8 -0
new file mode 100644
index 0000000..1a10ec1
@@ -0,0 +1,173 @@
+//
+// Copyright (C) 2021 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
Quoted 8 lines

lib/iterate.c

Quoted 8 lines
lib/iterate.c new file +4 -0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or

implied.

lib/iterate.c new file +3 -0
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//

Same here.

Quoted 18 lines
Quoted 18 lines
lib/iterate.c new file +18 -0
+
+#include "erofs/internal.h"
+#include "erofs_fs.h"
+#include "erofs/print.h"
+#include "erofs/iterate.h"
+
+static int erofs_read_dirent(const struct erofs_sb_info* sbi,
+                             const struct erofs_dirent* de,
+                             erofs_nid_t nid,
+                             erofs_nid_t parent_nid,
+                             const char* dname,
+                             ErofsIterCallback cb,
+                             void* arg) {
+  int err;
+  erofs_off_t occupied_size = 0;
+  struct erofs_inode inode = {.nid = de->nid};
+  err = erofs_read_inode_from_disk(&inode);
+  if (err) {
Quoted 8 lines

lib/iterate.c line 41

Quoted 8 lines
lib/iterate.c new file +8 -0
+    erofs_err("read file inode from disk failed!");
+    return err;
+  }
+  err = erofs_get_occupied_size(&inode, &occupied_size);
+  if (err) {
+    erofs_err("get file size failed\n");
+    return err;
+  }

no need to do this.

Quoted 43 lines
Quoted 43 lines
lib/iterate.c new file +31 -0
+  char buf[PATH_MAX + 1];
+  erofs_get_inode_name(sbi, de->nid, buf, PATH_MAX + 1);
+  struct erofs_inode_info info = {
+      .inode_id = de->nid,
+      .name = buf,
+      .is_reg_file = de->file_type == EROFS_FT_REG_FILE,
+      .compressed_size = occupied_size,
+      .uncompressed_size = inode.i_size,
+      .inode = &inode,
+      .arg = arg};
+  cb(info);
+  if ((de->file_type == EROFS_FT_DIR) && de->nid != nid &&
+      de->nid != parent_nid) {
+    err = erofs_iterate_dir(sbi, de->nid, nid, cb, arg);
+    if (err) {
+      erofs_err("parse dir nid %u error occurred\n",
+                (unsigned int)(de->nid));
+      return err;
+    }
+  }
+  return 0;
+}
+
+static inline int erofs_checkdirent(const struct erofs_dirent* de,
+                                    const struct erofs_dirent* last_de,
+                                    u32 maxsize,
+                                    const char* dname) {
+  int dname_len;
+  unsigned int nameoff = le16_to_cpu(de->nameoff);
+  if (nameoff < sizeof(struct erofs_dirent) || nameoff >= PAGE_SIZE) {
+    erofs_err("invalid de[0].nameoff %u @ nid %llu", nameoff, de->nid |

0ULL);

lib/iterate.c new file +11 -0
+    return -EFSCORRUPTED;
+  }
+  dname_len = (de + 1 >= last_de) ? strnlen(dname, maxsize - nameoff)
+                                  : le16_to_cpu(de[1].nameoff) - nameoff;
+  /* a corrupted entry is found */
+  if (nameoff + dname_len > maxsize || dname_len > EROFS_NAME_LEN) {
+    erofs_err("bogus dirent @ nid %llu", le64_to_cpu(de->nid) | 0ULL);
+    DBG_BUGON(1);
+    return -EFSCORRUPTED;
+  }
+  if (de->file_type >= EROFS_FT_MAX) {
Quoted 8 lines

lib/iterate.c line 91

Quoted 8 lines
lib/iterate.c new file +8 -0
+    erofs_err("invalid file type %u", (unsigned int)(de->nid));
+    return -EFSCORRUPTED;
+  }
+  return dname_len;
+}
+
+int erofs_iterate_dir(const struct erofs_sb_info* sbi,
+                   erofs_nid_t nid,

nid is optional unless we do a fsck.

Thanks,
Gao Xiang

#6 lore raw

PATCH v3 1/2 Add API to get full pathname of an inode

Change-Id Iba99e590f051638285d1d1949311a08f5a5f1a82
---
 dump/main.c              |  70 ++-----------------------
 include/erofs/internal.h |   5 ++
 lib/namei.c              | 108 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 116 insertions(+), 67 deletions(-)
dump/main.c +3 -67
index 71b44b4..b728703 100644
@@ -11,6 +11,7 @@
 #include "erofs/print.h"
 #include "erofs/inode.h"
 #include "erofs/io.h"
+#include "erofs/internal.h"
 
 #ifdef HAVE_LIBUUID
 #include <uuid.h>
@@ -365,71 +366,6 @@ static int erofs_read_dir(erofs_nid_t nid, erofs_nid_t parent_nid)
 	return 0;
 }
 
-static int erofs_get_pathname(erofs_nid_t nid, erofs_nid_t parent_nid,
-		erofs_nid_t target, char *path, unsigned int pos)
-{
-	int err;
-	erofs_off_t offset;
-	char buf[EROFS_BLKSIZ];
-	struct erofs_inode inode = { .nid = nid };
-
-	path[pos++] = '/';
-	if (target == sbi.root_nid)
-		return 0;
-
-	err = erofs_read_inode_from_disk(&inode);
-	if (err) {
-		erofs_err("read inode failed @ nid %llu", nid | 0ULL);
-		return err;
-	}
-
-	offset = 0;
-	while (offset < inode.i_size) {
-		erofs_off_t maxsize = min_t(erofs_off_t,
-					inode.i_size - offset, EROFS_BLKSIZ);
-		struct erofs_dirent *de = (void *)buf;
-		struct erofs_dirent *end;
-		unsigned int nameoff;
-
-		err = erofs_pread(&inode, buf, maxsize, offset);
-		if (err)
-			return err;
-
-		nameoff = le16_to_cpu(de->nameoff);
-		end = (void *)buf + nameoff;
-		while (de < end) {
-			const char *dname;
-			int len;
-
-			nameoff = le16_to_cpu(de->nameoff);
-			dname = (char *)buf + nameoff;
-			len = erofs_checkdirent(de, end, maxsize, dname);
-			if (len < 0)
-				return len;
-
-			if (de->nid == target) {
-				memcpy(path + pos, dname, len);
-				path[pos + len] = '\0';
-				return 0;
-			}
-
-			if (de->file_type == EROFS_FT_DIR &&
-					de->nid != parent_nid &&
-					de->nid != nid) {
-				memcpy(path + pos, dname, len);
-				err = erofs_get_pathname(de->nid, nid,
-						target, path, pos + len);
-				if (!err)
-					return 0;
-				memset(path + pos, 0, len);
-			}
-			++de;
-		}
-		offset += maxsize;
-	}
-	return -1;
-}
-
 static int erofsdump_map_blocks(struct erofs_inode *inode,
 		struct erofs_map_blocks *map, int flags)
 {
@@ -478,12 +414,12 @@ static void erofsdump_show_fileinfo(bool show_extent)
 		return;
 	}
 
-	err = erofs_get_pathname(sbi.root_nid, sbi.root_nid,
-				 inode.nid, path, 0);
+	err = erofs_get_inode_name(&sbi, inode.nid, path, PATH_MAX+1);
 	if (err < 0) {
 		erofs_err("file path not found @ nid %llu", inode.nid | 0ULL);
 		return;
 	}
+  printf("Path: %s\n", path);
 
 	strftime(timebuf, sizeof(timebuf),
 		 "%Y-%m-%d %H:%M:%S", localtime((time_t *)&inode.i_ctime));
include/erofs/internal.h +5 -0
index a68de32..632461e 100644
@@ -305,6 +305,11 @@ int erofs_read_superblock(void);
 int erofs_read_inode_from_disk(struct erofs_inode *vi);
 int erofs_ilookup(const char *path, struct erofs_inode *vi);
 int erofs_read_inode_from_disk(struct erofs_inode *vi);
+// Get full path name of an inode, return 0 if success
+int erofs_get_inode_name(const struct erofs_sb_info* sbi,
+                         erofs_nid_t target,
+                         char* output_buffer,
+                         size_t capacity);
 
 /* data.c */
 int erofs_pread(struct erofs_inode *inode, char *buf,
lib/namei.c +108 -0

111 lines not on this page · raw message

Signature · 1 line
2.34.1.173.g76aa8bc2d0-goog
#7 lore raw

PATCH v3 2/2 Add API to iterate over inodes in EROFS

Change-Id Ia35708080a72ee204eaaddfc670d3cb8023a078c
---
 include/erofs/iterate.h |  40 +++++++++++
 include/erofs_fs.h      |   4 +-
 lib/Makefile.am         |   2 +-
 lib/iterate.c           | 148 ++++++++++++++++++++++++++++++++++++++++
 4 files changed, 191 insertions(+), 3 deletions(-)
 create mode 100644 include/erofs/iterate.h
 create mode 100644 lib/iterate.c
include/erofs/iterate.h new file +40 -0
new file mode 100644
index 0000000..af29e14
@@ -0,0 +1,40 @@
+// SPDX-License-Identifier: Apache-2.0
+
+#ifndef ITERATE_ITERATE
+#define ITERATE_ITERATE
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+
+#include "erofs/io.h"
+#include "erofs/print.h"
+
+
+struct erofs_inode_info {
+	const char* name;
+	enum erofs_ftype ftype;
+	struct erofs_inode* inode;
+	void* arg;
+};
+// Callback function for iterating over inodes of EROFS
+
+typedef bool (*erofs_readdir_cb)(struct erofs_inode_info*);
+
+int erofs_iterate_dir(const struct erofs_sb_info* sbi,
+									 erofs_nid_t nid,
+									 erofs_nid_t parent_nid,
+									 erofs_readdir_cb cb,
+									 void* arg);
+int erofs_iterate_root_dir(const struct erofs_sb_info* sbi,
+												erofs_readdir_cb cbg,
+												void* arg);
+int erofs_get_occupied_size(struct erofs_inode* inode, erofs_off_t* size);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif  // ITERATE_ITERATE
include/erofs_fs.h +2 -2
index 9a91877..7ee8251 100644
@@ -389,8 +389,8 @@ struct erofs_dirent {
 } __packed;
 
 /* file types used in inode_info->flags */
-enum {
-	EROFS_FT_UNKNOWN,
+enum erofs_ftype {
+	EROFS_FT_UNKNOWN = 0,
 	EROFS_FT_REG_FILE,
 	EROFS_FT_DIR,
 	EROFS_FT_CHRDEV,
lib/Makefile.am +1 -1
index 67ba798..20c0e4f 100644
@@ -27,7 +27,7 @@ noinst_HEADERS = $(top_srcdir)/include/erofs_fs.h \
 noinst_HEADERS += compressor.h
 liberofs_la_SOURCES = config.c io.c cache.c super.c inode.c xattr.c exclude.c \
 		      namei.c data.c compress.c compressor.c zmap.c decompress.c \
-		      compress_hints.c hashmap.c sha256.c blobchunk.c
+		      compress_hints.c hashmap.c sha256.c blobchunk.c iterate.c
 liberofs_la_CFLAGS = -Wall -Werror -I$(top_srcdir)/include
 if ENABLE_LZ4
 liberofs_la_CFLAGS += ${LZ4_CFLAGS}
lib/iterate.c new file +148 -0

148 lines not on this page · raw message

Signature · 1 line
2.34.1.173.g76aa8bc2d0-goog
#8 lore raw

Re: [PATCH v3 2/2] Add API to iterate over inodes in EROFS

Fixed most of the issues you pointed out. Except I didn't quite understand the "nid is optional unless we do a fsck." part. Not sure how we can implement the iterate dir function w/o nid. Can you provide more context?

Quoted 281 lines · Kelvin Zhang
Change-Id Ia35708080a72ee204eaaddfc670d3cb8023a078c
---
 include/erofs/iterate.h |  40 +++++++++++
 include/erofs_fs.h      |   4 +-
 lib/Makefile.am         |   2 +-
 lib/iterate.c           | 148 ++++++++++++++++++++++++++++++++++++++++
 4 files changed, 191 insertions(+), 3 deletions(-)
 create mode 100644 include/erofs/iterate.h
 create mode 100644 lib/iterate.c
include/erofs/iterate.h new file +27 -0
new file mode 100644
index 0000000..af29e14
@@ -0,0 +1,40 @@
+// SPDX-License-Identifier: Apache-2.0
+
+#ifndef ITERATE_ITERATE
+#define ITERATE_ITERATE
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+
+#include "erofs/io.h"
+#include "erofs/print.h"
+
+
+struct erofs_inode_info {
+       const char* name;
+       enum erofs_ftype ftype;
+       struct erofs_inode* inode;
+       void* arg;
+};
+// Callback function for iterating over inodes of EROFS
+
+typedef bool (*erofs_readdir_cb)(struct erofs_inode_info*);
+
+int erofs_iterate_dir(const struct erofs_sb_info* sbi,
+

erofs_nid_t nid,
+
erofs_nid_t parent_nid,
+
erofs_readdir_cb cb,
+
void* arg);

include/erofs/iterate.h new file +10 -0
+int erofs_iterate_root_dir(const struct erofs_sb_info* sbi,
+
                     erofs_readdir_cb cbg,
+
                     void* arg);
+int erofs_get_occupied_size(struct erofs_inode* inode, erofs_off_t* size);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif  // ITERATE_ITERATE
include/erofs_fs.h +2 -2
index 9a91877..7ee8251 100644
@@ -389,8 +389,8 @@ struct erofs_dirent {
 } __packed;
 
 /* file types used in inode_info->flags */
-enum {
-       EROFS_FT_UNKNOWN,
+enum erofs_ftype {
+       EROFS_FT_UNKNOWN = 0,
        EROFS_FT_REG_FILE,
        EROFS_FT_DIR,
        EROFS_FT_CHRDEV,
lib/Makefile.am +1 -1
index 67ba798..20c0e4f 100644
@@ -27,7 +27,7 @@ noinst_HEADERS = $(top_srcdir)/include/erofs_fs.h \
 noinst_HEADERS += compressor.h
 liberofs_la_SOURCES = config.c io.c cache.c super.c inode.c xattr.c
 exclude.c \
                      namei.c data.c compress.c compressor.c zmap.c
 decompress.c \
-                     compress_hints.c hashmap.c sha256.c blobchunk.c
+                     compress_hints.c hashmap.c sha256.c blobchunk.c

iterate.c
liberofs_la_CFLAGS = -Wall -Werror -I$(top_srcdir)/include if ENABLE_LZ4
liberofs_la_CFLAGS += ${LZ4_CFLAGS}

lib/iterate.c new file +36 -0
new file mode 100644
index 0000000..ec44217
@@ -0,0 +1,148 @@
+// SPDX-License-Identifier: Apache-2.0
+
+#include "erofs/internal.h"
+#include "erofs_fs.h"
+#include "erofs/print.h"
+#include "erofs/iterate.h"
+
+static int erofs_read_dirent(const struct erofs_sb_info* sbi,
+
                                      const struct erofs_dirent* de,
+
                                      erofs_nid_t nid,
+
                                      erofs_nid_t parent_nid,
+
                                      const char* dname,
+
                                      erofs_readdir_cb cb,
+
                                      void* arg) {
+       int err;
+       erofs_off_t occupied_size = 0;
+       struct erofs_inode inode = {.nid = de->nid};
+       err = erofs_read_inode_from_disk(&inode);
+       if (err) {
+               erofs_err("read file inode from disk failed!");
+               return err;
+       }
+       char buf[PATH_MAX + 1];
+       erofs_get_inode_name(sbi, de->nid, buf, PATH_MAX + 1);
+       struct erofs_inode_info info = {
+                       .name = buf,
+                       .ftype = de->file_type,
+                       .inode = &inode,
+                       .arg = arg};
+       cb(&info);
+       if ((de->file_type == EROFS_FT_DIR) && de->nid != nid &&
+                       de->nid != parent_nid) {
+               err = erofs_iterate_dir(sbi, de->nid, nid, cb, arg);
+               if (err) {
+                       erofs_err("parse dir nid %u error occurred\n",
+                                                               (unsigned

int)(de->nid));

lib/iterate.c new file +8 -0
+                       return err;
+               }
+       }
+       return 0;
+}
+
+static inline int erofs_checkdirent(const struct erofs_dirent* de,
+
                                                                     const

struct erofs_dirent* last_de,
+

                                                                     u32

maxsize,
+
const
char* dname) {

lib/iterate.c new file +3 -0
+       int dname_len;
+       unsigned int nameoff = le16_to_cpu(de->nameoff);
+       if (nameoff < sizeof(struct erofs_dirent) || nameoff >= PAGE_SIZE)

{
+ erofs_err("invalid de[0].nameoff %u @ nid %llu", nameoff,
de->nid | 0ULL);

lib/iterate.c new file +4 -0
+               return -EFSCORRUPTED;
+       }
+       dname_len = (de + 1 >= last_de) ? strnlen(dname, maxsize - nameoff)
+
                                                             :

le16_to_cpu(de[1].nameoff) - nameoff;

lib/iterate.c new file +3 -0
+       /* a corrupted entry is found */
+       if (nameoff + dname_len > maxsize || dname_len > EROFS_NAME_LEN) {
+               erofs_err("bogus dirent @ nid %llu", le64_to_cpu(de->nid)

| 0ULL);

lib/iterate.c new file +12 -0
+               DBG_BUGON(1);
+               return -EFSCORRUPTED;
+       }
+       if (de->file_type >= EROFS_FT_MAX) {
+               erofs_err("invalid file type %u", (unsigned int)(de->nid));
+               return -EFSCORRUPTED;
+       }
+       return dname_len;
+}
+
+int erofs_iterate_dir(const struct erofs_sb_info* sbi,
+

erofs_nid_t nid,
+
erofs_nid_t parent_nid,
+
erofs_readdir_cb cb,
+
void* arg) {

lib/iterate.c new file +21 -0
+       int err;
+       erofs_off_t offset;
+       char buf[EROFS_BLKSIZ];
+       struct erofs_inode vi = {.nid = nid};
+       err = erofs_read_inode_from_disk(&vi);
+       if (err)
+               return err;
+       struct erofs_inode_info inode_info = {
+                       .name = buf,
+                       .ftype = EROFS_FT_DIR,
+                       .inode = &vi,
+                       .arg = arg,
+       };
+       err = erofs_get_inode_name(sbi, nid, buf, EROFS_BLKSIZ);
+       cb(&inode_info);
+       if (err) {
+               return err;
+       }
+       offset = 0;
+       while (offset < vi.i_size) {
+               erofs_off_t maxsize = min_t(erofs_off_t, vi.i_size -

offset, EROFS_BLKSIZ);
+ const struct erofs_dirent* de = (const struct
erofs_dirent*)(buf);

lib/iterate.c new file +20 -0
+               struct erofs_dirent* end;
+               unsigned int nameoff;
+               err = erofs_pread(&vi, buf, maxsize, offset);
+               if (err)
+                       return err;
+               nameoff = le16_to_cpu(de->nameoff);
+               end = (struct erofs_dirent*)(buf + nameoff);
+               while (de < end) {
+                       const char* dname;
+                       int ret;
+                       /* skip "." and ".." dentry */
+                       if (de->nid == nid || de->nid == parent_nid) {
+                               de++;
+                               continue;
+                       }
+                       dname = (char*)buf + nameoff;
+                       ret = erofs_checkdirent(de, end, maxsize, dname);
+                       if (ret < 0)
+                               return ret;
+                       ret = erofs_read_dirent(sbi, de, nid, parent_nid,

dname, cb, arg);

lib/iterate.c new file +10 -0
+                       if (ret < 0)
+                               return ret;
+                       ++de;
+               }
+               offset += maxsize;
+       }
+       return 0;
+}
+
+int erofs_get_occupied_size(struct erofs_inode* inode, erofs_off_t* size)

{

lib/iterate.c new file +22 -0
+       *size = 0;
+       switch (inode->datalayout) {
+               case EROFS_INODE_FLAT_INLINE:
+               case EROFS_INODE_FLAT_PLAIN:
+               case EROFS_INODE_CHUNK_BASED:
+                       *size = inode->i_size;
+                       break;
+               case EROFS_INODE_FLAT_COMPRESSION_LEGACY:
+               case EROFS_INODE_FLAT_COMPRESSION:
+                       *size = inode->u.i_blocks * EROFS_BLKSIZ;
+                       break;
+               default:
+                       erofs_err("unknown datalayout");
+                       return -1;
+       }
+       return 0;
+}
+
+int erofs_iterate_root_dir(const struct erofs_sb_info* sbi,
+
                     erofs_readdir_cb cb,
+
                     void* arg) {
+       return erofs_iterate_dir(sbi, sbi->root_nid, sbi->root_nid, cb,

arg);

lib/iterate.c new file +2 -1
+}
+
--

2.34.1.173.g76aa8bc2d0-goog

Signature · 3 lines
Sincerely,

Kelvin Zhang
#9 lore raw

Re: [PATCH v3 2/2] Add API to iterate over inodes in EROFS

Quoted 3 lines · Kelvin Zhang

Fixed most of the issues you pointed out. Except I didn't quite understand the "nid is optional unless we do a fsck." part. Not sure how we can implement the iterate dir function w/o nid. Can you provide more context?

There were two nids there, parent_nid and nid. I meant you could leave dir nid (no matter how it's called) mandatorily. dir's parent nid is optional.

Sorry if I made some confusion at that time.

Thanks,
Gao Xiang

#10 lore raw

Re: [PATCH v3 2/2] Add API to iterate over inodes in EROFS

I think we still need parent_nid to skip "." and ".." directories correctly? Let's leave these parameters there.

Quoted 15 lines · Gao Xiang
Quoted 1 line · Kelvin Zhang

Fixed most of the issues you pointed out. Except I didn't quite

understand

Quoted 2 lines

the "nid is optional unless we do a fsck." part. Not sure how we can implement the iterate dir function w/o nid. Can you provide more context?

There were two nids there, parent_nid and nid. I meant you could leave dir nid (no matter how it's called) mandatorily. dir's parent nid is optional.

Sorry if I made some confusion at that time.

Thanks,
Gao Xiang

Signature · 3 lines
Sincerely,

Kelvin Zhang
#11 lore raw

Re: [PATCH v3 2/2] Add API to iterate over inodes in EROFS

Quoted 2 lines · Kelvin Zhang

I think we still need parent_nid to skip "." and ".." directories correctly? Let's leave these parameters there.

We could also just skip these by the name ("." and "..") instead of nids. parent nid is just for sanity check.

Thanks,
Gao Xiang

Quoted 25 lines
Quoted 15 lines · Gao Xiang
Quoted 1 line · Kelvin Zhang

Fixed most of the issues you pointed out. Except I didn't quite

understand

Quoted 2 lines

the "nid is optional unless we do a fsck." part. Not sure how we can implement the iterate dir function w/o nid. Can you provide more context?

There were two nids there, parent_nid and nid. I meant you could leave dir nid (no matter how it's called) mandatorily. dir's parent nid is optional.

Sorry if I made some confusion at that time.

Thanks,
Gao Xiang

Signature · 3 lines
Sincerely,

Kelvin Zhang
#12 lore raw

PATCH v4 1/2 Add API to get full pathname of an inode

Change-Id Iba99e590f051638285d1d1949311a08f5a5f1a82
---
 dump/main.c              |  70 ++-----------------------
 include/erofs/internal.h |   5 ++
 lib/namei.c              | 108 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 116 insertions(+), 67 deletions(-)
dump/main.c +3 -67
index 71b44b4..b728703 100644
@@ -11,6 +11,7 @@
 #include "erofs/print.h"
 #include "erofs/inode.h"
 #include "erofs/io.h"
+#include "erofs/internal.h"
 
 #ifdef HAVE_LIBUUID
 #include <uuid.h>
@@ -365,71 +366,6 @@ static int erofs_read_dir(erofs_nid_t nid, erofs_nid_t parent_nid)
 	return 0;
 }
 
-static int erofs_get_pathname(erofs_nid_t nid, erofs_nid_t parent_nid,
-		erofs_nid_t target, char *path, unsigned int pos)
-{
-	int err;
-	erofs_off_t offset;
-	char buf[EROFS_BLKSIZ];
-	struct erofs_inode inode = { .nid = nid };
-
-	path[pos++] = '/';
-	if (target == sbi.root_nid)
-		return 0;
-
-	err = erofs_read_inode_from_disk(&inode);
-	if (err) {
-		erofs_err("read inode failed @ nid %llu", nid | 0ULL);
-		return err;
-	}
-
-	offset = 0;
-	while (offset < inode.i_size) {
-		erofs_off_t maxsize = min_t(erofs_off_t,
-					inode.i_size - offset, EROFS_BLKSIZ);
-		struct erofs_dirent *de = (void *)buf;
-		struct erofs_dirent *end;
-		unsigned int nameoff;
-
-		err = erofs_pread(&inode, buf, maxsize, offset);
-		if (err)
-			return err;
-
-		nameoff = le16_to_cpu(de->nameoff);
-		end = (void *)buf + nameoff;
-		while (de < end) {
-			const char *dname;
-			int len;
-
-			nameoff = le16_to_cpu(de->nameoff);
-			dname = (char *)buf + nameoff;
-			len = erofs_checkdirent(de, end, maxsize, dname);
-			if (len < 0)
-				return len;
-
-			if (de->nid == target) {
-				memcpy(path + pos, dname, len);
-				path[pos + len] = '\0';
-				return 0;
-			}
-
-			if (de->file_type == EROFS_FT_DIR &&
-					de->nid != parent_nid &&
-					de->nid != nid) {
-				memcpy(path + pos, dname, len);
-				err = erofs_get_pathname(de->nid, nid,
-						target, path, pos + len);
-				if (!err)
-					return 0;
-				memset(path + pos, 0, len);
-			}
-			++de;
-		}
-		offset += maxsize;
-	}
-	return -1;
-}
-
 static int erofsdump_map_blocks(struct erofs_inode *inode,
 		struct erofs_map_blocks *map, int flags)
 {
@@ -478,12 +414,12 @@ static void erofsdump_show_fileinfo(bool show_extent)
 		return;
 	}
 
-	err = erofs_get_pathname(sbi.root_nid, sbi.root_nid,
-				 inode.nid, path, 0);
+	err = erofs_get_inode_name(&sbi, inode.nid, path, PATH_MAX+1);
 	if (err < 0) {
 		erofs_err("file path not found @ nid %llu", inode.nid | 0ULL);
 		return;
 	}
+  printf("Path: %s\n", path);
 
 	strftime(timebuf, sizeof(timebuf),
 		 "%Y-%m-%d %H:%M:%S", localtime((time_t *)&inode.i_ctime));
include/erofs/internal.h +5 -0
index a68de32..632461e 100644
@@ -305,6 +305,11 @@ int erofs_read_superblock(void);
 int erofs_read_inode_from_disk(struct erofs_inode *vi);
 int erofs_ilookup(const char *path, struct erofs_inode *vi);
 int erofs_read_inode_from_disk(struct erofs_inode *vi);
+// Get full path name of an inode, return 0 if success
+int erofs_get_inode_name(const struct erofs_sb_info* sbi,
+                         erofs_nid_t target,
+                         char* output_buffer,
+                         size_t capacity);
 
 /* data.c */
 int erofs_pread(struct erofs_inode *inode, char *buf,
lib/namei.c +108 -0

111 lines not on this page · raw message

Signature · 1 line
2.34.1.173.g76aa8bc2d0-goog
#13 lore raw

PATCH v4 2/2 Add API to iterate over inodes in EROFS

Change-Id Ia35708080a72ee204eaaddfc670d3cb8023a078c
---
 include/erofs/iterate.h |  46 ++++++++++++
 include/erofs_fs.h      |   4 +-
 lib/Makefile.am         |   2 +-
 lib/iterate.c           | 154 ++++++++++++++++++++++++++++++++++++++++
 4 files changed, 203 insertions(+), 3 deletions(-)
 create mode 100644 include/erofs/iterate.h
 create mode 100644 lib/iterate.c
include/erofs/iterate.h new file +46 -0
new file mode 100644
index 0000000..4e2c783
@@ -0,0 +1,46 @@
+// SPDX-License-Identifier: Apache-2.0
+
+#ifndef ITERATE_ITERATE
+#define ITERATE_ITERATE
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+
+#include "erofs/io.h"
+#include "erofs/print.h"
+
+
+struct erofs_inode_info {
+	const char* name;
+	enum erofs_ftype ftype;
+	struct erofs_inode* inode;
+	void* arg;
+};
+// Callback function for iterating over inodes of EROFS
+
+typedef bool (*erofs_readdir_cb)(struct erofs_inode_info*);
+
+// Iterate over inodes that are in directory specified by |nid|.
+// |parent_nid| is optional, if specified, additional sanity checks will
+// be performed.
+// |cb| will be called for every inode, regardless of type of inode.
+// |arg| will be passed to the callback in |erofs_readdir_cb| struct's
+// |arg| field.
+int erofs_iterate_dir(const struct erofs_sb_info* sbi,
+									 erofs_nid_t nid,
+									 erofs_nid_t parent_nid,
+									 erofs_readdir_cb cb,
+									 void* arg);
+int erofs_iterate_root_dir(const struct erofs_sb_info* sbi,
+												erofs_readdir_cb cbg,
+												void* arg);
+int erofs_get_occupied_size(const struct erofs_inode* inode, erofs_off_t* size);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif  // ITERATE_ITERATE
include/erofs_fs.h +2 -2
index 9a91877..7ee8251 100644
@@ -389,8 +389,8 @@ struct erofs_dirent {
 } __packed;
 
 /* file types used in inode_info->flags */
-enum {
-	EROFS_FT_UNKNOWN,
+enum erofs_ftype {
+	EROFS_FT_UNKNOWN = 0,
 	EROFS_FT_REG_FILE,
 	EROFS_FT_DIR,
 	EROFS_FT_CHRDEV,
lib/Makefile.am +1 -1
index 67ba798..20c0e4f 100644
@@ -27,7 +27,7 @@ noinst_HEADERS = $(top_srcdir)/include/erofs_fs.h \
 noinst_HEADERS += compressor.h
 liberofs_la_SOURCES = config.c io.c cache.c super.c inode.c xattr.c exclude.c \
 		      namei.c data.c compress.c compressor.c zmap.c decompress.c \
-		      compress_hints.c hashmap.c sha256.c blobchunk.c
+		      compress_hints.c hashmap.c sha256.c blobchunk.c iterate.c
 liberofs_la_CFLAGS = -Wall -Werror -I$(top_srcdir)/include
 if ENABLE_LZ4
 liberofs_la_CFLAGS += ${LZ4_CFLAGS}
lib/iterate.c new file +154 -0

154 lines not on this page · raw message

Signature · 1 line
2.34.1.173.g76aa8bc2d0-goog
#14 lore raw

Re: [PATCH v4 2/2] Add API to iterate over inodes in EROFS

Done, changed that as well. Also added a bit of documentation to note that parent_nid is optional.

Quoted 296 lines · Kelvin Zhang
Change-Id Ia35708080a72ee204eaaddfc670d3cb8023a078c
---
 include/erofs/iterate.h |  46 ++++++++++++
 include/erofs_fs.h      |   4 +-
 lib/Makefile.am         |   2 +-
 lib/iterate.c           | 154 ++++++++++++++++++++++++++++++++++++++++
 4 files changed, 203 insertions(+), 3 deletions(-)
 create mode 100644 include/erofs/iterate.h
 create mode 100644 lib/iterate.c
include/erofs/iterate.h new file +33 -0
new file mode 100644
index 0000000..4e2c783
@@ -0,0 +1,46 @@
+// SPDX-License-Identifier: Apache-2.0
+
+#ifndef ITERATE_ITERATE
+#define ITERATE_ITERATE
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+
+#include "erofs/io.h"
+#include "erofs/print.h"
+
+
+struct erofs_inode_info {
+       const char* name;
+       enum erofs_ftype ftype;
+       struct erofs_inode* inode;
+       void* arg;
+};
+// Callback function for iterating over inodes of EROFS
+
+typedef bool (*erofs_readdir_cb)(struct erofs_inode_info*);
+
+// Iterate over inodes that are in directory specified by |nid|.
+// |parent_nid| is optional, if specified, additional sanity checks will
+// be performed.
+// |cb| will be called for every inode, regardless of type of inode.
+// |arg| will be passed to the callback in |erofs_readdir_cb| struct's
+// |arg| field.
+int erofs_iterate_dir(const struct erofs_sb_info* sbi,
+

erofs_nid_t nid,
+
erofs_nid_t parent_nid,
+
erofs_readdir_cb cb,
+
void* arg);

include/erofs/iterate.h new file +4 -0
+int erofs_iterate_root_dir(const struct erofs_sb_info* sbi,
+
                     erofs_readdir_cb cbg,
+
                     void* arg);
+int erofs_get_occupied_size(const struct erofs_inode* inode, erofs_off_t*

size);

include/erofs/iterate.h new file +6 -0
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif  // ITERATE_ITERATE
include/erofs_fs.h +2 -2
index 9a91877..7ee8251 100644
@@ -389,8 +389,8 @@ struct erofs_dirent {
 } __packed;
 
 /* file types used in inode_info->flags */
-enum {
-       EROFS_FT_UNKNOWN,
+enum erofs_ftype {
+       EROFS_FT_UNKNOWN = 0,
        EROFS_FT_REG_FILE,
        EROFS_FT_DIR,
        EROFS_FT_CHRDEV,
lib/Makefile.am +1 -1
index 67ba798..20c0e4f 100644
@@ -27,7 +27,7 @@ noinst_HEADERS = $(top_srcdir)/include/erofs_fs.h \
 noinst_HEADERS += compressor.h
 liberofs_la_SOURCES = config.c io.c cache.c super.c inode.c xattr.c
 exclude.c \
                      namei.c data.c compress.c compressor.c zmap.c
 decompress.c \
-                     compress_hints.c hashmap.c sha256.c blobchunk.c
+                     compress_hints.c hashmap.c sha256.c blobchunk.c

iterate.c
liberofs_la_CFLAGS = -Wall -Werror -I$(top_srcdir)/include if ENABLE_LZ4
liberofs_la_CFLAGS += ${LZ4_CFLAGS}

lib/iterate.c new file +35 -0
new file mode 100644
index 0000000..e01eadf
@@ -0,0 +1,154 @@
+// SPDX-License-Identifier: Apache-2.0
+
+#include "erofs/internal.h"
+#include "erofs_fs.h"
+#include "erofs/print.h"
+#include "erofs/iterate.h"
+
+static int erofs_read_dirent(const struct erofs_sb_info* sbi,
+
                                      const struct erofs_dirent* de,
+
                                      erofs_nid_t nid,
+
                                      erofs_nid_t parent_nid,
+
                                      const char* dname,
+
                                      erofs_readdir_cb cb,
+
                                      void* arg) {
+       int err;
+       struct erofs_inode inode = {.nid = de->nid};
+       err = erofs_read_inode_from_disk(&inode);
+       if (err) {
+               erofs_err("read file inode from disk failed!");
+               return err;
+       }
+       char buf[PATH_MAX + 1];
+       erofs_get_inode_name(sbi, de->nid, buf, PATH_MAX + 1);
+       struct erofs_inode_info info = {
+                       .name = buf,
+                       .ftype = de->file_type,
+                       .inode = &inode,
+                       .arg = arg};
+       cb(&info);
+       if ((de->file_type == EROFS_FT_DIR) && de->nid != nid &&
+                       de->nid != parent_nid) {
+               err = erofs_iterate_dir(sbi, de->nid, nid, cb, arg);
+               if (err) {
+                       erofs_err("parse dir nid %u error occurred\n",
+                                                               (unsigned

int)(de->nid));

lib/iterate.c new file +8 -0
+                       return err;
+               }
+       }
+       return 0;
+}
+
+static inline int erofs_checkdirent(const struct erofs_dirent* de,
+
                                                                     const

struct erofs_dirent* last_de,
+

                                                                     u32

maxsize,
+
const
char* dname) {

lib/iterate.c new file +3 -0
+       int dname_len;
+       unsigned int nameoff = le16_to_cpu(de->nameoff);
+       if (nameoff < sizeof(struct erofs_dirent) || nameoff >= PAGE_SIZE)

{
+ erofs_err("invalid de[0].nameoff %u @ nid %llu", nameoff,
de->nid | 0ULL);

lib/iterate.c new file +4 -0
+               return -EFSCORRUPTED;
+       }
+       dname_len = (de + 1 >= last_de) ? strnlen(dname, maxsize - nameoff)
+
                                                             :

le16_to_cpu(de[1].nameoff) - nameoff;

lib/iterate.c new file +3 -0
+       /* a corrupted entry is found */
+       if (nameoff + dname_len > maxsize || dname_len > EROFS_NAME_LEN) {
+               erofs_err("bogus dirent @ nid %llu", le64_to_cpu(de->nid)

| 0ULL);

lib/iterate.c new file +12 -0
+               DBG_BUGON(1);
+               return -EFSCORRUPTED;
+       }
+       if (de->file_type >= EROFS_FT_MAX) {
+               erofs_err("invalid file type %u", (unsigned int)(de->nid));
+               return -EFSCORRUPTED;
+       }
+       return dname_len;
+}
+
+int erofs_iterate_dir(const struct erofs_sb_info* sbi,
+

erofs_nid_t nid,
+
erofs_nid_t parent_nid,
+
erofs_readdir_cb cb,
+
void* arg) {

lib/iterate.c new file +21 -0
+       int err;
+       erofs_off_t offset;
+       char buf[EROFS_BLKSIZ];
+       struct erofs_inode vi = {.nid = nid};
+       err = erofs_read_inode_from_disk(&vi);
+       if (err)
+               return err;
+       struct erofs_inode_info inode_info = {
+                       .name = buf,
+                       .ftype = EROFS_FT_DIR,
+                       .inode = &vi,
+                       .arg = arg,
+       };
+       err = erofs_get_inode_name(sbi, nid, buf, EROFS_BLKSIZ);
+       cb(&inode_info);
+       if (err) {
+               return err;
+       }
+       offset = 0;
+       while (offset < vi.i_size) {
+               erofs_off_t maxsize = min_t(erofs_off_t, vi.i_size -

offset, EROFS_BLKSIZ);
+ const struct erofs_dirent* de = (const struct
erofs_dirent*)(buf);

lib/iterate.c new file +14 -0
+               struct erofs_dirent* end;
+               unsigned int nameoff;
+               err = erofs_pread(&vi, buf, maxsize, offset);
+               if (err)
+                       return err;
+               nameoff = le16_to_cpu(de->nameoff);
+               end = (struct erofs_dirent*)(buf + nameoff);
+               while (de < end) {
+                       const char * const dname = (char*)buf + nameoff;
+                       int ret;
+                       /* skip "." and ".." dentry */
+                       if (is_dot_dotdot(dname)) {
+                               if (dname[1] == '.' && parent_nid > 0) {
+                                       // Directory ".." should have nid

== parent_nid.
+ // But parent_nid parameter is
optional, so only perform the check

lib/iterate.c new file +12 -0
+                                       // if parent_nid is specified.
+                                       if (parent_nid != de->nid) {
+                                               return EFSCORRUPTED;
+                                       }
+                               }
+                               de++;
+                               continue;
+                       }
+                       ret = erofs_checkdirent(de, end, maxsize, dname);
+                       if (ret < 0)
+                               return ret;
+                       ret = erofs_read_dirent(sbi, de, nid, parent_nid,

dname, cb, arg);

lib/iterate.c new file +10 -0
+                       if (ret < 0)
+                               return ret;
+                       ++de;
+               }
+               offset += maxsize;
+       }
+       return 0;
+}
+
+int erofs_get_occupied_size(const struct erofs_inode* inode, erofs_off_t*

size) {

lib/iterate.c new file +22 -0
+       *size = 0;
+       switch (inode->datalayout) {
+               case EROFS_INODE_FLAT_INLINE:
+               case EROFS_INODE_FLAT_PLAIN:
+               case EROFS_INODE_CHUNK_BASED:
+                       *size = inode->i_size;
+                       break;
+               case EROFS_INODE_FLAT_COMPRESSION_LEGACY:
+               case EROFS_INODE_FLAT_COMPRESSION:
+                       *size = inode->u.i_blocks * EROFS_BLKSIZ;
+                       break;
+               default:
+                       erofs_err("unknown datalayout");
+                       return -1;
+       }
+       return 0;
+}
+
+int erofs_iterate_root_dir(const struct erofs_sb_info* sbi,
+
                     erofs_readdir_cb cb,
+
                     void* arg) {
+       return erofs_iterate_dir(sbi, sbi->root_nid, sbi->root_nid, cb,

arg);

lib/iterate.c new file +2 -1
+}
+
--

2.34.1.173.g76aa8bc2d0-goog

Signature · 3 lines
Sincerely,

Kelvin Zhang
#16 lore raw

PATCH v5 1/2 Add API to get full pathname of an inode

Change-Id Iba99e590f051638285d1d1949311a08f5a5f1a82
---
 dump/main.c              |  70 ++-----------------------
 include/erofs/internal.h |   5 ++
 lib/namei.c              | 108 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 116 insertions(+), 67 deletions(-)
dump/main.c +3 -67
index 072d726..85695ee 100644
@@ -11,6 +11,7 @@
 #include "erofs/print.h"
 #include "erofs/inode.h"
 #include "erofs/io.h"
+#include "erofs/internal.h"
 
 #ifdef HAVE_LIBUUID
 #include <uuid.h>
@@ -366,71 +367,6 @@ static int erofs_read_dir(erofs_nid_t nid, erofs_nid_t parent_nid)
 	return 0;
 }
 
-static int erofs_get_pathname(erofs_nid_t nid, erofs_nid_t parent_nid,
-		erofs_nid_t target, char *path, unsigned int pos)
-{
-	int err;
-	erofs_off_t offset;
-	char buf[EROFS_BLKSIZ];
-	struct erofs_inode inode = { .nid = nid };
-
-	path[pos++] = '/';
-	if (target == sbi.root_nid)
-		return 0;
-
-	err = erofs_read_inode_from_disk(&inode);
-	if (err) {
-		erofs_err("read inode failed @ nid %llu", nid | 0ULL);
-		return err;
-	}
-
-	offset = 0;
-	while (offset < inode.i_size) {
-		erofs_off_t maxsize = min_t(erofs_off_t,
-					inode.i_size - offset, EROFS_BLKSIZ);
-		struct erofs_dirent *de = (void *)buf;
-		struct erofs_dirent *end;
-		unsigned int nameoff;
-
-		err = erofs_pread(&inode, buf, maxsize, offset);
-		if (err)
-			return err;
-
-		nameoff = le16_to_cpu(de->nameoff);
-		end = (void *)buf + nameoff;
-		while (de < end) {
-			const char *dname;
-			int len;
-
-			nameoff = le16_to_cpu(de->nameoff);
-			dname = (char *)buf + nameoff;
-			len = erofs_checkdirent(de, end, maxsize, dname);
-			if (len < 0)
-				return len;
-
-			if (le64_to_cpu(de->nid) == target) {
-				memcpy(path + pos, dname, len);
-				path[pos + len] = '\0';
-				return 0;
-			}
-
-			if (de->file_type == EROFS_FT_DIR &&
-			    le64_to_cpu(de->nid) != parent_nid &&
-			    le64_to_cpu(de->nid) != nid) {
-				memcpy(path + pos, dname, len);
-				err = erofs_get_pathname(le64_to_cpu(de->nid),
-						nid, target, path, pos + len);
-				if (!err)
-					return 0;
-				memset(path + pos, 0, len);
-			}
-			++de;
-		}
-		offset += maxsize;
-	}
-	return -1;
-}
-
 static int erofsdump_map_blocks(struct erofs_inode *inode,
 		struct erofs_map_blocks *map, int flags)
 {
@@ -479,12 +415,12 @@ static void erofsdump_show_fileinfo(bool show_extent)
 		return;
 	}
 
-	err = erofs_get_pathname(sbi.root_nid, sbi.root_nid,
-				 inode.nid, path, 0);
+	err = erofs_get_inode_name(&sbi, inode.nid, path, PATH_MAX+1);
 	if (err < 0) {
 		erofs_err("file path not found @ nid %llu", inode.nid | 0ULL);
 		return;
 	}
+  printf("Path: %s\n", path);
 
 	strftime(timebuf, sizeof(timebuf),
 		 "%Y-%m-%d %H:%M:%S", localtime((time_t *)&inode.i_ctime));
include/erofs/internal.h +5 -0
index e6beb8c..59ecdd8 100644
@@ -305,6 +305,11 @@ int erofs_read_superblock(void);
 int erofs_read_inode_from_disk(struct erofs_inode *vi);
 int erofs_ilookup(const char *path, struct erofs_inode *vi);
 int erofs_read_inode_from_disk(struct erofs_inode *vi);
+// Get full path name of an inode, return 0 if success
+int erofs_get_inode_name(const struct erofs_sb_info* sbi,
+                         erofs_nid_t target,
+                         char* output_buffer,
+                         size_t capacity);
 
 /* data.c */
 int erofs_pread(struct erofs_inode *inode, char *buf,
lib/namei.c +108 -0

111 lines not on this page · raw message

Signature · 1 line
2.34.1.173.g76aa8bc2d0-goog
#17 lore raw

PATCH v5 2/2 Add API to iterate over inodes in EROFS

Change-Id Ia35708080a72ee204eaaddfc670d3cb8023a078c
---
 include/erofs/iterate.h |  46 ++++++++++++
 include/erofs_fs.h      |   4 +-
 lib/Makefile.am         |   2 +-
 lib/iterate.c           | 154 ++++++++++++++++++++++++++++++++++++++++
 4 files changed, 203 insertions(+), 3 deletions(-)
 create mode 100644 include/erofs/iterate.h
 create mode 100644 lib/iterate.c
include/erofs/iterate.h new file +46 -0
new file mode 100644
index 0000000..4e2c783
@@ -0,0 +1,46 @@
+// SPDX-License-Identifier: Apache-2.0
+
+#ifndef ITERATE_ITERATE
+#define ITERATE_ITERATE
+
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+
+#include "erofs/io.h"
+#include "erofs/print.h"
+
+
+struct erofs_inode_info {
+	const char* name;
+	enum erofs_ftype ftype;
+	struct erofs_inode* inode;
+	void* arg;
+};
+// Callback function for iterating over inodes of EROFS
+
+typedef bool (*erofs_readdir_cb)(struct erofs_inode_info*);
+
+// Iterate over inodes that are in directory specified by |nid|.
+// |parent_nid| is optional, if specified, additional sanity checks will
+// be performed.
+// |cb| will be called for every inode, regardless of type of inode.
+// |arg| will be passed to the callback in |erofs_readdir_cb| struct's
+// |arg| field.
+int erofs_iterate_dir(const struct erofs_sb_info* sbi,
+									 erofs_nid_t nid,
+									 erofs_nid_t parent_nid,
+									 erofs_readdir_cb cb,
+									 void* arg);
+int erofs_iterate_root_dir(const struct erofs_sb_info* sbi,
+												erofs_readdir_cb cbg,
+												void* arg);
+int erofs_get_occupied_size(const struct erofs_inode* inode, erofs_off_t* size);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif  // ITERATE_ITERATE
include/erofs_fs.h +2 -2
index 9a91877..7ee8251 100644
@@ -389,8 +389,8 @@ struct erofs_dirent {
 } __packed;
 
 /* file types used in inode_info->flags */
-enum {
-	EROFS_FT_UNKNOWN,
+enum erofs_ftype {
+	EROFS_FT_UNKNOWN = 0,
 	EROFS_FT_REG_FILE,
 	EROFS_FT_DIR,
 	EROFS_FT_CHRDEV,
lib/Makefile.am +1 -1
index c745e49..99fcfa1 100644
@@ -27,7 +27,7 @@ noinst_HEADERS = $(top_srcdir)/include/erofs_fs.h \
 noinst_HEADERS += compressor.h
 liberofs_la_SOURCES = config.c io.c cache.c super.c inode.c xattr.c exclude.c \
 		      namei.c data.c compress.c compressor.c zmap.c decompress.c \
-		      compress_hints.c hashmap.c sha256.c blobchunk.c
+		      compress_hints.c hashmap.c sha256.c blobchunk.c iterate.c
 liberofs_la_CFLAGS = -Wall -Werror -I$(top_srcdir)/include
 if ENABLE_LZ4
 liberofs_la_CFLAGS += ${LZ4_CFLAGS}
lib/iterate.c new file +154 -0

154 lines not on this page · raw message

Signature · 1 line
2.34.1.173.g76aa8bc2d0-goog
#18 lore raw

Re: [PATCH v5 2/2] Add API to iterate over inodes in EROFS

Hi Kelvin,

Quoted 3 lines · Kelvin Zhang
Change-Id Ia35708080a72ee204eaaddfc670d3cb8023a078c

---

In brief, I've taken some time to clean up / polish / enhance such logic and convert erofsfuse to use it:
https://lore.kernel.org/linux-erofs/[email protected]

PTAL and check if it helps your own scenario. I didn't update the original author name, also I updated the license into GPL-2.0+ OR Apache-2.0 dual license

since Apache-2.0 is incompatible with GPL.

If you have some other concerns, please drop me a word, thanks.

Some minor comments below.

Quoted 21 lines
 include/erofs/iterate.h |  46 ++++++++++++
 include/erofs_fs.h      |   4 +-
 lib/Makefile.am         |   2 +-
 lib/iterate.c           | 154 ++++++++++++++++++++++++++++++++++++++++
 4 files changed, 203 insertions(+), 3 deletions(-)
 create mode 100644 include/erofs/iterate.h
 create mode 100644 lib/iterate.c
include/erofs/iterate.h new file +7 -0
new file mode 100644
index 0000000..4e2c783
@@ -0,0 +1,46 @@
+// SPDX-License-Identifier: Apache-2.0
+
+#ifndef ITERATE_ITERATE
+#define ITERATE_ITERATE
+
+#ifdef __cplusplus
+extern "C"

include/erofs/iterate.h line 17

Quoted 10 lines
include/erofs/iterate.h new file +10 -0
+{
+#endif
+
+
+#include "erofs/io.h"
+#include "erofs/print.h"
+
+
+struct erofs_inode_info {
+	const char* name;
	const char *name;

include/erofs/iterate.h line 19

Quoted 2 lines
include/erofs/iterate.h new file +2 -0
+	enum erofs_ftype ftype;
+	struct erofs_inode* inode;
	struct erofs_inode *inode;

include/erofs/iterate.h line 31

Quoted 12 lines
include/erofs/iterate.h new file +12 -0
+	void* arg;
+};
+// Callback function for iterating over inodes of EROFS
+
+typedef bool (*erofs_readdir_cb)(struct erofs_inode_info*);
+
+// Iterate over inodes that are in directory specified by |nid|.
+// |parent_nid| is optional, if specified, additional sanity checks will
+// be performed.
+// |cb| will be called for every inode, regardless of type of inode.
+// |arg| will be passed to the callback in |erofs_readdir_cb| struct's
+// |arg| field.

erofs-utils follows linux kernel coding style, so apart from SPDX, it'd be better to use C-style comments only.

Also you could run scripts/checkpatch.pl in advance to avoid most style problems..

I'd be many thanks if code could follow the correct coding style, so I could have much less extra work to do.

include/erofs/iterate.h line 36

Quoted 5 lines
include/erofs/iterate.h new file +5 -0
+int erofs_iterate_dir(const struct erofs_sb_info* sbi,
+									 erofs_nid_t nid,
+									 erofs_nid_t parent_nid,
+									 erofs_readdir_cb cb,
+									 void* arg);

1 tab equals to 8 spaces in the kernel coding style. So the indentation is somewhat weird for me.

Thanks,
Gao Xiang

#19 lore raw
In reply to Gao Xiang

I just rebased them on top of the dev branch. Please try again now.

Quoted 11 lines · Gao Xiang
Quoted 2 lines · Kelvin Zhang
Change-Id Iba99e590f051638285d1d1949311a08f5a5f1a82

I couldn't apply them to dev or experimental branch, would you mind rebasing them? Or some hints about the base commit so I could apply?

Thanks,
Gao Xiang

Signature · 3 lines
Sincerely,

Kelvin Zhang