* [PATCH] mkfs: simplify setup_proto file status checks
@ 2026-06-22 7:11 liuh
2026-06-24 17:13 ` Darrick J. Wong
0 siblings, 1 reply; 3+ messages in thread
From: liuh @ 2026-06-22 7:11 UTC (permalink / raw)
To: djwong; +Cc: linux-xfs, liuh
setup_proto() calls filesize() before validating the source type with
fstat(), even though both operations ultimately require the same file
status information.
Perform a single fstat() immediately after opening the source path and
reuse the resulting metadata for both source type validation and file
size retrieval. This simplifies the setup logic and eliminates a
redundant metadata lookup.
Signed-off-by: liuh <liuhuan01@kylinos.cn>
---
mkfs/proto.c | 14 +++++++++++---
1 file changed, 11 insertions(+), 3 deletions(-)
diff --git a/mkfs/proto.c b/mkfs/proto.c
index a460aebd..51f61e18 100644
--- a/mkfs/proto.c
+++ b/mkfs/proto.c
@@ -82,14 +82,17 @@ setup_proto(
return result;
}
- if ((fd = open(fname, O_RDONLY)) < 0 || (size = filesize(fd)) < 0) {
+ if ((fd = open(fname, O_RDONLY)) < 0) {
fprintf(stderr, _("%s: failed to open %s: %s\n"),
progname, fname, strerror(errno));
goto out_fail;
}
- if (fstat(fd, &statbuf) < 0)
- fail(_("invalid or unreadable source path"), errno);
+ if (fstat(fd, &statbuf) < 0) {
+ fprintf(stderr, _("%s: failed to stat %s: %s\n"),
+ progname, fname, strerror(errno));
+ goto out_fail;
+ }
/*
* Handle directory inputs.
@@ -101,6 +104,11 @@ setup_proto(
return result;
}
+ /*
+ * Get size from statbuf for regular file
+ */
+ size = statbuf.st_size;
+
/*
* Else this is a protofile, let's handle traditionally.
*/
--
2.43.0
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH] mkfs: simplify setup_proto file status checks
2026-06-22 7:11 [PATCH] mkfs: simplify setup_proto file status checks liuh
@ 2026-06-24 17:13 ` Darrick J. Wong
0 siblings, 0 replies; 3+ messages in thread
From: Darrick J. Wong @ 2026-06-24 17:13 UTC (permalink / raw)
To: liuh; +Cc: linux-xfs
On Mon, Jun 22, 2026 at 03:11:10PM +0800, liuh wrote:
> setup_proto() calls filesize() before validating the source type with
> fstat(), even though both operations ultimately require the same file
> status information.
>
> Perform a single fstat() immediately after opening the source path and
> reuse the resulting metadata for both source type validation and file
> size retrieval. This simplifies the setup logic and eliminates a
> redundant metadata lookup.
>
> Signed-off-by: liuh <liuhuan01@kylinos.cn>
> ---
> mkfs/proto.c | 14 +++++++++++---
> 1 file changed, 11 insertions(+), 3 deletions(-)
>
> diff --git a/mkfs/proto.c b/mkfs/proto.c
> index a460aebd..51f61e18 100644
> --- a/mkfs/proto.c
> +++ b/mkfs/proto.c
> @@ -82,14 +82,17 @@ setup_proto(
> return result;
> }
>
> - if ((fd = open(fname, O_RDONLY)) < 0 || (size = filesize(fd)) < 0) {
> + if ((fd = open(fname, O_RDONLY)) < 0) {
> fprintf(stderr, _("%s: failed to open %s: %s\n"),
> progname, fname, strerror(errno));
> goto out_fail;
> }
>
> - if (fstat(fd, &statbuf) < 0)
> - fail(_("invalid or unreadable source path"), errno);
> + if (fstat(fd, &statbuf) < 0) {
> + fprintf(stderr, _("%s: failed to stat %s: %s\n"),
> + progname, fname, strerror(errno));
> + goto out_fail;
> + }
>
> /*
> * Handle directory inputs.
> @@ -101,6 +104,11 @@ setup_proto(
> return result;
> }
>
> + /*
> + * Get size from statbuf for regular file
There's no guarantee that this is a regular file; the only option that
we've eliminated here is S_IFDIR. There ought to be a check for
S_IFREG here.
--D
> + */
> + size = statbuf.st_size;
> +
> /*
> * Else this is a protofile, let's handle traditionally.
> */
> --
> 2.43.0
>
>
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH] mkfs: simplify setup_proto file status checks
@ 2026-06-22 7:11 liuh
0 siblings, 0 replies; 3+ messages in thread
From: liuh @ 2026-06-22 7:11 UTC (permalink / raw)
To: djwong; +Cc: linux-xfs, liuh
setup_proto() calls filesize() before validating the source type with
fstat(), even though both operations ultimately require the same file
status information.
Perform a single fstat() immediately after opening the source path and
reuse the resulting metadata for both source type validation and file
size retrieval. This simplifies the setup logic and eliminates a
redundant metadata lookup.
Signed-off-by: liuh <liuhuan01@kylinos.cn>
---
mkfs/proto.c | 14 +++++++++++---
1 file changed, 11 insertions(+), 3 deletions(-)
diff --git a/mkfs/proto.c b/mkfs/proto.c
index a460aebd..51f61e18 100644
--- a/mkfs/proto.c
+++ b/mkfs/proto.c
@@ -82,14 +82,17 @@ setup_proto(
return result;
}
- if ((fd = open(fname, O_RDONLY)) < 0 || (size = filesize(fd)) < 0) {
+ if ((fd = open(fname, O_RDONLY)) < 0) {
fprintf(stderr, _("%s: failed to open %s: %s\n"),
progname, fname, strerror(errno));
goto out_fail;
}
- if (fstat(fd, &statbuf) < 0)
- fail(_("invalid or unreadable source path"), errno);
+ if (fstat(fd, &statbuf) < 0) {
+ fprintf(stderr, _("%s: failed to stat %s: %s\n"),
+ progname, fname, strerror(errno));
+ goto out_fail;
+ }
/*
* Handle directory inputs.
@@ -101,6 +104,11 @@ setup_proto(
return result;
}
+ /*
+ * Get size from statbuf for regular file
+ */
+ size = statbuf.st_size;
+
/*
* Else this is a protofile, let's handle traditionally.
*/
--
2.43.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-06-24 17:13 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-06-22 7:11 [PATCH] mkfs: simplify setup_proto file status checks liuh
2026-06-24 17:13 ` Darrick J. Wong
-- strict thread matches above, loose matches on Subject: below --
2026-06-22 7:11 liuh
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox