Files
mars-nwe/src/nwarchive.c
Mario Fetka 5a701740fb
All checks were successful
Source release / source-package (push) Successful in 36s
Xattr remove empty
2026-05-25 21:23:02 +02:00

149 lines
3.9 KiB
C

/* nwarchive.c - mars_nwe private archive metadata helpers */
#include "net.h"
#include "nwarchive.h"
#if XATTR_SUPPORT
#include <errno.h>
#include <sys/types.h>
#include <sys/xattr.h>
#endif
#define MARS_NWE_ARCHIVE_XATTR "user.mars_nwe.archive"
#define MARS_NWE_ARCHIVE_VERSION 1
typedef struct {
uint8 version;
uint8 flags;
uint8 archive_date[2];
uint8 archive_time[2];
uint8 archiver_id[4];
} MARS_NWE_ARCHIVE_XATTR_DATA;
static void init_archive_data(MARS_NWE_ARCHIVE_XATTR_DATA *d)
{
memset(d, 0, sizeof(*d));
d->version = MARS_NWE_ARCHIVE_VERSION;
}
static void read_archive_data(MARS_NWE_ARCHIVE_XATTR_DATA *d,
uint16 *archive_date,
uint16 *archive_time,
uint32 *archiver_id,
uint8 *flags)
{
if (flags)
*flags = d->flags;
if (archive_date)
*archive_date = (d->flags & MARS_NWE_ARCHIVE_HAS_DATE)
? (uint16)GET_16(d->archive_date) : 0;
if (archive_time)
*archive_time = (d->flags & MARS_NWE_ARCHIVE_HAS_TIME)
? (uint16)GET_16(d->archive_time) : 0;
if (archiver_id)
*archiver_id = (d->flags & MARS_NWE_ARCHIVE_HAS_ARCHIVER)
? GET_BE32(d->archiver_id) : 0;
}
#if XATTR_SUPPORT
static int get_archive_data(char *unixname, MARS_NWE_ARCHIVE_XATTR_DATA *d)
{
ssize_t len;
init_archive_data(d);
if (!unixname || !*unixname)
return(-1);
len = getxattr(unixname, MARS_NWE_ARCHIVE_XATTR, d, sizeof(*d));
if (len != sizeof(*d) || d->version != MARS_NWE_ARCHIVE_VERSION) {
init_archive_data(d);
return(-1);
}
return(0);
}
#endif
void mars_nwe_get_archive_info(char *unixname,
uint16 *archive_date,
uint16 *archive_time,
uint32 *archiver_id,
uint8 *flags)
{
MARS_NWE_ARCHIVE_XATTR_DATA d;
#if XATTR_SUPPORT
(void)get_archive_data(unixname, &d);
#else
(void)unixname;
init_archive_data(&d);
#endif
read_archive_data(&d, archive_date, archive_time, archiver_id, flags);
}
int mars_nwe_set_archive_info(char *unixname,
int set_date, uint16 archive_date,
int set_time, uint16 archive_time,
int set_archiver, uint32 archiver_id)
{
#if XATTR_SUPPORT
MARS_NWE_ARCHIVE_XATTR_DATA d;
(void)get_archive_data(unixname, &d);
if (set_date) {
U16_TO_16(archive_date, d.archive_date);
if (archive_date)
d.flags |= MARS_NWE_ARCHIVE_HAS_DATE;
else
d.flags &= (uint8)~MARS_NWE_ARCHIVE_HAS_DATE;
}
if (set_time) {
U16_TO_16(archive_time, d.archive_time);
if (archive_time)
d.flags |= MARS_NWE_ARCHIVE_HAS_TIME;
else
d.flags &= (uint8)~MARS_NWE_ARCHIVE_HAS_TIME;
}
if (set_archiver) {
U32_TO_BE32(archiver_id, d.archiver_id);
if (archiver_id)
d.flags |= MARS_NWE_ARCHIVE_HAS_ARCHIVER;
else
d.flags &= (uint8)~MARS_NWE_ARCHIVE_HAS_ARCHIVER;
}
if (!unixname || !*unixname)
return(0);
if (!d.flags) {
if (removexattr(unixname, MARS_NWE_ARCHIVE_XATTR)) {
int err = errno;
if (err != ENODATA
#ifdef ENOATTR
&& err != ENOATTR
#endif
)
XDPRINTF((5,0,"mars_nwe archive xattr remove ignored for %s errno=%d", unixname, err));
}
return(0);
}
if (setxattr(unixname, MARS_NWE_ARCHIVE_XATTR, &d, sizeof(d), 0)) {
int err = errno;
/* Compatibility rule: NetWare archive metadata has no POSIX field.
* If Linux xattrs are unavailable or rejected, keep the NCP call
* successful and only log that the extra metadata was not stored.
*/
XDPRINTF((5,0,"mars_nwe archive xattr ignored for %s errno=%d", unixname, err));
}
#else
(void)unixname;
(void)set_date;
(void)archive_date;
(void)set_time;
(void)archive_time;
(void)set_archiver;
(void)archiver_id;
#endif
return(0);
}