#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/attr.h>
#include <sys/stat.h>
#include <sys/unistd.h>
#include "hfs.h"
#include "hfs_cnode.h"
#include "hfs_mount.h"
#include "hfs_dbg.h"
#include "hfs_attrlist.h"
extern uid_t console_user;
extern int hfs_write_access(struct vnode *vp, struct ucred *cred,
struct proc *p, Boolean considerFlags);
extern int hfs_chflags(struct vnode *vp, u_long flags, struct ucred *cred,
struct proc *p);
extern int hfs_chmod(struct vnode *vp, int mode, struct ucred *cred,
struct proc *p);
extern int hfs_chown(struct vnode *vp, uid_t uid, gid_t gid, struct ucred *cred,
struct proc *p);
extern char * hfs_getnamehint(struct cnode *dcp, int index);
extern void hfs_savenamehint(struct cnode *dcp, int index, const char * namehint);
extern void hfs_relnamehint(struct cnode *dcp, int index);
static void packvolcommonattr(struct attrblock *abp, struct hfsmount *hfsmp,
struct vnode *vp);
static void packvolattr(struct attrblock *abp, struct hfsmount *hfsmp,
struct vnode *vp);
static void packcommonattr(struct attrblock *abp, struct hfsmount *hfsmp,
struct vnode *vp, struct cat_desc * cdp,
struct cat_attr * cap);
static void packfileattr(struct attrblock *abp, struct hfsmount *hfsmp,
struct cat_attr *cattrp, struct cat_fork *datafork,
struct cat_fork *rsrcfork);
static void packdirattr(struct attrblock *abp, struct hfsmount *hfsmp,
struct vnode *vp, struct cat_desc * descp,
struct cat_attr * cattrp);
static void unpackattrblk(struct attrblock *abp, struct vnode *vp);
static void unpackcommonattr(struct attrblock *abp, struct vnode *vp);
static void unpackvolattr(struct attrblock *abp, struct hfsmount *hfsmp,
struct vnode *rootvp);
__private_extern__
int
hfs_getattrlist(ap)
struct vop_getattrlist_args *ap;
{
struct vnode *vp = ap->a_vp;
struct cnode *cp = VTOC(vp);
struct hfsmount *hfsmp = VTOHFS(vp);
struct attrlist *alist = ap->a_alist;
struct timeval tv;
int fixedblocksize;
int attrblocksize;
int attrbufsize;
void *attrbufptr;
void *attrptr;
void *varptr;
struct attrblock attrblk;
struct cat_fork *datafp = NULL;
struct cat_fork *rsrcfp = NULL;
struct cat_fork rsrcfork = {0};
int error = 0;
if ((alist->bitmapcount != ATTR_BIT_MAP_COUNT) ||
((alist->commonattr & ~ATTR_CMN_VALIDMASK) != 0) ||
((alist->volattr & ~ATTR_VOL_VALIDMASK) != 0) ||
((alist->dirattr & ~ATTR_DIR_VALIDMASK) != 0) ||
((alist->fileattr & ~ATTR_FILE_VALIDMASK) != 0)) {
return (EINVAL);
}
if ((alist->volattr != 0) &&
(((alist->volattr & ATTR_VOL_INFO) == 0) ||
(alist->dirattr != 0) || (alist->fileattr != 0))) {
return (EINVAL);
}
if ((alist->commonattr & (ATTR_CMN_NAMEDATTRCOUNT | ATTR_CMN_NAMEDATTRLIST)) ||
(alist->fileattr & (ATTR_FILE_FILETYPE | ATTR_FILE_FORKCOUNT | ATTR_FILE_FORKLIST))) {
return (EINVAL);
}
if ((alist->volattr) && cp->c_fileid != kRootDirID)
return (EINVAL);
if (VNODE_IS_RSRC(vp) && (alist->fileattr & ATTR_DATAFORK_MASK))
return (EINVAL);
if (cp->c_flag & (C_NOEXISTS | C_DELETED))
return (ENOENT);
if ((cp->c_desc.cd_namelen == 0) && (alist->commonattr & ATTR_CMN_NAME))
return (ENOENT);
tv = time;
CTIMES(cp, &tv, &tv);
if ((vp->v_type == VREG) &&
(alist->commonattr & ATTR_CMN_OBJPERMANENTID) &&
(VTOVCB(vp)->vcbSigWord != kHFSPlusSigWord)) {
if (VTOVFS(vp)->mnt_flag & MNT_RDONLY)
return (EROFS);
if ((error = hfs_write_access(vp, ap->a_cred, ap->a_p, false)) != 0)
return (error);
hfs_global_shared_lock_acquire(hfsmp);
if (hfsmp->jnl) {
if ((error = journal_start_transaction(hfsmp->jnl)) != 0) {
hfs_global_shared_lock_release(hfsmp);
return error;
}
}
error = hfs_metafilelocking(hfsmp, kHFSCatalogFileID, LK_SHARED, ap->a_p);
if (error) {
if (hfsmp->jnl) {
journal_end_transaction(hfsmp->jnl);
}
hfs_global_shared_lock_release(hfsmp);
return (error);
}
error = cat_insertfilethread(hfsmp, &cp->c_desc);
(void) hfs_metafilelocking(hfsmp, kHFSCatalogFileID, LK_RELEASE, ap->a_p);
if (hfsmp->jnl) {
journal_end_transaction(hfsmp->jnl);
}
hfs_global_shared_lock_release(hfsmp);
if (error)
return (error);
}
if (cp->c_datafork != NULL) {
datafp = &cp->c_datafork->ff_data;
if ((cp->c_rsrcfork == NULL) &&
(cp->c_blocks == datafp->cf_blocks))
rsrcfp = &rsrcfork;
}
if (cp->c_rsrcfork != NULL)
rsrcfp = &cp->c_rsrcfork->ff_data;
if ((alist->fileattr & ATTR_RSRCFORK_MASK) && (rsrcfp == NULL)) {
error = hfs_metafilelocking(hfsmp, kHFSCatalogFileID, LK_SHARED, ap->a_p);
if (error)
return (error);
error = cat_lookup(hfsmp, &cp->c_desc, 1,
(struct cat_desc *)0, (struct cat_attr *)0, &rsrcfork);
(void) hfs_metafilelocking(hfsmp, kHFSCatalogFileID, LK_RELEASE, ap->a_p);
if (error)
return (error);
rsrcfp = &rsrcfork;
}
fixedblocksize = hfs_attrblksize(alist);
attrblocksize = fixedblocksize + (sizeof(u_long));
if (alist->commonattr & ATTR_CMN_NAME)
attrblocksize += kHFSPlusMaxFileNameBytes + 1;
if (alist->volattr & ATTR_VOL_MOUNTPOINT)
attrblocksize += PATH_MAX;
if (alist->volattr & ATTR_VOL_NAME)
attrblocksize += kHFSPlusMaxFileNameBytes + 1;
#if 0
if (alist->commonattr & ATTR_CMN_NAMEDATTRLIST)
attrblocksize += 0;
if (alist->fileattr & ATTR_FILE_FORKLIST)
attrblocksize += 0;
#endif
attrbufsize = MIN(ap->a_uio->uio_resid, attrblocksize);
MALLOC(attrbufptr, void *, attrblocksize, M_TEMP, M_WAITOK);
attrptr = attrbufptr;
*((u_long *)attrptr) = 0;
++((u_long *)attrptr);
varptr = ((char *)attrptr) + fixedblocksize;
attrblk.ab_attrlist = alist;
attrblk.ab_attrbufpp = &attrptr;
attrblk.ab_varbufpp = &varptr;
attrblk.ab_flags = 0;
attrblk.ab_blocksize = attrblocksize;
hfs_packattrblk(&attrblk, hfsmp, vp, &cp->c_desc, &cp->c_attr,
datafp, rsrcfp);
attrbufsize = MIN(attrbufsize, (u_int)varptr - (u_int)attrbufptr);
*((u_long *)attrbufptr) = attrbufsize;
error = uiomove((caddr_t)attrbufptr, attrbufsize, ap->a_uio);
FREE(attrbufptr, M_TEMP);
return (error);
}
__private_extern__
int
hfs_setattrlist(ap)
struct vop_setattrlist_args *ap;
{
struct vnode *vp = ap->a_vp;
struct cnode *cp = VTOC(vp);
struct hfsmount * hfsmp = VTOHFS(vp);
struct attrlist *alist = ap->a_alist;
struct ucred *cred = ap->a_cred;
struct proc *p = ap->a_p;
int attrblocksize;
void *attrbufptr = NULL;
void *attrptr;
void *varptr = NULL;
struct attrblock attrblk;
uid_t saved_uid;
gid_t saved_gid;
mode_t saved_mode;
u_long saved_flags;
int error = 0;
if (VTOVFS(vp)->mnt_flag & MNT_RDONLY)
return (EROFS);
if ((alist->bitmapcount != ATTR_BIT_MAP_COUNT) ||
((alist->commonattr & ~ATTR_CMN_SETMASK) != 0) ||
((alist->volattr & ~ATTR_VOL_SETMASK) != 0) ||
((alist->dirattr & ~ATTR_DIR_SETMASK) != 0) ||
((alist->fileattr & ~ATTR_FILE_SETMASK) != 0)) {
return (EINVAL);
}
if ((alist->volattr != 0) &&
(((alist->volattr & ATTR_VOL_INFO) == 0) ||
(alist->commonattr & ~ATTR_CMN_VOLSETMASK) ||
(cp->c_fileid != kRootDirID))) {
if ((alist->volattr & ATTR_VOL_INFO) == 0)
printf("hfs_setattrlist: you forgot to set ATTR_VOL_INFO bit!\n");
else
printf("hfs_setattrlist: you cannot set bits 0x%08X!\n",
alist->commonattr & ~ATTR_CMN_VOLSETMASK);
return (EINVAL);
}
if (cp->c_flag & (C_NOEXISTS | C_DELETED))
return (ENOENT);
if (hfsmp->jnl && cp->c_datafork) {
struct HFSPlusExtentDescriptor *extd;
extd = &cp->c_datafork->ff_data.cf_extents[0];
if (extd->startBlock == HFSTOVCB(hfsmp)->vcbJinfoBlock || extd->startBlock == hfsmp->jnl_start) {
return EPERM;
}
}
if ((alist->commonattr & (ATTR_OWNERSHIP_SETMASK & ~ATTR_CMN_FLAGS)) ||
((alist->commonattr & ATTR_CMN_FLAGS) &&
(VTOVCB(vp)->vcbSigWord != kHFSSigWord))) {
if ((error = hfs_owner_rights(hfsmp, cp->c_uid, cred, p, true)) != 0)
return (error);
}
if (((alist->commonattr & ~ATTR_OWNERSHIP_SETMASK) != 0) ||
(alist->volattr != 0) || (alist->dirattr != 0) ||
(alist->fileattr != 0)) {
if ((error = hfs_write_access(vp, cred, p, false)) != 0)
return (error);
}
attrblocksize = ap->a_uio->uio_resid;
if (attrblocksize < hfs_attrblksize(alist))
return (EINVAL);
MALLOC(attrbufptr, void *, attrblocksize, M_TEMP, M_WAITOK);
error = uiomove((caddr_t)attrbufptr, attrblocksize, ap->a_uio);
if (error)
goto ErrorExit;
saved_uid = cp->c_uid;
saved_gid = cp->c_gid;
saved_mode = cp->c_mode;
saved_flags = cp->c_flags;
attrptr = attrbufptr;
attrblk.ab_attrlist = alist;
attrblk.ab_attrbufpp = &attrptr;
attrblk.ab_varbufpp = &varptr;
attrblk.ab_flags = 0;
attrblk.ab_blocksize = attrblocksize;
unpackattrblk(&attrblk, vp);
if ((saved_uid != cp->c_uid) || (saved_gid != cp->c_gid)) {
uid_t uid;
gid_t gid;
uid = cp->c_uid;
cp->c_uid = saved_uid;
gid = cp->c_gid;
cp->c_gid = saved_gid;
if ((error = hfs_chown(vp, uid, gid, cred, p)))
goto ErrorExit;
}
if (saved_mode != cp->c_mode) {
mode_t mode;
mode = cp->c_mode;
cp->c_mode = saved_mode;
if ((error = hfs_chmod(vp, mode, cred, p)))
goto ErrorExit;
}
if (saved_flags !=cp->c_flags) {
u_long flags;
flags = cp->c_flags;
cp->c_flags = saved_flags;
if ((error = hfs_chflags(vp, flags, cred, p)))
goto ErrorExit;
}
if (alist->volattr == 0) {
struct timeval tv;
cp->c_flag |= C_MODIFIED;
tv = time;
CTIMES(cp, &tv, &tv);
if ((error = VOP_UPDATE(vp, &tv, &tv, 1)))
goto ErrorExit;
}
if (alist->volattr & ATTR_VOL_NAME) {
ExtendedVCB *vcb = VTOVCB(vp);
if (vcb->vcbVN[0] == 0) {
copystr(cp->c_desc.cd_nameptr, vcb->vcbVN, sizeof(vcb->vcbVN), NULL);
} else {
struct cat_desc to_desc = {0};
struct cat_desc todir_desc = {0};
struct cat_desc new_desc = {0};
todir_desc.cd_parentcnid = kRootParID;
todir_desc.cd_cnid = kRootParID;
todir_desc.cd_flags = CD_ISDIR;
to_desc.cd_nameptr = vcb->vcbVN;
to_desc.cd_namelen = strlen(vcb->vcbVN);
to_desc.cd_parentcnid = kRootParID;
to_desc.cd_cnid = cp->c_cnid;
to_desc.cd_flags = CD_ISDIR;
hfs_global_shared_lock_acquire(hfsmp);
if (hfsmp->jnl) {
if (journal_start_transaction(hfsmp->jnl) != 0) {
hfs_global_shared_lock_release(hfsmp);
error = EINVAL;
copystr(cp->c_desc.cd_nameptr, vcb->vcbVN, sizeof(vcb->vcbVN), NULL);
vcb->vcbFlags |= 0xFF00;
goto ErrorExit;
}
}
error = hfs_metafilelocking(hfsmp, kHFSCatalogFileID, LK_EXCLUSIVE, p);
if (error) {
if (hfsmp->jnl) {
journal_end_transaction(hfsmp->jnl);
}
hfs_global_shared_lock_release(hfsmp);
copystr(cp->c_desc.cd_nameptr, vcb->vcbVN, sizeof(vcb->vcbVN), NULL);
vcb->vcbFlags |= 0xFF00;
goto ErrorExit;
}
error = cat_rename(hfsmp, &cp->c_desc, &todir_desc, &to_desc, &new_desc);
(void) hfs_metafilelocking(hfsmp, kHFSCatalogFileID, LK_RELEASE, p);
if (hfsmp->jnl) {
journal_end_transaction(hfsmp->jnl);
}
hfs_global_shared_lock_release(hfsmp);
if (error) {
copystr(cp->c_desc.cd_nameptr, vcb->vcbVN, sizeof(vcb->vcbVN), NULL);
vcb->vcbFlags |= 0xFF00;
goto ErrorExit;
}
if (cp->c_desc.cd_flags & CD_HASBUF) {
char *name = cp->c_desc.cd_nameptr;
cp->c_desc.cd_nameptr = 0;
cp->c_desc.cd_namelen = 0;
cp->c_desc.cd_flags &= ~CD_HASBUF;
FREE(name, M_TEMP);
}
replace_desc(cp, &new_desc);
vcb->volumeNameEncodingHint = new_desc.cd_encoding;
cp->c_flag |= C_CHANGE;
}
}
if ((alist->volattr & ATTR_VOL_INFO) &&
((alist->volattr & ATTR_VOL_NAME) ||
(alist->commonattr & ATTR_CMN_FNDRINFO))) {
(void) hfs_flushvolumeheader(hfsmp, MNT_WAIT, 0);
}
ErrorExit:
if (attrbufptr)
FREE(attrbufptr, M_TEMP);
return (error);
}
__private_extern__
int
hfs_readdirattr(ap)
struct vop_readdirattr_args *ap;
{
struct vnode *dvp = ap->a_vp;
struct cnode *dcp = VTOC(dvp);
struct hfsmount * hfsmp = VTOHFS(dvp);
struct attrlist *alist = ap->a_alist;
struct uio *uio = ap->a_uio;
int maxcount = ap->a_maxcount;
struct proc *p = current_proc();
u_long fixedblocksize;
u_long maxattrblocksize;
u_long currattrbufsize;
void *attrbufptr = NULL;
void *attrptr;
void *varptr;
struct attrblock attrblk;
int error = 0;
int depleted = 0;
int index, startindex;
int i, dir_entries;
struct cat_desc *lastdescp = NULL;
struct cat_desc prevdesc;
char * prevnamebuf = NULL;
struct cat_entrylist *ce_list = NULL;
dir_entries = dcp->c_entries;
if (dcp->c_attr.ca_fileid == kHFSRootFolderID && hfsmp->jnl) {
dir_entries -= 3;
}
*(ap->a_actualcount) = 0;
*(ap->a_eofflag) = 0;
if (ap->a_cookies != NULL) {
printf("readdirattr: no cookies!\n");
return (EINVAL);
}
if (((ap->a_options & ~(FSOPT_NOINMEMUPDATE | FSOPT_NOFOLLOW)) != 0)
|| (uio->uio_resid <= 0) || (uio->uio_iovcnt > 1) || (maxcount <= 0))
return (EINVAL);
if ((alist->bitmapcount != ATTR_BIT_MAP_COUNT) ||
((alist->commonattr & ~ATTR_CMN_VALIDMASK) != 0) ||
(alist->volattr != 0) ||
((alist->dirattr & ~ATTR_DIR_VALIDMASK) != 0) ||
((alist->fileattr & ~ATTR_FILE_VALIDMASK) != 0))
return (EINVAL);
if ((alist->commonattr & (ATTR_CMN_NAMEDATTRCOUNT | ATTR_CMN_NAMEDATTRLIST |
ATTR_CMN_OBJPERMANENTID)) ||
(alist->fileattr & (ATTR_FILE_FILETYPE | ATTR_FILE_FORKCOUNT |
ATTR_FILE_FORKLIST | ATTR_FILE_DATAEXTENTS | ATTR_FILE_RSRCEXTENTS))) {
printf("readdirattr: unsupported attributes! (%s)\n", dcp->c_desc.cd_nameptr);
return (EINVAL);
}
startindex = index = uio->uio_offset / sizeof(struct dirent);
if ((index + 1) > dir_entries) {
*(ap->a_eofflag) = 1;
error = 0;
goto exit;
}
fixedblocksize = (sizeof(u_long) + hfs_attrblksize(alist));
maxattrblocksize = fixedblocksize;
if (alist->commonattr & ATTR_CMN_NAME)
maxattrblocksize += kHFSPlusMaxFileNameBytes + 1;
MALLOC(attrbufptr, void *, maxattrblocksize, M_TEMP, M_WAITOK);
attrptr = attrbufptr;
varptr = (char *)attrbufptr + fixedblocksize;
MALLOC(ce_list, struct cat_entrylist *, sizeof(*ce_list), M_TEMP, M_WAITOK);
bzero(ce_list, sizeof(*ce_list));
ce_list->maxentries = MAXCATENTRIES;
bzero(&prevdesc, sizeof(prevdesc));
prevdesc.cd_flags = CD_DECOMPOSED;
prevdesc.cd_hint = dcp->c_childhint;
prevdesc.cd_parentcnid = dcp->c_cnid;
prevdesc.cd_nameptr = hfs_getnamehint(dcp, index);
prevdesc.cd_namelen = prevdesc.cd_nameptr ? strlen(prevdesc.cd_nameptr) : 0;
while (!depleted) {
int maxentries;
maxentries = uio->uio_resid / (fixedblocksize + HFS_AVERAGE_NAME_SIZE);
maxentries = min(maxentries, dcp->c_entries - index);
maxentries = min(maxentries, maxcount);
ce_list->maxentries = min(maxentries, ce_list->maxentries);
lastdescp = NULL;
error = hfs_metafilelocking(hfsmp, kHFSCatalogFileID, LK_SHARED, p);
if (error)
goto exit;
error = cat_getentriesattr(hfsmp, &prevdesc, index, ce_list);
(void) hfs_metafilelocking(hfsmp, kHFSCatalogFileID, LK_RELEASE, p);
if (error == ENOENT) {
*(ap->a_eofflag) = TRUE;
error = 0;
depleted = 1;
}
if (error)
break;
for (i = 0; i < ce_list->realentries; ++i) {
struct cnode *cp = NULL;
struct vnode *vp = NULL;
struct vnode *rvp = NULL;
struct cat_desc * cdescp;
struct cat_attr * cattrp;
struct cat_fork c_datafork = {0};
struct cat_fork c_rsrcfork = {0};
cdescp = &ce_list->entry[i].ce_desc;
cattrp = &ce_list->entry[i].ce_attr;
c_datafork.cf_size = ce_list->entry[i].ce_datasize;
c_datafork.cf_blocks = ce_list->entry[i].ce_datablks;
c_rsrcfork.cf_size = ce_list->entry[i].ce_rsrcsize;
c_rsrcfork.cf_blocks = ce_list->entry[i].ce_rsrcblks;
if (!(ap->a_options & FSOPT_NOINMEMUPDATE)) {
cp = hfs_chashget(dcp->c_dev, cattrp->ca_fileid, 0, &vp, &rvp);
if (cp != NULL) {
if (!(cp->c_flag & C_HARDLINK))
cdescp = &cp->c_desc;
cattrp = &cp->c_attr;
if (cp->c_datafork) {
c_datafork.cf_size = cp->c_datafork->ff_data.cf_size;
c_datafork.cf_clump = cp->c_datafork->ff_data.cf_clump;
c_datafork.cf_blocks = cp->c_datafork->ff_data.cf_blocks;
}
if (cp->c_rsrcfork) {
c_rsrcfork.cf_size = cp->c_rsrcfork->ff_data.cf_size;
c_rsrcfork.cf_clump = cp->c_rsrcfork->ff_data.cf_clump;
c_rsrcfork.cf_blocks = cp->c_rsrcfork->ff_data.cf_blocks;
}
}
}
*((u_long *)attrptr)++ = 0;
attrblk.ab_attrlist = alist;
attrblk.ab_attrbufpp = &attrptr;
attrblk.ab_varbufpp = &varptr;
attrblk.ab_flags = 0;
attrblk.ab_blocksize = maxattrblocksize;
hfs_packattrblk(&attrblk, hfsmp, vp, cdescp, cattrp,
&c_datafork, &c_rsrcfork);
currattrbufsize = ((char *)varptr - (char *)attrbufptr);
if (vp) {
vput(vp);
vp = NULL;
} else if (rvp) {
vput(rvp);
rvp = NULL;
}
cp = NULL;
if (currattrbufsize > uio->uio_resid) {
depleted = 1;
break;
} else {
*((u_long *)attrbufptr) = currattrbufsize;
error = uiomove((caddr_t)attrbufptr, currattrbufsize, ap->a_uio);
if (error != E_NONE) {
depleted = 1;
break;
}
attrptr = attrbufptr;
varptr = (char *)attrbufptr + fixedblocksize;
lastdescp = &ce_list->entry[i].ce_desc;
index++;
*ap->a_actualcount += 1;
if ((--maxcount <= 0) ||
(uio->uio_resid < (fixedblocksize + HFS_AVERAGE_NAME_SIZE)) ||
(index >= dir_entries)) {
depleted = 1;
break;
}
}
}
if (index < dir_entries
&& !(*(ap->a_eofflag))
&& lastdescp != NULL) {
if (prevnamebuf == NULL)
MALLOC(prevnamebuf, char *, kHFSPlusMaxFileNameBytes + 1, M_TEMP, M_WAITOK);
bcopy(lastdescp->cd_nameptr, prevnamebuf, lastdescp->cd_namelen + 1);
if (!depleted) {
prevdesc.cd_hint = lastdescp->cd_hint;
prevdesc.cd_nameptr = prevnamebuf;
prevdesc.cd_namelen = lastdescp->cd_namelen + 1;
}
}
for (i = 0; i < ce_list->realentries; ++i)
cat_releasedesc(&ce_list->entry[i].ce_desc);
ce_list->realentries = 0;
}
*ap->a_newstate = dcp->c_mtime;
hfs_relnamehint(dcp, startindex);
startindex = 0;
uio->uio_offset = index * sizeof(struct dirent);
if ((error == 0) && prevnamebuf && (index + 1) < dcp->c_entries)
hfs_savenamehint(dcp, index, prevnamebuf);
exit:
if (startindex > 0)
hfs_relnamehint(dcp, startindex);
if (attrbufptr)
FREE(attrbufptr, M_TEMP);
if (ce_list)
FREE(ce_list, M_TEMP);
if (prevnamebuf)
FREE(prevnamebuf, M_TEMP);
return (error);
}
__private_extern__
void
hfs_packattrblk(struct attrblock *abp,
struct hfsmount *hfsmp,
struct vnode *vp,
struct cat_desc *descp,
struct cat_attr *attrp,
struct cat_fork *datafork,
struct cat_fork *rsrcfork)
{
struct attrlist *attrlistp = abp->ab_attrlist;
if (attrlistp->volattr) {
if (attrlistp->commonattr)
packvolcommonattr(abp, hfsmp, vp);
if (attrlistp->volattr & ~ATTR_VOL_INFO)
packvolattr(abp, hfsmp, vp);
} else {
if (attrlistp->commonattr)
packcommonattr(abp, hfsmp, vp, descp, attrp);
if (attrlistp->dirattr && S_ISDIR(attrp->ca_mode))
packdirattr(abp, hfsmp, vp, descp,attrp);
if (attrlistp->fileattr && !S_ISDIR(attrp->ca_mode))
packfileattr(abp, hfsmp, attrp, datafork, rsrcfork);
}
}
static char*
mountpointname(struct mount *mp)
{
size_t namelength = strlen(mp->mnt_stat.f_mntonname);
int foundchars = 0;
char *c;
if (namelength == 0)
return (NULL);
for (c = mp->mnt_stat.f_mntonname + namelength - 1;
namelength > 0; --c, --namelength) {
if (*c != '/') {
foundchars = 1;
} else if (foundchars) {
return (c + 1);
}
}
return (mp->mnt_stat.f_mntonname);
}
static void
packnameattr(
struct attrblock *abp,
struct vnode *vp,
char *name,
int namelen)
{
void *varbufptr;
struct attrreference * attr_refptr;
char *mpname;
size_t mpnamelen;
u_long attrlength;
char empty = 0;
if ((vp != NULL) && (vp->v_flag & VROOT) &&
(mpname = mountpointname(vp->v_mount))) {
mpnamelen = strlen(mpname);
while ((mpnamelen > 0) && (mpname[mpnamelen-1] == '/'))
--mpnamelen;
if (mpnamelen > 0) {
name = mpname;
namelen = mpnamelen;
}
}
if (name == NULL) {
name = ∅
namelen = 0;
}
varbufptr = *abp->ab_varbufpp;
attr_refptr = (struct attrreference *)(*abp->ab_attrbufpp);
attrlength = namelen + 1;
attr_refptr->attr_dataoffset = (char *)varbufptr - (char *)attr_refptr;
attr_refptr->attr_length = attrlength;
(void) strncpy((unsigned char *)varbufptr, name, attrlength);
(char *)(varbufptr) += attrlength + ((4 - (attrlength & 3)) & 3);
++attr_refptr;
*abp->ab_attrbufpp = attr_refptr;
*abp->ab_varbufpp = varbufptr;
}
static void
packvolcommonattr(struct attrblock *abp, struct hfsmount *hfsmp, struct vnode *vp)
{
attrgroup_t attr;
void *attrbufptr = *abp->ab_attrbufpp;
void *varbufptr = *abp->ab_varbufpp;
struct cnode *cp = VTOC(vp);
struct mount *mp = VTOVFS(vp);
ExtendedVCB *vcb = HFSTOVCB(hfsmp);
u_long attrlength;
attr = abp->ab_attrlist->commonattr;
if (ATTR_CMN_NAME & attr) {
packnameattr(abp, vp, cp->c_desc.cd_nameptr, cp->c_desc.cd_namelen);
attrbufptr = *abp->ab_attrbufpp;
varbufptr = *abp->ab_varbufpp;
}
if (ATTR_CMN_DEVID & attr) {
*((dev_t *)attrbufptr)++ = hfsmp->hfs_raw_dev;
}
if (ATTR_CMN_FSID & attr) {
*((fsid_t *)attrbufptr) = mp->mnt_stat.f_fsid;
++((fsid_t *)attrbufptr);
}
if (ATTR_CMN_OBJTYPE & attr) {
*((fsobj_type_t *)attrbufptr)++ = 0;
}
if (ATTR_CMN_OBJTAG & attr) {
*((fsobj_tag_t *)attrbufptr)++ = VT_HFS;
}
if (ATTR_CMN_OBJID & attr) {
((fsobj_id_t *)attrbufptr)->fid_objno = 0;
((fsobj_id_t *)attrbufptr)->fid_generation = 0;
++((fsobj_id_t *)attrbufptr);
}
if (ATTR_CMN_OBJPERMANENTID & attr) {
((fsobj_id_t *)attrbufptr)->fid_objno = 0;
((fsobj_id_t *)attrbufptr)->fid_generation = 0;
++((fsobj_id_t *)attrbufptr);
}
if (ATTR_CMN_PAROBJID & attr) {
((fsobj_id_t *)attrbufptr)->fid_objno = 0;
((fsobj_id_t *)attrbufptr)->fid_generation = 0;
++((fsobj_id_t *)attrbufptr);
}
if (ATTR_CMN_SCRIPT & attr) {
u_long encoding;
if (vcb->vcbSigWord == kHFSPlusSigWord)
encoding = vcb->volumeNameEncodingHint;
else
encoding = hfsmp->hfs_encoding;
*((text_encoding_t *)attrbufptr)++ = encoding;
}
if (ATTR_CMN_CRTIME & attr) {
((struct timespec *)attrbufptr)->tv_sec = vcb->vcbCrDate;
((struct timespec *)attrbufptr)->tv_nsec = 0;
++((struct timespec *)attrbufptr);
}
if (ATTR_CMN_MODTIME & attr) {
((struct timespec *)attrbufptr)->tv_sec = vcb->vcbLsMod;
((struct timespec *)attrbufptr)->tv_nsec = 0;
++((struct timespec *)attrbufptr);
}
if (ATTR_CMN_CHGTIME & attr) {
((struct timespec *)attrbufptr)->tv_sec = vcb->vcbLsMod;
((struct timespec *)attrbufptr)->tv_nsec = 0;
++((struct timespec *)attrbufptr);
}
if (ATTR_CMN_ACCTIME & attr) {
((struct timespec *)attrbufptr)->tv_sec = vcb->vcbLsMod;
((struct timespec *)attrbufptr)->tv_nsec = 0;
++((struct timespec *)attrbufptr);
}
if (ATTR_CMN_BKUPTIME & attr) {
((struct timespec *)attrbufptr)->tv_sec = vcb->vcbVolBkUp;
((struct timespec *)attrbufptr)->tv_nsec = 0;
++((struct timespec *)attrbufptr);
}
if (ATTR_CMN_FNDRINFO & attr) {
bcopy (&vcb->vcbFndrInfo, attrbufptr, sizeof(vcb->vcbFndrInfo));
(char *)attrbufptr += sizeof(vcb->vcbFndrInfo);
}
if (ATTR_CMN_OWNERID & attr) {
if (cp->c_uid == UNKNOWNUID)
*((uid_t *)attrbufptr)++ = console_user;
else
*((uid_t *)attrbufptr)++ = cp->c_uid;
}
if (ATTR_CMN_GRPID & attr) {
*((gid_t *)attrbufptr)++ = cp->c_gid;
}
if (ATTR_CMN_ACCESSMASK & attr) {
*((u_long *)attrbufptr)++ =
(cp->c_uid == UNKNOWNUID) ? cp->c_mode & ~(S_ISUID | S_ISGID) : cp->c_mode;
}
if (ATTR_CMN_NAMEDATTRCOUNT & attr) {
*((u_long *)attrbufptr)++ = 0;
}
if (ATTR_CMN_NAMEDATTRLIST & attr) {
attrlength = 0;
((struct attrreference *)attrbufptr)->attr_dataoffset = 0;
((struct attrreference *)attrbufptr)->attr_length = attrlength;
(char *)varbufptr += attrlength + ((4 - (attrlength & 3)) & 3);
++((struct attrreference *)attrbufptr);
}
if (ATTR_CMN_FLAGS & attr) {
*((u_long *)attrbufptr)++ = cp->c_flags;
}
if (ATTR_CMN_USERACCESS & attr) {
*((u_long *)attrbufptr)++ =
DerivePermissionSummary(cp->c_uid, cp->c_gid, cp->c_mode,
VTOVFS(vp), current_proc()->p_ucred, current_proc());
}
*abp->ab_attrbufpp = attrbufptr;
*abp->ab_varbufpp = varbufptr;
}
static void
packvolattr(struct attrblock *abp, struct hfsmount *hfsmp, struct vnode *vp)
{
attrgroup_t attr;
void *attrbufptr = *abp->ab_attrbufpp;
void *varbufptr = *abp->ab_varbufpp;
struct cnode *cp = VTOC(vp);
struct mount *mp = VTOVFS(vp);
ExtendedVCB *vcb = HFSTOVCB(hfsmp);
u_long attrlength;
attr = abp->ab_attrlist->volattr;
if (ATTR_VOL_FSTYPE & attr) {
*((u_long *)attrbufptr)++ = (u_long)mp->mnt_vfc->vfc_typenum;
}
if (ATTR_VOL_SIGNATURE & attr) {
*((u_long *)attrbufptr)++ = (u_long)vcb->vcbSigWord;
}
if (ATTR_VOL_SIZE & attr) {
*((off_t *)attrbufptr)++ =
(off_t)vcb->totalBlocks * (off_t)vcb->blockSize;
}
if (ATTR_VOL_SPACEFREE & attr) {
*((off_t *)attrbufptr)++ = (off_t)hfs_freeblks(hfsmp, 0) *
(off_t)vcb->blockSize;
}
if (ATTR_VOL_SPACEAVAIL & attr) {
*((off_t *)attrbufptr)++ = (off_t)hfs_freeblks(hfsmp, 1) *
(off_t)vcb->blockSize;
}
if (ATTR_VOL_MINALLOCATION & attr) {
*((off_t *)attrbufptr)++ = (off_t)vcb->blockSize;
}
if (ATTR_VOL_ALLOCATIONCLUMP & attr) {
*((off_t *)attrbufptr)++ = (off_t)(vcb->vcbClpSiz);
}
if (ATTR_VOL_IOBLOCKSIZE & attr) {
*((u_long *)attrbufptr)++ = (u_long)hfsmp->hfs_logBlockSize;
}
if (ATTR_VOL_OBJCOUNT & attr) {
*((u_long *)attrbufptr)++ =
(u_long)vcb->vcbFilCnt + (u_long)vcb->vcbDirCnt;
}
if (ATTR_VOL_FILECOUNT & attr) {
*((u_long *)attrbufptr)++ = (u_long)vcb->vcbFilCnt;
}
if (ATTR_VOL_DIRCOUNT & attr) {
*((u_long *)attrbufptr)++ = (u_long)vcb->vcbDirCnt;
}
if (ATTR_VOL_MAXOBJCOUNT & attr) {
*((u_long *)attrbufptr)++ = 0xFFFFFFFF;
}
if (ATTR_VOL_MOUNTPOINT & attr) {
((struct attrreference *)attrbufptr)->attr_dataoffset =
(char *)varbufptr - (char *)attrbufptr;
((struct attrreference *)attrbufptr)->attr_length =
strlen(mp->mnt_stat.f_mntonname) + 1;
attrlength = ((struct attrreference *)attrbufptr)->attr_length;
attrlength = attrlength + ((4 - (attrlength & 3)) & 3);
(void) bcopy(mp->mnt_stat.f_mntonname, varbufptr, attrlength);
(char *)varbufptr += attrlength;
++((struct attrreference *)attrbufptr);
}
if (ATTR_VOL_NAME & attr) {
((struct attrreference *)attrbufptr)->attr_dataoffset =
(char *)varbufptr - (char *)attrbufptr;
((struct attrreference *)attrbufptr)->attr_length =
cp->c_desc.cd_namelen + 1;
attrlength = ((struct attrreference *)attrbufptr)->attr_length;
attrlength = attrlength + ((4 - (attrlength & 3)) & 3);
bcopy(cp->c_desc.cd_nameptr, varbufptr, attrlength);
(char *)varbufptr += attrlength;
++((struct attrreference *)attrbufptr);
}
if (ATTR_VOL_MOUNTFLAGS & attr) {
*((u_long *)attrbufptr)++ = (u_long)mp->mnt_flag;
}
if (ATTR_VOL_MOUNTEDDEVICE & attr) {
((struct attrreference *)attrbufptr)->attr_dataoffset =
(char *)varbufptr - (char *)attrbufptr;
((struct attrreference *)attrbufptr)->attr_length =
strlen(mp->mnt_stat.f_mntfromname) + 1;
attrlength = ((struct attrreference *)attrbufptr)->attr_length;
attrlength = attrlength + ((4 - (attrlength & 3)) & 3);
(void) bcopy(mp->mnt_stat.f_mntfromname, varbufptr, attrlength);
(char *)varbufptr += attrlength;
++((struct attrreference *)attrbufptr);
}
if (ATTR_VOL_ENCODINGSUSED & attr) {
*((unsigned long long *)attrbufptr)++ =
(unsigned long long)vcb->encodingsBitmap;
}
if (ATTR_VOL_CAPABILITIES & attr) {
vol_capabilities_attr_t *vcapattrptr;
vcapattrptr = (vol_capabilities_attr_t *)attrbufptr;
if (vcb->vcbSigWord == kHFSPlusSigWord) {
vcapattrptr->capabilities[VOL_CAPABILITIES_FORMAT] =
VOL_CAP_FMT_PERSISTENTOBJECTIDS |
VOL_CAP_FMT_SYMBOLICLINKS |
VOL_CAP_FMT_HARDLINKS;
} else {
vcapattrptr->capabilities[VOL_CAPABILITIES_FORMAT] =
VOL_CAP_FMT_PERSISTENTOBJECTIDS;
}
vcapattrptr->capabilities[VOL_CAPABILITIES_INTERFACES] =
VOL_CAP_INT_SEARCHFS |
VOL_CAP_INT_ATTRLIST |
VOL_CAP_INT_NFSEXPORT |
VOL_CAP_INT_READDIRATTR ;
vcapattrptr->capabilities[VOL_CAPABILITIES_RESERVED1] = 0;
vcapattrptr->capabilities[VOL_CAPABILITIES_RESERVED2] = 0;
vcapattrptr->valid[VOL_CAPABILITIES_FORMAT] =
VOL_CAP_FMT_PERSISTENTOBJECTIDS |
VOL_CAP_FMT_SYMBOLICLINKS |
VOL_CAP_FMT_HARDLINKS;
vcapattrptr->valid[VOL_CAPABILITIES_INTERFACES] =
VOL_CAP_INT_SEARCHFS |
VOL_CAP_INT_ATTRLIST |
VOL_CAP_INT_NFSEXPORT |
VOL_CAP_INT_READDIRATTR ;
vcapattrptr->valid[VOL_CAPABILITIES_RESERVED1] = 0;
vcapattrptr->valid[VOL_CAPABILITIES_RESERVED2] = 0;
++((vol_capabilities_attr_t *)attrbufptr);
}
if (ATTR_VOL_ATTRIBUTES & attr) {
vol_attributes_attr_t *volattrattrp;
volattrattrp = (vol_attributes_attr_t *)attrbufptr;
volattrattrp->validattr.commonattr = ATTR_CMN_VALIDMASK;
volattrattrp->validattr.volattr = ATTR_VOL_VALIDMASK;
volattrattrp->validattr.dirattr = ATTR_DIR_VALIDMASK;
volattrattrp->validattr.fileattr = ATTR_FILE_VALIDMASK;
volattrattrp->validattr.forkattr = ATTR_FORK_VALIDMASK;
volattrattrp->nativeattr.commonattr = ATTR_CMN_VALIDMASK;
volattrattrp->nativeattr.volattr = ATTR_VOL_VALIDMASK;
volattrattrp->nativeattr.dirattr = ATTR_DIR_VALIDMASK;
volattrattrp->nativeattr.fileattr = ATTR_FILE_VALIDMASK;
volattrattrp->nativeattr.forkattr = ATTR_FORK_VALIDMASK;
++((vol_attributes_attr_t *)attrbufptr);
}
*abp->ab_attrbufpp = attrbufptr;
*abp->ab_varbufpp = varbufptr;
}
static void
packcommonattr(
struct attrblock *abp,
struct hfsmount *hfsmp,
struct vnode *vp,
struct cat_desc * cdp,
struct cat_attr * cap)
{
attrgroup_t attr = abp->ab_attrlist->commonattr;
struct mount *mp = HFSTOVFS(hfsmp);
void *attrbufptr = *abp->ab_attrbufpp;
void *varbufptr = *abp->ab_varbufpp;
u_long attrlength = 0;
if (ATTR_CMN_NAME & attr) {
packnameattr(abp, vp, cdp->cd_nameptr, cdp->cd_namelen);
attrbufptr = *abp->ab_attrbufpp;
varbufptr = *abp->ab_varbufpp;
}
if (ATTR_CMN_DEVID & attr) {
*((dev_t *)attrbufptr)++ = hfsmp->hfs_raw_dev;
}
if (ATTR_CMN_FSID & attr) {
*((fsid_t *)attrbufptr) = mp->mnt_stat.f_fsid;
++((fsid_t *)attrbufptr);
}
if (ATTR_CMN_OBJTYPE & attr) {
*((fsobj_type_t *)attrbufptr)++ = IFTOVT(cap->ca_mode);
}
if (ATTR_CMN_OBJTAG & attr) {
*((fsobj_tag_t *)attrbufptr)++ = VT_HFS;
}
if (ATTR_CMN_OBJID & attr) {
((fsobj_id_t *)attrbufptr)->fid_objno = cdp->cd_cnid;
((fsobj_id_t *)attrbufptr)->fid_generation = 0;
++((fsobj_id_t *)attrbufptr);
}
if (ATTR_CMN_OBJPERMANENTID & attr) {
((fsobj_id_t *)attrbufptr)->fid_objno = cdp->cd_cnid;
((fsobj_id_t *)attrbufptr)->fid_generation = 0;
++((fsobj_id_t *)attrbufptr);
}
if (ATTR_CMN_PAROBJID & attr) {
((fsobj_id_t *)attrbufptr)->fid_objno = cdp->cd_parentcnid;
((fsobj_id_t *)attrbufptr)->fid_generation = 0;
++((fsobj_id_t *)attrbufptr);
}
if (ATTR_CMN_SCRIPT & attr) {
*((text_encoding_t *)attrbufptr)++ = cdp->cd_encoding;
}
if (ATTR_CMN_CRTIME & attr) {
((struct timespec *)attrbufptr)->tv_sec = cap->ca_itime;
((struct timespec *)attrbufptr)->tv_nsec = 0;
++((struct timespec *)attrbufptr);
}
if (ATTR_CMN_MODTIME & attr) {
((struct timespec *)attrbufptr)->tv_sec = cap->ca_mtime;
((struct timespec *)attrbufptr)->tv_nsec = 0;
++((struct timespec *)attrbufptr);
}
if (ATTR_CMN_CHGTIME & attr) {
((struct timespec *)attrbufptr)->tv_sec = cap->ca_ctime;
((struct timespec *)attrbufptr)->tv_nsec = 0;
++((struct timespec *)attrbufptr);
}
if (ATTR_CMN_ACCTIME & attr) {
((struct timespec *)attrbufptr)->tv_sec = cap->ca_atime;
((struct timespec *)attrbufptr)->tv_nsec = 0;
++((struct timespec *)attrbufptr);
}
if (ATTR_CMN_BKUPTIME & attr) {
((struct timespec *)attrbufptr)->tv_sec = cap->ca_btime;
((struct timespec *)attrbufptr)->tv_nsec = 0;
++((struct timespec *)attrbufptr);
}
if (ATTR_CMN_FNDRINFO & attr) {
bcopy(&cap->ca_finderinfo, attrbufptr, sizeof(u_int8_t) * 32);
(char *)attrbufptr += sizeof(u_int8_t) * 32;
}
if (ATTR_CMN_OWNERID & attr) {
*((uid_t *)attrbufptr)++ =
(cap->ca_uid == UNKNOWNUID) ? console_user : cap->ca_uid;
}
if (ATTR_CMN_GRPID & attr) {
*((gid_t *)attrbufptr)++ = cap->ca_gid;
}
if (ATTR_CMN_ACCESSMASK & attr) {
*((u_long *)attrbufptr)++ =
(cap->ca_uid == UNKNOWNUID) ? cap->ca_mode & ~(S_ISUID | S_ISGID) : cap->ca_mode;
}
if (ATTR_CMN_NAMEDATTRCOUNT & attr) {
*((u_long *)attrbufptr)++ = 0;
}
if (ATTR_CMN_NAMEDATTRLIST & attr) {
attrlength = 0;
((struct attrreference *)attrbufptr)->attr_dataoffset = 0;
((struct attrreference *)attrbufptr)->attr_length = attrlength;
(char *)varbufptr += attrlength + ((4 - (attrlength & 3)) & 3);
++((struct attrreference *)attrbufptr);
}
if (ATTR_CMN_FLAGS & attr) {
*((u_long *)attrbufptr)++ = cap->ca_flags;
}
if (ATTR_CMN_USERACCESS & attr) {
*((u_long *)attrbufptr)++ =
DerivePermissionSummary(cap->ca_uid, cap->ca_gid,
cap->ca_mode, mp, current_proc()->p_ucred,
current_proc());
}
*abp->ab_attrbufpp = attrbufptr;
*abp->ab_varbufpp = varbufptr;
}
static void
packdirattr(
struct attrblock *abp,
struct hfsmount *hfsmp,
struct vnode *vp,
struct cat_desc * descp,
struct cat_attr * cattrp)
{
attrgroup_t attr = abp->ab_attrlist->dirattr;
void *attrbufptr = *abp->ab_attrbufpp;
if (ATTR_DIR_LINKCOUNT & attr)
*((u_long *)attrbufptr)++ = cattrp->ca_nlink;
if (ATTR_DIR_ENTRYCOUNT & attr) {
u_long entries = cattrp->ca_entries;
if (descp->cd_parentcnid == kRootParID) {
if (hfsmp->hfs_private_metadata_dir != 0)
--entries;
if (hfsmp->jnl)
entries -= 2;
}
*((u_long *)attrbufptr)++ = entries;
}
if (ATTR_DIR_MOUNTSTATUS & attr) {
if (vp != NULL && vp->v_mountedhere != NULL)
*((u_long *)attrbufptr)++ = DIR_MNTSTATUS_MNTPOINT;
else
*((u_long *)attrbufptr)++ = 0;
}
*abp->ab_attrbufpp = attrbufptr;
}
static void
packfileattr(
struct attrblock *abp,
struct hfsmount *hfsmp,
struct cat_attr *cattrp,
struct cat_fork *datafork,
struct cat_fork *rsrcfork)
{
attrgroup_t attr = abp->ab_attrlist->fileattr;
void *attrbufptr = *abp->ab_attrbufpp;
void *varbufptr = *abp->ab_varbufpp;
u_long attrlength;
u_long allocblksize;
allocblksize = HFSTOVCB(hfsmp)->blockSize;
if (ATTR_FILE_LINKCOUNT & attr) {
*((u_long *)attrbufptr)++ = cattrp->ca_nlink;
}
if (ATTR_FILE_TOTALSIZE & attr) {
*((off_t *)attrbufptr)++ = datafork->cf_size + rsrcfork->cf_size;
}
if (ATTR_FILE_ALLOCSIZE & attr) {
*((off_t *)attrbufptr)++ =
(off_t)cattrp->ca_blocks * (off_t)allocblksize;
}
if (ATTR_FILE_IOBLOCKSIZE & attr) {
*((u_long *)attrbufptr)++ = hfsmp->hfs_logBlockSize;
}
if (ATTR_FILE_CLUMPSIZE & attr) {
*((u_long *)attrbufptr)++ = datafork->cf_clump;
}
if (ATTR_FILE_DEVTYPE & attr) {
if (S_ISBLK(cattrp->ca_mode) || S_ISCHR(cattrp->ca_mode))
*((u_long *)attrbufptr)++ = (u_long)cattrp->ca_rdev;
else
*((u_long *)attrbufptr)++ = 0;
}
if (ATTR_FILE_FILETYPE & attr) {
*((u_long *)attrbufptr)++ = 0;
}
if (ATTR_FILE_FORKCOUNT & attr) {
*((u_long *)attrbufptr)++ = 2;
}
if (ATTR_FILE_FORKLIST & attr) {
attrlength = 0;
((struct attrreference *)attrbufptr)->attr_dataoffset = 0;
((struct attrreference *)attrbufptr)->attr_length = attrlength;
(char *)varbufptr += attrlength + ((4 - (attrlength & 3)) & 3);
++((struct attrreference *)attrbufptr);
}
if (ATTR_FILE_DATALENGTH & attr) {
*((off_t *)attrbufptr)++ = datafork->cf_size;
}
if (ATTR_FILE_DATAALLOCSIZE & attr) {
*((off_t *)attrbufptr)++ =
(off_t)datafork->cf_blocks * (off_t)allocblksize;
}
if (ATTR_FILE_DATAEXTENTS & attr) {
bcopy(&datafork->cf_extents, attrbufptr, sizeof(extentrecord));
(char *)attrbufptr += sizeof(extentrecord);
}
if (ATTR_FILE_RSRCLENGTH & attr) {
*((off_t *)attrbufptr)++ = rsrcfork->cf_size;
}
if (ATTR_FILE_RSRCALLOCSIZE & attr) {
*((off_t *)attrbufptr)++ =
(off_t)rsrcfork->cf_blocks * (off_t)allocblksize;
}
if (ATTR_FILE_RSRCEXTENTS & attr) {
bcopy(&rsrcfork->cf_extents, attrbufptr, sizeof(extentrecord));
(char *)attrbufptr += sizeof(extentrecord);
}
*abp->ab_attrbufpp = attrbufptr;
*abp->ab_varbufpp = varbufptr;
}
static void
unpackattrblk(struct attrblock *abp, struct vnode *vp)
{
struct attrlist *attrlistp = abp->ab_attrlist;
if (attrlistp->volattr)
unpackvolattr(abp, VTOHFS(vp), vp);
else if (attrlistp->commonattr)
unpackcommonattr(abp, vp);
}
static void
unpackcommonattr(
struct attrblock *abp,
struct vnode *vp)
{
attrgroup_t attr = abp->ab_attrlist->commonattr;
void *attrbufptr = *abp->ab_attrbufpp;
struct cnode *cp = VTOC(vp);
if (ATTR_CMN_SCRIPT & attr) {
cp->c_encoding = (u_int32_t)*((text_encoding_t *)attrbufptr)++;
hfs_setencodingbits(VTOHFS(vp), cp->c_encoding);
}
if (ATTR_CMN_CRTIME & attr) {
cp->c_itime = ((struct timespec *)attrbufptr)->tv_sec;
++((struct timespec *)attrbufptr);
}
if (ATTR_CMN_MODTIME & attr) {
cp->c_mtime = ((struct timespec *)attrbufptr)->tv_sec;
cp->c_mtime_nsec = ((struct timespec *)attrbufptr)->tv_nsec;
++((struct timespec *)attrbufptr);
cp->c_flag &= ~C_UPDATE;
}
if (ATTR_CMN_CHGTIME & attr) {
cp->c_ctime = ((struct timespec *)attrbufptr)->tv_sec;
++((struct timespec *)attrbufptr);
cp->c_flag &= ~C_CHANGE;
}
if (ATTR_CMN_ACCTIME & attr) {
cp->c_atime = ((struct timespec *)attrbufptr)->tv_sec;
++((struct timespec *)attrbufptr);
cp->c_flag &= ~C_ACCESS;
}
if (ATTR_CMN_BKUPTIME & attr) {
cp->c_btime = ((struct timespec *)attrbufptr)->tv_sec;
++((struct timespec *)attrbufptr);
}
if (ATTR_CMN_FNDRINFO & attr) {
bcopy(attrbufptr, &cp->c_attr.ca_finderinfo,
sizeof(cp->c_attr.ca_finderinfo));
(char *)attrbufptr += sizeof(cp->c_attr.ca_finderinfo);
}
if (ATTR_CMN_OWNERID & attr) {
if (VTOVCB(vp)->vcbSigWord == kHFSPlusSigWord) {
u_int32_t uid = (u_int32_t)*((uid_t *)attrbufptr)++;
if (uid != (uid_t)VNOVAL)
cp->c_uid = uid;
} else {
((uid_t *)attrbufptr)++;
}
}
if (ATTR_CMN_GRPID & attr) {
u_int32_t gid = (u_int32_t)*((gid_t *)attrbufptr)++;
if (VTOVCB(vp)->vcbSigWord == kHFSPlusSigWord) {
if (gid != (gid_t)VNOVAL)
cp->c_gid = gid;
}
}
if (ATTR_CMN_ACCESSMASK & attr) {
u_int16_t mode = (u_int16_t)*((u_long *)attrbufptr)++;
if (VTOVCB(vp)->vcbSigWord == kHFSPlusSigWord) {
if (mode != (mode_t)VNOVAL) {
cp->c_mode &= ~ALLPERMS;
cp->c_mode |= (mode & ALLPERMS);
}
}
}
if (ATTR_CMN_FLAGS & attr) {
u_long flags = *((u_long *)attrbufptr)++;
if ((VTOVCB(vp)->vcbSigWord == kHFSPlusSigWord) ||
((VTOVCB(vp)->vcbSigWord == kHFSSigWord) &&
((flags & ~IMMUTABLE) == 0))) {
if (flags != (u_long)VNOVAL) {
cp->c_flags = flags;
}
}
}
*abp->ab_attrbufpp = attrbufptr;
}
static void
unpackvolattr(
struct attrblock *abp,
struct hfsmount *hfsmp,
struct vnode *rootvp)
{
void *attrbufptr = *abp->ab_attrbufpp;
ExtendedVCB *vcb = HFSTOVCB(hfsmp);
attrgroup_t attr;
attr = abp->ab_attrlist->commonattr;
if (attr == 0)
goto volattr;
if (ATTR_CMN_SCRIPT & attr) {
vcb->volumeNameEncodingHint =
(u_int32_t)*(((text_encoding_t *)attrbufptr)++);
}
if (ATTR_CMN_CRTIME & attr) {
vcb->vcbCrDate = ((struct timespec *)attrbufptr)->tv_sec;
++((struct timespec *)attrbufptr);
VTOC(rootvp)->c_itime = vcb->vcbCrDate;
VTOC(rootvp)->c_flag |= C_MODIFIED;
}
if (ATTR_CMN_MODTIME & attr) {
vcb->vcbLsMod = ((struct timespec *)attrbufptr)->tv_sec;
++((struct timespec *)attrbufptr);
}
if (ATTR_CMN_BKUPTIME & attr) {
vcb->vcbVolBkUp = ((struct timespec *)attrbufptr)->tv_sec;
++((struct timespec *)attrbufptr);
}
if (ATTR_CMN_FNDRINFO & attr) {
bcopy(attrbufptr, &vcb->vcbFndrInfo, sizeof(vcb->vcbFndrInfo));
(char *)attrbufptr += sizeof(vcb->vcbFndrInfo);
}
volattr:
attr = abp->ab_attrlist->volattr & ~ATTR_VOL_INFO;
if (ATTR_VOL_NAME & attr) {
copystr(((char *)attrbufptr) + *((u_long *)attrbufptr),
vcb->vcbVN, sizeof(vcb->vcbVN), NULL);
(char *)attrbufptr += sizeof(struct attrreference);
}
*abp->ab_attrbufpp = attrbufptr;
vcb->vcbFlags |= 0xFF00;
}
__private_extern__
int
hfs_attrblksize(struct attrlist *attrlist)
{
int size;
attrgroup_t a;
#if ((ATTR_CMN_NAME | ATTR_CMN_DEVID | ATTR_CMN_FSID | ATTR_CMN_OBJTYPE | \
ATTR_CMN_OBJTAG | ATTR_CMN_OBJID | ATTR_CMN_OBJPERMANENTID | \
ATTR_CMN_PAROBJID | ATTR_CMN_SCRIPT | ATTR_CMN_CRTIME | \
ATTR_CMN_MODTIME | ATTR_CMN_CHGTIME | ATTR_CMN_ACCTIME | \
ATTR_CMN_BKUPTIME | ATTR_CMN_FNDRINFO | ATTR_CMN_OWNERID | \
ATTR_CMN_GRPID | ATTR_CMN_ACCESSMASK | ATTR_CMN_NAMEDATTRCOUNT | \
ATTR_CMN_NAMEDATTRLIST | ATTR_CMN_FLAGS | ATTR_CMN_USERACCESS) \
!= ATTR_CMN_VALIDMASK)
#error hfs_attrblksize: Missing bits in common mask computation!
#endif
DBG_ASSERT((attrlist->commonattr & ~ATTR_CMN_VALIDMASK) == 0);
#if ((ATTR_VOL_FSTYPE | ATTR_VOL_SIGNATURE | ATTR_VOL_SIZE | \
ATTR_VOL_SPACEFREE | ATTR_VOL_SPACEAVAIL | ATTR_VOL_MINALLOCATION | \
ATTR_VOL_ALLOCATIONCLUMP | ATTR_VOL_IOBLOCKSIZE | \
ATTR_VOL_OBJCOUNT | ATTR_VOL_FILECOUNT | ATTR_VOL_DIRCOUNT | \
ATTR_VOL_MAXOBJCOUNT | ATTR_VOL_MOUNTPOINT | ATTR_VOL_NAME | \
ATTR_VOL_MOUNTFLAGS | ATTR_VOL_INFO | ATTR_VOL_MOUNTEDDEVICE | \
ATTR_VOL_ENCODINGSUSED | ATTR_VOL_CAPABILITIES | ATTR_VOL_ATTRIBUTES) \
!= ATTR_VOL_VALIDMASK)
#error hfs_attrblksize: Missing bits in volume mask computation!
#endif
DBG_ASSERT((attrlist->volattr & ~ATTR_VOL_VALIDMASK) == 0);
#if ((ATTR_DIR_LINKCOUNT | ATTR_DIR_ENTRYCOUNT | ATTR_DIR_MOUNTSTATUS) \
!= ATTR_DIR_VALIDMASK)
#error hfs_attrblksize: Missing bits in directory mask computation!
#endif
DBG_ASSERT((attrlist->dirattr & ~ATTR_DIR_VALIDMASK) == 0);
#if ((ATTR_FILE_LINKCOUNT | ATTR_FILE_TOTALSIZE | ATTR_FILE_ALLOCSIZE | \
ATTR_FILE_IOBLOCKSIZE | ATTR_FILE_CLUMPSIZE | ATTR_FILE_DEVTYPE | \
ATTR_FILE_FILETYPE | ATTR_FILE_FORKCOUNT | ATTR_FILE_FORKLIST | \
ATTR_FILE_DATALENGTH | ATTR_FILE_DATAALLOCSIZE | ATTR_FILE_DATAEXTENTS | \
ATTR_FILE_RSRCLENGTH | ATTR_FILE_RSRCALLOCSIZE | ATTR_FILE_RSRCEXTENTS) \
!= ATTR_FILE_VALIDMASK)
#error hfs_attrblksize: Missing bits in file mask computation!
#endif
DBG_ASSERT((attrlist->fileattr & ~ATTR_FILE_VALIDMASK) == 0);
#if ((ATTR_FORK_TOTALSIZE | ATTR_FORK_ALLOCSIZE) != ATTR_FORK_VALIDMASK)
#error hfs_attrblksize: Missing bits in fork mask computation!
#endif
DBG_ASSERT((attrlist->forkattr & ~ATTR_FORK_VALIDMASK) == 0);
size = 0;
if ((a = attrlist->commonattr) != 0) {
if (a & ATTR_CMN_NAME) size += sizeof(struct attrreference);
if (a & ATTR_CMN_DEVID) size += sizeof(dev_t);
if (a & ATTR_CMN_FSID) size += sizeof(fsid_t);
if (a & ATTR_CMN_OBJTYPE) size += sizeof(fsobj_type_t);
if (a & ATTR_CMN_OBJTAG) size += sizeof(fsobj_tag_t);
if (a & ATTR_CMN_OBJID) size += sizeof(fsobj_id_t);
if (a & ATTR_CMN_OBJPERMANENTID) size += sizeof(fsobj_id_t);
if (a & ATTR_CMN_PAROBJID) size += sizeof(fsobj_id_t);
if (a & ATTR_CMN_SCRIPT) size += sizeof(text_encoding_t);
if (a & ATTR_CMN_CRTIME) size += sizeof(struct timespec);
if (a & ATTR_CMN_MODTIME) size += sizeof(struct timespec);
if (a & ATTR_CMN_CHGTIME) size += sizeof(struct timespec);
if (a & ATTR_CMN_ACCTIME) size += sizeof(struct timespec);
if (a & ATTR_CMN_BKUPTIME) size += sizeof(struct timespec);
if (a & ATTR_CMN_FNDRINFO) size += 32 * sizeof(u_int8_t);
if (a & ATTR_CMN_OWNERID) size += sizeof(uid_t);
if (a & ATTR_CMN_GRPID) size += sizeof(gid_t);
if (a & ATTR_CMN_ACCESSMASK) size += sizeof(u_long);
if (a & ATTR_CMN_NAMEDATTRCOUNT) size += sizeof(u_long);
if (a & ATTR_CMN_NAMEDATTRLIST) size += sizeof(struct attrreference);
if (a & ATTR_CMN_FLAGS) size += sizeof(u_long);
if (a & ATTR_CMN_USERACCESS) size += sizeof(u_long);
};
if ((a = attrlist->volattr) != 0) {
if (a & ATTR_VOL_FSTYPE) size += sizeof(u_long);
if (a & ATTR_VOL_SIGNATURE) size += sizeof(u_long);
if (a & ATTR_VOL_SIZE) size += sizeof(off_t);
if (a & ATTR_VOL_SPACEFREE) size += sizeof(off_t);
if (a & ATTR_VOL_SPACEAVAIL) size += sizeof(off_t);
if (a & ATTR_VOL_MINALLOCATION) size += sizeof(off_t);
if (a & ATTR_VOL_ALLOCATIONCLUMP) size += sizeof(off_t);
if (a & ATTR_VOL_IOBLOCKSIZE) size += sizeof(u_long);
if (a & ATTR_VOL_OBJCOUNT) size += sizeof(u_long);
if (a & ATTR_VOL_FILECOUNT) size += sizeof(u_long);
if (a & ATTR_VOL_DIRCOUNT) size += sizeof(u_long);
if (a & ATTR_VOL_MAXOBJCOUNT) size += sizeof(u_long);
if (a & ATTR_VOL_MOUNTPOINT) size += sizeof(struct attrreference);
if (a & ATTR_VOL_NAME) size += sizeof(struct attrreference);
if (a & ATTR_VOL_MOUNTFLAGS) size += sizeof(u_long);
if (a & ATTR_VOL_MOUNTEDDEVICE) size += sizeof(struct attrreference);
if (a & ATTR_VOL_ENCODINGSUSED) size += sizeof(unsigned long long);
if (a & ATTR_VOL_CAPABILITIES) size += sizeof(vol_capabilities_attr_t);
if (a & ATTR_VOL_ATTRIBUTES) size += sizeof(vol_attributes_attr_t);
};
if ((a = attrlist->dirattr) != 0) {
if (a & ATTR_DIR_LINKCOUNT) size += sizeof(u_long);
if (a & ATTR_DIR_ENTRYCOUNT) size += sizeof(u_long);
if (a & ATTR_DIR_MOUNTSTATUS) size += sizeof(u_long);
};
if ((a = attrlist->fileattr) != 0) {
if (a & ATTR_FILE_LINKCOUNT) size += sizeof(u_long);
if (a & ATTR_FILE_TOTALSIZE) size += sizeof(off_t);
if (a & ATTR_FILE_ALLOCSIZE) size += sizeof(off_t);
if (a & ATTR_FILE_IOBLOCKSIZE) size += sizeof(size_t);
if (a & ATTR_FILE_CLUMPSIZE) size += sizeof(off_t);
if (a & ATTR_FILE_DEVTYPE) size += sizeof(u_long);
if (a & ATTR_FILE_FILETYPE) size += sizeof(u_long);
if (a & ATTR_FILE_FORKCOUNT) size += sizeof(u_long);
if (a & ATTR_FILE_FORKLIST) size += sizeof(struct attrreference);
if (a & ATTR_FILE_DATALENGTH) size += sizeof(off_t);
if (a & ATTR_FILE_DATAALLOCSIZE) size += sizeof(off_t);
if (a & ATTR_FILE_DATAEXTENTS) size += sizeof(extentrecord);
if (a & ATTR_FILE_RSRCLENGTH) size += sizeof(off_t);
if (a & ATTR_FILE_RSRCALLOCSIZE) size += sizeof(off_t);
if (a & ATTR_FILE_RSRCEXTENTS) size += sizeof(extentrecord);
};
if ((a = attrlist->forkattr) != 0) {
if (a & ATTR_FORK_TOTALSIZE) size += sizeof(off_t);
if (a & ATTR_FORK_ALLOCSIZE) size += sizeof(off_t);
};
return size;
}
__private_extern__
unsigned long
DerivePermissionSummary(uid_t obj_uid, gid_t obj_gid, mode_t obj_mode,
struct mount *mp, struct ucred *cred, struct proc *p)
{
register gid_t *gp;
unsigned long permissions;
int i;
if (obj_uid == UNKNOWNUID)
obj_uid = console_user;
if (cred->cr_uid == 0) {
permissions = R_OK | W_OK | X_OK;
goto Exit;
};
if (hfs_owner_rights(VFSTOHFS(mp), obj_uid, cred, p, false) == 0) {
permissions = ((unsigned long)obj_mode & S_IRWXU) >> 6;
goto Exit;
}
if (! (mp->mnt_flag & MNT_UNKNOWNPERMISSIONS)) {
for (i = 0, gp = cred->cr_groups; i < cred->cr_ngroups; i++, gp++) {
if (obj_gid == *gp) {
permissions = ((unsigned long)obj_mode & S_IRWXG) >> 3;
goto Exit;
}
}
}
permissions = (unsigned long)obj_mode & S_IRWXO;
Exit:
return (permissions);
}