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 2c0b1ed commit dde16da
Showing 1 changed file with 80 additions and 15 deletions.
95 changes: 80 additions & 15 deletions libutils/fsattrs.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ FSAttrsResult FileGetImmutableFlag(const char *filename, bool *flag)
{
if (errno == ENOENT)
{
/* O_CREAT is not set and the named file does not exist.
- open(2) */
return FS_ATTRS_NOENTRY;
}
Log(LOG_LEVEL_ERR,
Expand All @@ -38,9 +36,6 @@ FSAttrsResult FileGetImmutableFlag(const char *filename, bool *flag)
{
if (errno == ENOTTY)
{
/* The specified operation does not apply to the kind of object
that the file descriptor fd references.
- ioctl(2)) */
return FS_ATTRS_NOTSUPP;
}

Expand All @@ -56,7 +51,31 @@ FSAttrsResult FileGetImmutableFlag(const char *filename, bool *flag)
*flag = attrs & FS_IMMUTABLE_FL;
return FS_ATTRS_SUCCESS;

#else /* TODO: Handle macOS, AIX, HP-UX */
#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 All @@ -74,8 +93,6 @@ FSAttrsResult FileUpdateImmutableFlag(const char *filename, bool flag)
{
if (errno == ENOENT)
{
/* O_CREAT is not set and the named file does not exist.
- open(2) */
return FS_ATTRS_NOENTRY;
}
Log(LOG_LEVEL_ERR,
Expand All @@ -91,9 +108,6 @@ FSAttrsResult FileUpdateImmutableFlag(const char *filename, bool flag)
{
if (errno == ENOTTY)
{
/* The specified operation does not apply to the kind of object
that the file descriptor fd references.
- ioctl(2)) */
close(fd);
return FS_ATTRS_NOTSUPP;
}
Expand Down Expand Up @@ -127,9 +141,6 @@ FSAttrsResult FileUpdateImmutableFlag(const char *filename, bool flag)
close(fd);
if (errno == ENOTTY)
{
/* The specified operation does not apply to the kind of object
that the file descriptor fd references.
- ioctl(2)) */
return FS_ATTRS_NOTSUPP;
}

Expand All @@ -143,7 +154,61 @@ FSAttrsResult FileUpdateImmutableFlag(const char *filename, bool flag)
close(fd);
return FS_ATTRS_SUCCESS;

#else /* TODO: Handle macOS, AIX, HP-UX */
#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 dde16da

Please sign in to comment.