All of lore.kernel.org
 help / color / mirror / Atom feed
From: Gao Xiang <xiang@kernel.org>
To: linux-erofs@lists.ozlabs.org
Subject: [PATCH] erofs-utils: fsck: support extracting special files
Date: Sat, 11 Jun 2022 17:28:55 +0800	[thread overview]
Message-ID: <20220611092855.347106-1-xiang@kernel.org> (raw)

Now device special file, named pipe and UNIX domain socket can be
extracted too.

Signed-off-by: Gao Xiang <xiang@kernel.org>
---
 fsck/main.c | 48 +++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 47 insertions(+), 1 deletion(-)

diff --git a/fsck/main.c b/fsck/main.c
index 7fc7b6f..5a2f659 100644
--- a/fsck/main.c
+++ b/fsck/main.c
@@ -638,6 +638,44 @@ out:
 	return ret;
 }
 
+static int erofs_extract_special(struct erofs_inode *inode)
+{
+	bool tryagain = true;
+	int ret;
+
+	erofs_dbg("extract special to path: %s", fsckcfg.extract_path);
+
+	/* verify data chunk layout */
+	ret = erofs_verify_inode_data(inode, -1);
+	if (ret)
+		return ret;
+
+again:
+	if (mknod(fsckcfg.extract_path, inode->i_mode, inode->u.i_rdev) < 0) {
+		if (errno == EEXIST && fsckcfg.overwrite && tryagain) {
+			erofs_warn("try to forcely remove file %s",
+				   fsckcfg.extract_path);
+			if (unlink(fsckcfg.extract_path) < 0) {
+				erofs_err("failed to remove: %s",
+					  fsckcfg.extract_path);
+				return -errno;
+			}
+			tryagain = false;
+			goto again;
+		}
+		if (errno == EEXIST || fsckcfg.superuser) {
+			erofs_err("failed to create special file: %s",
+				  fsckcfg.extract_path);
+			ret = -errno;
+		} else {
+			erofs_warn("failed to create special file: %s, skipped",
+				   fsckcfg.extract_path);
+			ret = -ECANCELED;
+		}
+	}
+	return ret;
+}
+
 static int erofsfsck_dirent_iter(struct erofs_dir_context *ctx)
 {
 	int ret;
@@ -698,6 +736,12 @@ static int erofsfsck_check_inode(erofs_nid_t pnid, erofs_nid_t nid)
 		case S_IFLNK:
 			ret = erofs_extract_symlink(&inode);
 			break;
+		case S_IFCHR:
+		case S_IFBLK:
+		case S_IFIFO:
+		case S_IFSOCK:
+			ret = erofs_extract_special(&inode);
+			break;
 		default:
 			/* TODO */
 			goto verify;
@@ -707,7 +751,7 @@ verify:
 		/* verify data chunk layout */
 		ret = erofs_verify_inode_data(&inode, -1);
 	}
-	if (ret)
+	if (ret && ret != -ECANCELED)
 		goto out;
 
 	/* XXXX: the dir depth should be restricted in order to avoid loops */
@@ -725,6 +769,8 @@ verify:
 	if (!ret)
 		erofsfsck_set_attributes(&inode, fsckcfg.extract_path);
 
+	if (ret == -ECANCELED)
+		ret = 0;
 out:
 	if (ret && ret != -EIO)
 		fsckcfg.corrupted = true;
-- 
2.30.2


                 reply	other threads:[~2022-06-11  9:29 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220611092855.347106-1-xiang@kernel.org \
    --to=xiang@kernel.org \
    --cc=linux-erofs@lists.ozlabs.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.