[PATCH 17/19] xfsprogs: disable truncating of files

jtulak
2016-03-24T11:15:56Z

From: Jan Tulak <jtulak@redhat.com>

Unify mkfs.xfs behaviour a bit and never truncate files. If the user
is trying to mkfs an existing file, we don't want to destroy anything
he did with the file before (sparse file, allocations...)

Signed-off-by: Jan Tulak <jtulak@redhat.com>
---
libxfs/init.c | 2 +-
mkfs/xfs_mkfs.c | 18 +++++++++++-------
2 files changed, 12 insertions(+), 8 deletions(-)

diff --git a/libxfs/init.c b/libxfs/init.c
index 268136f..5f4b6c4 100644
--- a/libxfs/init.c
+++ b/libxfs/init.c
@@ -112,7 +112,7 @@ libxfs_device_open(char *path, int creat, int xflags, int setblksize)

retry:
flags = (readonly ? O_RDONLY : O_RDWR) | \
- (creat ? (O_CREAT|O_TRUNC) : 0) | \
+ (creat ? O_CREAT : 0) | \
(dio ? O_DIRECT : 0) | \
(excl ? O_EXCL : 0);

diff --git a/mkfs/xfs_mkfs.c b/mkfs/xfs_mkfs.c
index 9a6ae2c..2bb3b35 100644
--- a/mkfs/xfs_mkfs.c
+++ b/mkfs/xfs_mkfs.c
@@ -1029,15 +1029,19 @@ check_device_type(
const char *optname)
{
struct stat64 statbuf;
+ int statres;

- if (*isfile && (no_size || no_name)) {
+ statres = stat64(name, &statbuf);
+
+ if (*isfile && statres != 0 && (no_size || no_name)) {
fprintf(stderr,
- _("if -%s file then -%s name and -%s size are required\n"),
- optname, optname, optname);
+ _("if -%s file and the file does not exists, " \
+ "then -%s name and -%s size are required\n"),
+ optname, optname, optname);
usage();
}

- if (stat64(name, &statbuf)) {
+ if (statres) {
if (errno == ENOENT && *isfile) {
if (create)
*create = 1;
@@ -1059,9 +1063,9 @@ check_device_type(
}

/*
- * We only want to completely truncate and recreate an existing file if
- * we were specifically told it was a file. Set the create flag only in
- * this case to trigger that behaviour.
+ * We only want to create a file only if we were specifically told
+ * we want a file. Set the create flag only in this case to trigger
+ * that behaviour.
*/
if (S_ISREG(statbuf.st_mode)) {
if (!*isfile)
--
2.6.0