/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4 -*-
 * ex: set softtabstop=4 tabstop=8 expandtab shiftwidth=4: *
 * Editor Settings: expandtabs and use 4 spaces for indentation */

/*
 * Copyright Likewise Software
 * All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.  You should have received a copy of the GNU General
 * Public License along with this program.  If not, see
 * <http://www.gnu.org/licenses/>.
 *
 * LIKEWISE SOFTWARE MAKES THIS SOFTWARE AVAILABLE UNDER OTHER LICENSING
 * TERMS AS WELL.  IF YOU HAVE ENTERED INTO A SEPARATE LICENSE AGREEMENT
 * WITH LIKEWISE SOFTWARE, THEN YOU MAY ELECT TO USE THE SOFTWARE UNDER THE
 * TERMS OF THAT SOFTWARE LICENSE AGREEMENT INSTEAD OF THE TERMS OF THE GNU
 * GENERAL PUBLIC LICENSE, NOTWITHSTANDING THE ABOVE NOTICE.  IF YOU
 * HAVE QUESTIONS, OR WISH TO REQUEST A COPY OF THE ALTERNATE LICENSING
 * TERMS OFFERED BY LIKEWISE SOFTWARE, PLEASE CONTACT LIKEWISE SOFTWARE AT
 * license@likewisesoftware.com
 */

/*
 * Copyright (C) Likewise Software. All rights reserved.
 *
 * Module Name:
 *
 *        attrib_xattr.c
 *
 * Abstract:
 *
 *        Likewise Posix File System Driver (PVFS)
 *
 *        Extended Attribute Dos Attributes implementation
 *
 * Authors: Gerald Carter <gcarter@likewise.com>
 */

#include "pvfs.h"

#ifdef HAVE_EA_SUPPORT


#define PVFS_LINUX_EA_ATTRIB_NAME       "user.lwio_attrib_v1"
#define PVFS_FREEBSD_EA_ATTRIB_NAME     "lwio_attrib_v1"


/****************************************************************
 ***************************************************************/

NTSTATUS
PvfsGetFilenameAttributesXattr(
    IN PCSTR pszPath,
    OUT PFILE_ATTRIBUTES pAttributes
    )
{
    NTSTATUS status = STATUS_SUCCESS;
    int eaSize = 0;
    int unixerr = 0;

#if defined(__LWI_LINUX__)
    eaSize = getxattr(
                 pszPath,
                 PVFS_LINUX_EA_ATTRIB_NAME,
                 (PVOID)pAttributes,
                 sizeof(*pAttributes));
#elif defined(__LWI_FREEBSD__)
    eaSize = extattr_get_file(
                 pszPath,
                 EXTATTR_NAMESPACE_USER,
                 PVFS_FREEBSD_EA_ATTRIB_NAME,
                 (PVOID)pAttributes,
                 sizeof(*pAttributes));
#endif

    if (eaSize == -1)
    {
        PVFS_BAIL_ON_UNIX_ERROR(unixerr, status);
    }

    status = STATUS_SUCCESS;

error:

    if (!NT_SUCCESS(status))
    {
        // Perform some error mapping so make life easier on the caller
        switch(status)
        {
            case STATUS_NO_EAS_ON_FILE:
                status = STATUS_RESOURCE_DATA_NOT_FOUND;
                break;

            default:
                break;
        }
    }

    return status;
}

/****************************************************************
 ***************************************************************/

NTSTATUS
PvfsGetFileAttributesXattr(
    IN PPVFS_CCB pCcb,
    OUT PFILE_ATTRIBUTES pAttributes
    )
{
    NTSTATUS status = STATUS_SUCCESS;
    int bytesRead = 0;
    int unixErrno = 0;

    if (PvfsIsDefaultStream(pCcb->pScb))
    {
        // get the base file object attributes using our existing fd
#if defined(__LWI_LINUX__)
        bytesRead = fgetxattr(
                        pCcb->fd,
                        PVFS_LINUX_EA_ATTRIB_NAME,
                        (PVOID)pAttributes,
                        sizeof(*pAttributes));
#elif defined(__LWI_FREEBSD__)
        bytesRead = extattr_get_fd(
                        pCcb->fd,
                        EXTATTR_NAMESPACE_USER,
                        PVFS_FREEBSD_EA_ATTRIB_NAME,
                        (PVOID)pAttributes,
                        sizeof(*pAttributes));
#endif
    }
    else
    {
        // Retrieve the attributes from the base file object
#if defined(__LWI_LINUX__)
        bytesRead = getxattr(
                        pCcb->pScb->pOwnerFcb->pszFilename,
                        PVFS_LINUX_EA_ATTRIB_NAME,
                        (PVOID)pAttributes,
                        sizeof(*pAttributes));
#elif defined(__LWI_FREEBSD__)
        bytesRead = extattr_get_file(
                        pCcb->pScb->pOwnerFcb->pszFilename,
                        EXTATTR_NAMESPACE_USER,
                        PVFS_FREEBSD_EA_ATTRIB_NAME,
                        (PVOID)pAttributes,
                        sizeof(*pAttributes));
#endif
    }

    if (bytesRead == -1)
    {
        PVFS_BAIL_ON_UNIX_ERROR(unixErrno, status);
    }


error:

    if (!NT_SUCCESS(status))
    {
        // Perform some error mapping so make life easier on the caller
        switch(status)
        {
            case STATUS_NO_EAS_ON_FILE:
                status = STATUS_RESOURCE_DATA_NOT_FOUND;
                break;

            default:
                break;
        }
    }

    return status;
}

/****************************************************************
 ***************************************************************/

NTSTATUS
PvfsSetFileAttributesXattr(
    IN PPVFS_CCB pCcb,
    IN FILE_ATTRIBUTES Attributes
    )
{
    NTSTATUS ntError = STATUS_SUCCESS;
    int unixErrno = 0;
    int bytesWritten = 0;

    if (PvfsIsDefaultStream(pCcb->pScb))
    {
        // Default stream is written to the actual fd
#if defined(__LWI_LINUX__)
        bytesWritten = fsetxattr(
                           pCcb->fd,
                           PVFS_LINUX_EA_ATTRIB_NAME,
                           (PVOID)&Attributes,
                           sizeof(Attributes),
                           0);
#elif defined (__LWI_FREEBSD__)
        bytesWritten = extattr_set_fd(
                           pCcb->fd,
                           EXTATTR_NAMESPACE_USER,
                           PVFS_FREEBSD_EA_ATTRIB_NAME,
                           (PVOID)&Attributes,
                           sizeof(Attributes));
#endif
    }
    else
    {
        // Set the attributes on the base file object
#if defined(__LWI_LINUX__)
        bytesWritten = setxattr(
                           pCcb->pScb->pOwnerFcb->pszFilename,
                           PVFS_LINUX_EA_ATTRIB_NAME,
                           (PVOID)&Attributes,
                           sizeof(Attributes),
                           0);
#elif defined (__LWI_FREEBSD__)
        bytesWritten = extattr_set_file(
                           pCcb->pScb->pOwnerFcb->pszFilename,
                           EXTATTR_NAMESPACE_USER,
                           PVFS_FREEBSD_EA_ATTRIB_NAME,
                           (PVOID)&Attributes,
                           sizeof(Attributes));
#endif
    }

    if (bytesWritten == -1)
    {
        PVFS_BAIL_ON_UNIX_ERROR(unixErrno, ntError);
    }

error:

    return ntError;
}

////////////////////////////////////////////////////////////////////////

NTSTATUS
PvfsSetFilenameAttributesXattr(
    IN PCSTR Filename,
    IN FILE_ATTRIBUTES Attributes
    )
{
    NTSTATUS ntError = STATUS_SUCCESS;
    int unixErrno = 0;
    int bytesWritten = 0;

    // Assuming that Filename represents the base file object

#if defined(__LWI_LINUX__)
    bytesWritten = setxattr(
                       Filename,
                       PVFS_LINUX_EA_ATTRIB_NAME,
                       (PVOID)&Attributes,
                       sizeof(Attributes),
                       0);
#elif defined(__LWI_FREEBSD__)
    bytesWritten = extattr_set_file(
                       Filename,
                       EXTATTR_NAMESPACE_USER,
                       PVFS_FREEBSD_EA_ATTRIB_NAME,
                       (PVOID)&Attributes,
                       sizeof(Attributes));
#endif

    if (bytesWritten == -1)
    {
        PVFS_BAIL_ON_UNIX_ERROR(unixErrno, ntError);
    }

error:

    return ntError;
}


#endif   /* HAVE_EA_SUPPORT  */


