Skip to content

Commit

Permalink
Added immutable file flag support for BSD
Browse files Browse the repository at this point in the history
Ticket: ENT-10961, CFE-1840
Signed-off-by: Lars Erik Wik <[email protected]>
  • Loading branch information
larsewi committed Jan 30, 2025
1 parent 612a05d commit e23e7a1
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions libutils/fsattrs.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,31 @@ FSAttrsResult FileGetImmutableFlag(const char *filename, bool *flag)
*flag = attrs & FS_IMMUTABLE_FL;
return FS_ATTRS_SUCCESS;

#elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) \
|| defined(__NetBSD__)

struct stat sb;
int ret = lstat(filename, &sb);
if (ret == -1)
{
if (errno == ENOENT)
{
return FS_ATTRS_NOENTRY;
}

Log(LOG_LEVEL_ERR,
"Failed to get inode flags for file '%s': %s\n",
filename,
GetErrorStr());
return FS_ATTRS_FAILURE;
}

*flag = sb.st_flags & SF_IMMUTABLE;
return FS_ATTRS_SUCCESS;

#else
/* Windows, AIX, HP-UX does not have file system flags as far as I can
tell */
(void) filename;
(void) flag;
return FS_ATTRS_NOTSUPP;
Expand Down Expand Up @@ -129,7 +153,61 @@ FSAttrsResult FileUpdateImmutableFlag(const char *filename, bool flag)
close(fd);
return FS_ATTRS_SUCCESS;

#elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) \
|| defined(__NetBSD__)

struct stat sb;
int ret = lstat(filename, &sb);
if (ret == -1)
{
if (errno == ENOENT)
{
return FS_ATTRS_NOENTRY;
}

Log(LOG_LEVEL_ERR,
"Failed to get inode flags for file '%s': %s\n",
filename,
GetErrorStr());
return FS_ATTRS_FAILURE;
}


if (flag)
{
Log(LOG_LEVEL_DEBUG,
"Setting immutable bit in inode flags for file '%s'",
filename);
sb.st_flags |= SF_IMMUTABLE;
}
else
{
Log(LOG_LEVEL_DEBUG,
"Clearing immutable bit in inode flags for file '%s'",
filename);
sb.st_flags &= ~SF_IMMUTABLE;
}

ret = chflags(filename, sb.st_flags);
if (ret == -1)
{
if (errno == ENOTSUP)
{
return FS_ATTRS_NOTSUPP;
}

Log(LOG_LEVEL_ERR,
"Failed to update flags for file '%s': %s",
filename,
GetErrorStr());
return FS_ATTRS_FAILURE;
}

return FS_ATTRS_SUCCESS;

#else
/* Windows, AIX, HP-UX does not have file system flags as far as I can
tell */
(void) filename;
(void) flag;
return FS_ATTRS_NOTSUPP;
Expand Down

0 comments on commit e23e7a1

Please sign in to comment.