Changeset 3816


Ignore:
Timestamp:
Feb 19, 2014, 12:06:45 AM (11 years ago)
Author:
bird
Message:

trunk: Implemented chown, fchown and lchown and modified the EA writing of chmod, fchmod, and lchmod so it hopefully succeed more often.

Location:
trunk/libc
Files:
11 edited
4 copied

Legend:

Unmodified
Added
Removed
  • trunk/libc/include/klibc/backend.h

    r3811 r3816  
    11/* $Id$ */
    22/** @file
    3  *
    43 * kLIBC - Backend header.
    5  *
    6  * Copyright (c) 2004-2006 knut st. osmundsen <bird-srcspam@anduin.net>
     4 */
     5
     6/*
     7 * Copyright (c) 2004-2014 knut st. osmundsen <bird-srcspam@anduin.net>
    78 *
    89 *
     
    163164
    164165
    165 /** @defgroup __libc_Back_fs   kLIBC Backend - File Handle
     166/** @defgroup __libc_Back_fh   kLIBC Backend - File Handle
    166167 * @{ */
    167168
     
    412413
    413414/**
     415 * Changes the ownership and/or group, without following any final symlink.
     416 *
     417 * @returns 0 on success.
     418 * @returns Negative error code (errno.h) on failure.
     419 * @param   pszPath     Path to the file to modify ownership of.  If this is a
     420 *                      symbolic link, the link it self will modified.
     421 * @param   uid         The user id of the new owner, pass -1 to not change it.
     422 * @param   gid         The group id of the new group, pass -1 to not change it.
     423 */
     424int __libc_Back_fsSymlinkOwnerSet(const char *pszPath, uid_t uid, gid_t gid);
     425
     426/**
    414427 * Stats a file.
    415428 *
     
    472485 */
    473486int __libc_Back_fsFileTimesSetFH(int fh, const struct timeval *paTimes);
     487
     488/**
     489 * Changes the ownership and/or group, following all symbolic links.
     490 *
     491 * @returns 0 on success.
     492 * @returns Negative error code (errno.h) on failure.
     493 * @param   pszPath     Path to the file to modify ownership of.
     494 * @param   uid         The user id of the new owner, pass -1 to not change it.
     495 * @param   gid         The group id of the new group, pass -1 to not change it.
     496 */
     497int __libc_Back_fsFileOwnerSet(const char *pszPath, uid_t uid, gid_t gid);
     498
     499/**
     500 * Changes the ownership and/or group.
     501 *
     502 * @returns 0 on success.
     503 * @returns Negative error code (errno.h) on failure.
     504 * @param   fh          Handle to file.
     505 * @param   uid         The user id of the new owner, pass -1 to not change it.
     506 * @param   gid         The group id of the new group, pass -1 to not change it.
     507 */
     508int __libc_Back_fsFileOwnerSetFH(int fh, uid_t uid, gid_t gid);
    474509
    475510/**
  • trunk/libc/include/klibc/io.h

    r3689 r3816  
    11/* $Id: $ */
    22/** @file
    3  *
    43 * kLIBC I/O
    5  *
    6  * Copyright (c) 2006 knut st. osmundsen <bird@innotek.de>
     4 */
     5
     6/*
     7 * Copyright (c) 2006-2014 knut st. osmundsen <bird-src-spam@anduin.net>
    78 *
    89 *
     
    242243     * @returns Negated error code (errno.h) on failure.
    243244     * @param   pFH         Pointer to the handle structure to operate on.
    244      * @param   uid         The id of the new owner.
    245      */
    246     int (*pfnSetOwner)(struct __libc_FileHandle *pFH, uid_t uid);
     245     * @param   uid         The id of the new owner, -1 if not to be changed.
     246     * @param   gid         The id of the new owner group, if no change,
     247     */
     248    int (*pfnSetOwner)(struct __libc_FileHandle *pFH, uid_t uid, gid_t gid);
    247249
    248250    /**
  • trunk/libc/src/kNIX/Makefile.kmk

    r3687 r3816  
    11# $Id$
    22## @file
    3 #
    43# kBuild Sub-Makefile for kLIBC - src/libc/sys.
    54#
    6 # Copyright (c) 2006 knut st. osmundsen <bird-src-spam@anduin.net>
     5
     6#
     7# Copyright (c) 2006-2014 knut st. osmundsen <bird-src-spam@anduin.net>
    78#
    89#
     
    4142    $(PATH_LIBC_SRC)/kNIX/b_fsPathResolve.c \
    4243    $(PATH_LIBC_SRC)/kNIX/b_fsSymlinkModeSet.c \
     44    $(PATH_LIBC_SRC)/kNIX/b_fsSymlinkOwnerSet.c \
    4345    $(PATH_LIBC_SRC)/kNIX/b_fsSymlinkCreate.c \
    4446    $(PATH_LIBC_SRC)/kNIX/b_fsFileTimesSet.c \
     
    4749    $(PATH_LIBC_SRC)/kNIX/b_fsFileModeSet.c \
    4850    $(PATH_LIBC_SRC)/kNIX/b_fsFileModeSetFH.c \
     51    $(PATH_LIBC_SRC)/kNIX/b_fsFileOwnerSet.c \
     52    $(PATH_LIBC_SRC)/kNIX/b_fsFileOwnerSetFH.c \
    4953    $(PATH_LIBC_SRC)/kNIX/b_fsFileStat.c \
    5054    $(PATH_LIBC_SRC)/kNIX/b_fsFileStatFH.c \
     
    98102    $(PATH_LIBC_SRC)/kNIX/os2/b_fsDriveDefaultSet.c \
    99103    $(PATH_LIBC_SRC)/kNIX/os2/b_fsNativeFileModeSet.c \
     104    $(PATH_LIBC_SRC)/kNIX/os2/b_fsNativeFileOwnerSet.c \
    100105    $(PATH_LIBC_SRC)/kNIX/os2/b_fsNativeFileStat.c \
    101106    $(PATH_LIBC_SRC)/kNIX/os2/b_fsNativeFileTimesSet.c \
  • trunk/libc/src/kNIX/b_fsFileOwnerSet.c

    r3807 r3816  
    11/* $Id$ */
    22/** @file
    3  *
    4  * kNIX - lstat.
    5  *
    6  * Copyright (c) 2004 knut st. osmundsen <bird-src-spam@anduin.net>
     3 * kNIX - chown.
     4 */
     5
     6/*
     7 * Copyright (c) 2004-2014 knut st. osmundsen <bird-src-spam@anduin.net>
    78 *
    89 *
     
    3031
    3132/**
    32  * Stats a symbolic link.
     33 * Changes the ownership and/or group, without following any final symlink.
    3334 *
    3435 * @returns 0 on success.
    3536 * @returns Negative error code (errno.h) on failure.
    36  * @param   pszPath     Path to the file to stat. If this is a symbolic link
    37  *                      the link it self will be stat'ed.
    38  * @param   pStat       Where to store the file stats.
     37 * @param   pszPath     Path to the file to modify ownership of.  If this is a
     38 *                      symbolic link, the link it self will modified.
     39 * @param   uid         The user id of the new owner, pass -1 to not change it.
     40 * @param   gid         The group id of the new group, pass -1 to not change it.
    3941 */
    40 int __libc_Back_fsSymlinkStat(const char *pszPath, struct stat *pStat)
     42int __libc_Back_fsFileOwnerSet(const char *pszPath, uid_t uid, gid_t gid)
    4143{
    42     LIBCLOG_ENTER("pszPath=%p:{%s} pStat=%p\n", (void *)pszPath, pszPath, (void *)pStat);
     44    LIBCLOG_ENTER("pszPath=%p:{%s} %ld %ld\n", (void *)pszPath, pszPath, (long)uid, (long)gid);
    4345
    4446    /*
     
    4648     */
    4749    char szNativePath[PATH_MAX];
    48     int rc = __libc_back_fsResolve(pszPath, BACKFS_FLAGS_RESOLVE_FULL_SYMLINK | BACKFS_FLAGS_RESOLVE_DIR_MAYBE, szNativePath, NULL);
     50    int rc = __libc_back_fsResolve(pszPath, BACKFS_FLAGS_RESOLVE_FULL | BACKFS_FLAGS_RESOLVE_DIR_MAYBE,
     51                                   szNativePath, NULL);
    4952    if (!rc)
    50         rc = __libc_back_fsNativeFileStat(szNativePath, pStat);
     53        rc = __libc_back_fsNativeFileOwnerSet(szNativePath, uid, gid);
    5154
    5255    if (!rc)
  • trunk/libc/src/kNIX/b_fsFileOwnerSetFH.c

    r3807 r3816  
    11/* $Id$ */
    22/** @file
    3  *
    4  * kNIX - fchmod().
    5  *
    6  * Copyright (c) 2005-2006 knut st. osmundsen <bird-src-spam@anduin.net>
     3 * kNIX - fchown().
     4 */
     5
     6/*
     7 * Copyright (c) 2005-2014 knut st. osmundsen <bird-src-spam@anduin.net>
    78 *
    89 *
     
    3132
    3233/**
    33  * Sets the file access mode of a file by filehandle.
     34 * Sets the file ownership of a file by filehandle.
    3435 *
    3536 * @returns 0 on success.
    3637 * @returns Negative error code (errno.h) on failure.
    37  * @param   fh      Handle to file.
    38  * @param   fMode   The filemode.
     38 * @param   fh          Handle to file.
     39 * @param   uid         The user id of the new owner, pass -1 to not change it.
     40 * @param   gid         The group id of the new group, pass -1 to not change it.
    3941 */
    40 int __libc_Back_fsFileModeSetFH(int fh, mode_t fMode)
     42int __libc_Back_fsFileOwnerSetFH(int fh, uid_t uid, gid_t gid)
    4143{
    42     LIBCLOG_ENTER("fh=%d fMode=%#o (%#x)\n", fh, fMode, fMode);
     44    LIBCLOG_ENTER("fh=%d uid=%ld gid=%ld\n", fh, (long)uid, (long)gid);
    4345
    4446    /*
     
    5254         * If supported, do handle operation, otherwise predend it's a pipe (see SuS).
    5355         */
    54         if (pFH->pOps->pfnSetMode)
    55             rc = pFH->pOps->pfnSetMode(pFH, fMode);
     56        if (pFH->pOps->pfnSetOwner)
     57            rc = pFH->pOps->pfnSetOwner(pFH, uid, gid);
    5658        else
    5759            rc = EINVAL;
     
    6163}
    6264
    63 
  • trunk/libc/src/kNIX/b_fsSymlinkOwnerSet.c

    r3807 r3816  
    11/* $Id$ */
    22/** @file
    3  *
    4  * kNIX - lstat.
    5  *
    6  * Copyright (c) 2004 knut st. osmundsen <bird-src-spam@anduin.net>
     3 * kNIX - lchown.
     4 */
     5
     6/*
     7 * Copyright (c) 2004-2014 knut st. osmundsen <bird-src-spam@anduin.net>
    78 *
    89 *
     
    3031
    3132/**
    32  * Stats a symbolic link.
     33 * Changes the ownership and/or group, without following any final symlink.
    3334 *
    3435 * @returns 0 on success.
    3536 * @returns Negative error code (errno.h) on failure.
    36  * @param   pszPath     Path to the file to stat. If this is a symbolic link
    37  *                      the link it self will be stat'ed.
    38  * @param   pStat       Where to store the file stats.
     37 * @param   pszPath     Path to the file to modify ownership of.  If this is a
     38 *                      symbolic link, the link it self will modified.
     39 * @param   uid         The user id of the new owner, pass -1 to not change it.
     40 * @param   gid         The group id of the new group, pass -1 to not change it.
    3941 */
    40 int __libc_Back_fsSymlinkStat(const char *pszPath, struct stat *pStat)
     42int __libc_Back_fsSymlinkOwnerSet(const char *pszPath, uid_t uid, gid_t gid)
    4143{
    42     LIBCLOG_ENTER("pszPath=%p:{%s} pStat=%p\n", (void *)pszPath, pszPath, (void *)pStat);
     44    LIBCLOG_ENTER("pszPath=%p:{%s} %ld %ld\n", (void *)pszPath, pszPath, (long)uid, (long)gid);
    4345
    4446    /*
     
    4648     */
    4749    char szNativePath[PATH_MAX];
    48     int rc = __libc_back_fsResolve(pszPath, BACKFS_FLAGS_RESOLVE_FULL_SYMLINK | BACKFS_FLAGS_RESOLVE_DIR_MAYBE, szNativePath, NULL);
     50    int rc = __libc_back_fsResolve(pszPath, BACKFS_FLAGS_RESOLVE_FULL_SYMLINK | BACKFS_FLAGS_RESOLVE_DIR_MAYBE,
     51                                   szNativePath, NULL);
    4952    if (!rc)
    50         rc = __libc_back_fsNativeFileStat(szNativePath, pStat);
     53        rc = __libc_back_fsNativeFileOwnerSet(szNativePath, uid, gid);
    5154
    5255    if (!rc)
  • trunk/libc/src/kNIX/kNIX.h

    r3770 r3816  
    11/* $Id$ */
    22/** @file
    3  *
    43 * kNIX - Main header.
    5  *
    6  * Copyright (c) 2006 knut st. osmundsen <bird-src-spam@anduin.net>
     4 */
     5
     6/*
     7 * Copyright (c) 2006-2014 knut st. osmundsen <bird-src-spam@anduin.net>
    78 *
    89 *
     
    232233int __libc_back_fsNativeFileModeSet(const char *pszNativePath, mode_t Mode);
    233234int __libc_back_fsNativeFileTimesSet(const char *pszNativePath, const struct timeval *paTimes);
     235int __libc_back_fsNativeFileOwnerSet(const char *pszNativePath, uid_t uid, gid_t gid);
     236int __libc_back_fsNativeFileOwnerSetCommon(intptr_t hNative, const char *pszNativePath, int fUnixEAs, uid_t uid, gid_t gid);
     237int __libc_back_fsNativeSetEAs(intptr_t hNative, const char *pszNativePath, PFEA2LIST pEAs, PEAOP2 pEaOp2);
    234238dev_t __libc_back_fsPathCalcInodeAndDev(const char *pszNativePath, ino_t *pInode);
    235239__LIBC_PFSINFO __libc_back_fsInfoObjByDev(dev_t Dev);
  • trunk/libc/src/kNIX/os2/b_fsNativeFileModeSet.c

    r3794 r3816  
    11/* $Id$ */
    22/** @file
    3  *
    43 * kNIX - internal [l]chmod, OS/2.
    5  *
    6  * Copyright (c) 2005-2006 knut st. osmundsen <bird-src-spam@anduin.net>
     4 */
     5
     6/*
     7 * Copyright (c) 2005-2014 knut st. osmundsen <bird-src-spam@anduin.net>
    78 *
    89 *
     
    180181            };
    181182            #pragma pack()
    182             EAOP2 EaOp2;
    183             EaOp2.fpGEA2List = NULL;
    184             EaOp2.fpFEA2List = (PFEA2LIST)&EAs;
    185             EaOp2.oError = 0;
    186183
    187184            if (!S_ISREG(Mode) && !S_ISDIR(Mode))
     
    189186
    190187            /* finally, try update / add the EA. */
    191             rc = DosSetPathInfo((PCSZ)pszNativePath, FIL_QUERYEASIZE, &EaOp2, sizeof(EaOp2), 0);
     188            EAOP2 EaOp2;
     189            rc = __libc_back_fsNativeSetEAs(-1, pszNativePath, (PFEA2LIST)&EAs, &EaOp2);
    192190            if (__predict_false(rc != NO_ERROR))
    193191            {
    194                 LIBCLOG_ERROR("DosSetPathInfo('%s',,,,) -> %d, oError=%#lx\n", pszNativePath, rc, EaOp2.oError);
     192                LIBCLOG_ERROR("__libc_back_fsNativeSetEAs('%s',,,,) -> %d, oError=%#lx\n", pszNativePath, rc, EaOp2.oError);
    195193                if (rc == ERROR_EAS_NOT_SUPPORTED)
    196                     rc = 0;
     194                    rc = NO_ERROR;
    197195                else
    198196                    rc = -__libc_back_native2errno(rc);
  • trunk/libc/src/kNIX/os2/b_fsNativeFileOwnerSet.c

    r3807 r3816  
    11/* $Id$ */
    22/** @file
    3  *
    4  * kNIX - internal [l]chmod, OS/2.
    5  *
    6  * Copyright (c) 2005-2006 knut st. osmundsen <bird-src-spam@anduin.net>
     3 * kNIX - internal [l]chown, OS/2.
     4 */
     5
     6/*
     7 * Copyright (c) 2005-2014 knut st. osmundsen <bird-src-spam@anduin.net>
    78 *
    89 *
     
    2930#include <klibc/logstrict.h>
    3031
     32
    3133/**
    32  * Sets the file access mode of a native file.
     34 * Sets the file ownership of a native file.
    3335 *
    3436 * @returns 0 on success.
    3537 * @returns Negative error code (errno.h) on failure.
    36  * @param   fh      Handle to file.
    37  * @param   Mode    The filemode.
     38 * @param   hNative         Native handle, -1 if only the path is available.
     39 * @param   pszNativePath   The native path.
     40 * @param   uid             The user id of the new owner, pass -1 to not change it.
     41 * @param   gid             The group id of the new group, pass -1 to not change it.
    3842 */
    39 int __libc_back_fsNativeFileModeSet(const char *pszNativePath, mode_t Mode)
     43int __libc_back_fsNativeFileOwnerSetCommon(intptr_t hNative, const char *pszNativePath, int fUnixEAs, uid_t uid, gid_t gid)
    4044{
    41     LIBCLOG_ENTER("pszNativePath=%p:{%s} Mode=%#x\n", (void *)pszNativePath, pszNativePath, Mode);
    42     union
     45    FS_VAR_SAVE_LOAD();
     46    int rc;
     47    if (fUnixEAs)
    4348    {
    44         FILESTATUS4     fsts4;
    45         FILESTATUS4L    fsts4L;
    46     } info;
     49        struct stat StOrg;
     50        memset(&StOrg, 0, sizeof(StOrg));
     51        rc = __libc_back_fsUnixAttribsGet(hNative, pszNativePath, &StOrg);
     52        if (__predict_true(!rc || rc == -ENOTSUP))
     53        {
     54            /** @todo check for permissions to change file ownership. */
     55            if (uid != (uid_t)-1 || gid != (gid_t)-1)
     56            {
     57                /* construct FEA2 stuff. */
     58                #pragma pack(1)
     59                struct __LIBC_FSUNIXATTRIBSSETOWNER
     60                {
     61                    ULONG   cbList;
     62
     63                    ULONG   offUid;
     64                    BYTE    fUidEA;
     65                    BYTE    cbUidName;
     66                    USHORT  cbUidValue;
     67                    CHAR    szUidName[sizeof(EA_UID)];
     68                    USHORT  usUidType;
     69                    USHORT  cbUidData;
     70                    uint32_t u32Uid;
     71                    CHAR    achUidAlign[((sizeof(EA_UID) + 4) & ~3) - sizeof(EA_UID)];
     72
     73                    ULONG   offGid;
     74                    BYTE    fGidEA;
     75                    BYTE    cbGidName;
     76                    USHORT  usGidValue;
     77                    CHAR    szGidName[sizeof(EA_GID)];
     78                    USHORT  usGidType;
     79                    USHORT  cbGidData;
     80                    uint32_t u32Gid;
     81                    CHAR    achGidAlign[((sizeof(EA_GID) + 4) & ~3) - sizeof(EA_GID)];
     82                } EAs =
     83                {
     84                    sizeof(EAs),
     85
     86                    /* .offUid      =*/ offsetof(struct __LIBC_FSUNIXATTRIBSSETOWNER, offGid)
     87                                      - offsetof(struct __LIBC_FSUNIXATTRIBSSETOWNER, offUid),
     88                    /* .fUidEA      =*/ 0,
     89                    /* .cbUidName   =*/ sizeof(EA_UID) - 1,
     90                    /* .cbUidValue  =*/ sizeof(uint32_t) + 4,
     91                    /* .szUidName   =*/ EA_UID,
     92                    /* .usUidType   =*/ EAT_BINARY,
     93                    /* .cbUidData   =*/ sizeof(uint32_t),
     94                    /* .u32Uid      =*/ uid != (uid_t)-1 ? uid : StOrg.st_uid,
     95                    /* .achUidAlign =*/ "",
     96
     97                    /* .offGid      =*/ 0,
     98                    /* .fGidEA      =*/ 0,
     99                    /* .cbGidName   =*/ sizeof(EA_GID) - 1,
     100                    /* .cbGidValue  =*/ sizeof(uint32_t) + 4,
     101                    /* .szGidName   =*/ EA_GID,
     102                    /* .usGidType   =*/ EAT_BINARY,
     103                    /* .cbGidData   =*/ sizeof(uint32_t),
     104                    /* .u32Gid      =*/ gid != (gid_t)-1 ? gid : StOrg.st_gid,
     105                    /* .achGidAlign =*/ "",
     106                };
     107                #pragma pack()
     108
     109                if (uid != -1 && uid != 0)
     110                    EAs.fUidEA = FEA_NEEDEA;
     111                if (gid != -1 && gid != 0)
     112                    EAs.fGidEA = FEA_NEEDEA;
     113
     114                EAOP2 EaOp2;
     115                rc = __libc_back_fsNativeSetEAs(hNative, pszNativePath, (PFEA2LIST)&EAs, &EaOp2);
     116                if (__predict_false(rc != NO_ERROR))
     117                {
     118                    LIBCLOG_ERROR2("__libc_back_fsNativeSetEAs('%s',,,,) -> %d, oError=%#lx\n", pszNativePath, rc, EaOp2.oError);
     119                    if (   rc == ERROR_EAS_NOT_SUPPORTED
     120                        && (uid == -1 || uid == 0)
     121                        && (gid == -1 || gid == 0) )
     122                        rc = 0;
     123                    else
     124                        rc = -__libc_back_native2errno(rc);
     125                }
     126            }
     127            else
     128                rc = 0; /* No change and thus no not-supported complaints. */
     129        }
     130    }
     131    else
     132    {
     133        /*
     134         * In this mode we'll simply check if the file exists and return the
     135         * appropriate errors if it doesn't or we cannot access it.
     136         */
     137        union
     138        {
     139            FILESTATUS4     fsts4;
     140            FILESTATUS4L    fsts4L;
     141        } info;
     142
    47143#if OFF_MAX > LONG_MAX
    48     int     fLarge = 0;
     144        if (__libc_gpfnDosOpenL)
     145            rc = DosQueryPathInfo((PCSZ)pszNativePath, FIL_STANDARDL, &info, sizeof(info.fsts4L) - sizeof(info.fsts4L.cbList));
     146        else
    49147#endif
    50     FS_VAR();
     148            rc = DosQueryPathInfo((PCSZ)pszNativePath, FIL_STANDARD, &info, sizeof(info.fsts4) - sizeof(info.fsts4.cbList));
     149
     150        if (rc != NO_ERROR)
     151            rc = -__libc_back_native2errno(rc);
     152    }
     153    FS_RESTORE();
     154    return rc;
     155}
     156
     157
     158/**
     159 * Sets the file ownership of a native file.
     160 *
     161 * @returns 0 on success.
     162 * @returns Negative error code (errno.h) on failure.
     163 * @param   pszNativePath   The native path.
     164 * @param   uid             The user id of the new owner, pass -1 to not change it.
     165 * @param   gid             The group id of the new group, pass -1 to not change it.
     166 */
     167int __libc_back_fsNativeFileOwnerSet(const char *pszNativePath, uid_t uid, gid_t gid)
     168{
     169    LIBCLOG_ENTER("pszNativePath=%p:{%s} uid=%ld gid=%ld\n", (void *)pszNativePath, pszNativePath, (long)uid, (long)gid);
    51170
    52171    /*
     
    68187
    69188    /*
    70      * Get path info.
     189     * Join paths with the file handle base code path.
    71190     */
    72     FS_SAVE_LOAD();
    73     int rc;
    74     const int fUnixEAs = __libc_back_fsInfoSupportUnixEAs(pszNativePath);
    75     if (fUnixEAs)
    76     {
    77 #if OFF_MAX > LONG_MAX
    78         if (__libc_gpfnDosOpenL)
    79         {
    80             rc = DosQueryPathInfo((PCSZ)pszNativePath, FIL_QUERYEASIZEL, &info, sizeof(info.fsts4L));
    81             fLarge = 1;
    82         }
    83         else
    84 #endif
    85             rc = DosQueryPathInfo((PCSZ)pszNativePath, FIL_QUERYEASIZE, &info, sizeof(info.fsts4));
    86         /* If the file is open in write mode, we cannot even get the EA size. stupid.
    87          * It'll fail with ERROR_SHARING_VIOLATION, which we handle rigth below. */
    88     }
    89     else
    90         rc = ERROR_SHARING_VIOLATION; /* take the fallback path if we don't want EAs. */
    91     if (rc == ERROR_SHARING_VIOLATION)
    92     {
    93 #if OFF_MAX > LONG_MAX
    94         if (__libc_gpfnDosOpenL)
    95         {
    96             rc = DosQueryPathInfo((PCSZ)pszNativePath, FIL_STANDARDL, &info, sizeof(info.fsts4L) - sizeof(info.fsts4L.cbList));
    97             fLarge = 1;
    98         }
    99         else
    100 #endif
    101             rc = DosQueryPathInfo((PCSZ)pszNativePath, FIL_STANDARD, &info, sizeof(info.fsts4) - sizeof(info.fsts4.cbList));
    102         info.fsts4L.cbList = LIBC_UNIX_EA_MIN;
    103     }
    104     if (rc)
    105     {
    106         rc = -__libc_back_native2errno(rc);
    107         FS_RESTORE();
     191    int rc = __libc_back_fsNativeFileOwnerSetCommon(-1, pszNativePath, __libc_back_fsInfoSupportUnixEAs(pszNativePath), uid, gid);
     192    if (__predict_false(rc != 0))
    108193        LIBCLOG_ERROR_RETURN_INT(rc);
    109     }
    110 
    111     /*
    112      * Update OS/2 attributes.
    113      */
    114 #if OFF_MAX > LONG_MAX
    115     if (fLarge)
    116     {
    117         if (Mode & S_IWRITE)
    118             info.fsts4L.attrFile &= ~FILE_READONLY;
    119         else
    120             info.fsts4L.attrFile = FILE_READONLY;
    121         rc = DosSetPathInfo((PCSZ)pszNativePath, FIL_STANDARDL, &info, sizeof(info.fsts4L) - sizeof(info.fsts4L.cbList), 0);
    122     }
    123     else
    124 #endif
    125     {
    126         if (Mode & S_IWRITE)
    127             info.fsts4.attrFile &= ~FILE_READONLY;
    128         else
    129             info.fsts4.attrFile |= FILE_READONLY;
    130         rc = DosSetPathInfo((PCSZ)pszNativePath, FIL_STANDARD, &info, sizeof(info.fsts4) - sizeof(info.fsts4.cbList), 0);
    131     }
    132     if (__predict_false(rc != NO_ERROR))
    133     {
    134         FS_RESTORE();
    135         rc = -__libc_back_native2errno(rc);
    136         LIBCLOG_ERROR_RETURN_INT(rc);
    137     }
    138 
    139     /*
    140      * If in unix mode we'll have to update/add the MODE too.
    141      */
    142     if (fUnixEAs)
    143     {
    144         mode_t CurMode;
    145         rc = __libc_back_fsUnixAttribsGetMode(-1, pszNativePath, &CurMode);
    146         if (__predict_true(!rc || rc == -ENOTSUP))
    147         {
    148             /* correct the passed in Mode mask. */
    149             Mode &= ALLPERMS; /** @todo sticky bit and set uid/gid access validation... */
    150             if (!(CurMode & ~ALLPERMS))
    151             {
    152 #if OFF_MAX > LONG_MAX
    153                 if ((fLarge ? info.fsts4L.attrFile : info.fsts4.attrFile) & FILE_DIRECTORY)
    154 #else
    155                 if (info.fsts4.attrFile & FILE_DIRECTORY)
    156 #endif
    157                     Mode |= S_IFDIR;
    158                 else
    159                     Mode |= S_IFREG;
    160             }
    161             else
    162                 Mode |= CurMode & ~ALLPERMS;
    163 
    164             /* construct FEA2 stuff. */
    165             #pragma pack(1)
    166             struct __LIBC_FSUNIXATTRIBSSETMODE
    167             {
    168                 ULONG   cbList;
    169                 ULONG   off;
    170                 BYTE    fEA;
    171                 BYTE    cbName;
    172                 USHORT  cbValue;
    173                 CHAR    szName[sizeof(EA_MODE)];
    174                 USHORT  usType;
    175                 USHORT  cbData;
    176                 uint32_t u32Mode;
    177             } EAs =
    178             {
    179                 sizeof(EAs), 0, 0, sizeof(EA_MODE) - 1, sizeof(uint32_t) + 4, EA_MODE, EAT_BINARY, sizeof(uint32_t), Mode
    180             };
    181             #pragma pack()
    182             EAOP2 EaOp2;
    183             EaOp2.fpGEA2List = NULL;
    184             EaOp2.fpFEA2List = (PFEA2LIST)&EAs;
    185             EaOp2.oError = 0;
    186 
    187             if (!S_ISREG(Mode) && !S_ISDIR(Mode))
    188                 EAs.fEA = FEA_NEEDEA;
    189 
    190             /* finally, try update / add the EA. */
    191             rc = DosSetPathInfo((PCSZ)pszNativePath, FIL_QUERYEASIZE, &EaOp2, sizeof(EaOp2), 0);
    192             if (__predict_false(rc != NO_ERROR))
    193             {
    194                 LIBCLOG_ERROR("DosSetPathInfo('%s',,,,) -> %d, oError=%#lx\n", pszNativePath, rc, EaOp2.oError);
    195                 if (rc == ERROR_EAS_NOT_SUPPORTED)
    196                     rc = 0;
    197                 else
    198                     rc = -__libc_back_native2errno(rc);
    199             }
    200         }
    201         if (__predict_false(rc != 0))
    202         {
    203             FS_RESTORE();
    204             LIBCLOG_ERROR_RETURN_INT(rc);
    205         }
    206     }
    207     FS_RESTORE();
    208 
    209194    LIBCLOG_RETURN_INT(0);
    210195}
  • trunk/libc/src/kNIX/os2/fhOS2File.c

    r3794 r3816  
    11/* $Id$ */
    22/** @file
    3  *
    43 * kNIX - Native file handles.
    5  *
    6  * Copyright (c) 2006 knut st. osmundsen <bird-src-spam@anduin.net>
     4 */
     5
     6/*
     7 * Copyright (c) 2006-2014 knut st. osmundsen <bird-src-spam@anduin.net>
    78 *
    89 *
     
    635636                };
    636637                #pragma pack()
    637                 EAOP2 EaOp2;
    638                 EaOp2.fpGEA2List = NULL;
    639                 EaOp2.fpFEA2List = (PFEA2LIST)&EAs;
    640                 EaOp2.oError = 0;
    641638
    642639                if (!S_ISREG(fMode) && !S_ISDIR(fMode))
     
    644641
    645642                /* finally, try update / add the EA. */
    646                 rc = DosSetFileInfo(pFH->fh, FIL_QUERYEASIZE, &EaOp2, sizeof(EaOp2));
     643                EAOP2 EaOp2;
     644                rc = __libc_back_fsNativeSetEAs(pFH->fh, pFH->pszNativePath, (PFEA2LIST)&EAs, &EaOp2);
    647645                if (__predict_false(rc != NO_ERROR))
    648646                {
    649                     LIBCLOG_ERROR("DosSetFileInfo(%d,,,,) -> %d, oError=%#lx\n", pFH->fh, rc, EaOp2.oError);
    650                     if (rc != ERROR_EAS_NOT_SUPPORTED && pFH->pszNativePath)
    651                     {
    652                         EaOp2.oError = 0;
    653                         int rc2 = DosSetPathInfo((PCSZ)pFH->pszNativePath, FIL_QUERYEASIZE, &EaOp2, sizeof(EaOp2), 0);
    654                         if (rc2)
    655                             LIBCLOG_ERROR("DosSetPathInfo('%s',,,,) -> %d, oError=%#lx\n", pFH->pszNativePath, rc2, EaOp2.oError);
    656                     }
     647                    LIBCLOG_ERROR("__libc_back_fsNativeSetEAs('%s',,,,) -> %d, oError=%#lx\n", pFH->pszNativePath, rc, EaOp2.oError);
    657648                    if (rc == ERROR_EAS_NOT_SUPPORTED)
    658649                        rc = 0;
     
    683674
    684675/** @copydoc __LIBC_FHOPS::pfnSetOwner */
    685 static int fhOs2FileSetOwner(__LIBC_PFH pFH, uid_t uid)
    686 {
    687     LIBCLOG_ENTER("pFH=%p:{.fd=%d} uid=%d\n", (void *)pFH, pFH->fh, uid);
     676static int fhOs2FileSetOwner(__LIBC_PFH pFH, uid_t uid, gid_t gid)
     677{
     678    LIBCLOG_ENTER("pFH=%p:{.fd=%d} uid=%ld gid=%ld\n", (void *)pFH, pFH->fh, (long)uid, (long)gid);
    688679    int rc;
    689     /** @todo fchown */
    690     /*if (pFH->pszNativePath)
    691         rc = -ENOSYS;//rc = __libc_back_fsNativeSetOwner(pFH->pszNativePath, uid);
    692     else
    693         rc = -EINVAL;*/
    694     rc = -ENOSYS;
     680    if (pFH->pszNativePath)
     681        rc = __libc_back_fsNativeFileOwnerSetCommon(pFH->hNative, pFH->pszNativePath, pFH->pFsInfo ? pFH->pFsInfo->fUnixEAs : 0,
     682                                                    uid, gid);
     683    else
     684        rc = -EINVAL;
    695685    LIBCLOG_MIX_RETURN_INT(rc);
    696686}
  • trunk/libc/src/kNIX/os2/fs-os2.c

    r3778 r3816  
    11/* $Id$ */
    22/** @file
    3  *
    43 * kNIX - file system, OS/2 common bits.
    5  *
    6  * Copyright (c) 2004-2006 knut st. osmundsen <bird-src-spam@anduin.net>
     4 */
     5
     6/*
     7 * Copyright (c) 2004-2014 knut st. osmundsen <bird-src-spam@anduin.net>
    78 *
    89 *
     
    17411742
    17421743/**
     1744 * Helper path and handle based native functions for writing EAs.
     1745 *
     1746 * This tries to work around sharing issues as well as trying to use
     1747 * fchmod/fchown on handles opened without write access.
     1748 *
     1749 * @returns Native status code.
     1750 * @param   hNative         The native handle, -1 if not available.
     1751 * @param   pszNativePath   The native path.
     1752 * @param   pEAs            The list of EAs to set.
     1753 * @param   pEaOp2          The EA operation 2 buffer to use.  This is passed in
     1754 *                          as a parameter so that the calling code can access
     1755 *                          error information.
     1756 */
     1757int __libc_back_fsNativeSetEAs(intptr_t hNative, const char *pszNativePath, PFEA2LIST pEAs, PEAOP2 pEaOp2)
     1758{
     1759    /*
     1760     * First copy the EAs so we can do a retry with a different access path.
     1761     */
     1762    PFEA2LIST pCopyEAs = (PFEA2LIST)alloca(pEAs->cbList);
     1763    memcpy(pCopyEAs, pEAs, pEAs->cbList);
     1764
     1765    /*
     1766     * First try.
     1767     */
     1768    APIRET rc;
     1769    pEaOp2->fpGEA2List = NULL;
     1770    pEaOp2->fpFEA2List = pEAs;
     1771    pEaOp2->oError     = 0;
     1772    if (hNative != -1)
     1773        rc = DosSetFileInfo(hNative, FIL_QUERYEASIZE, pEaOp2, sizeof(*pEaOp2));
     1774    else
     1775        rc = DosSetPathInfo((PCSZ)pszNativePath, FIL_QUERYEASIZE, pEaOp2, sizeof(*pEaOp2), 0);
     1776
     1777    if (   (   rc == ERROR_SHARING_VIOLATION
     1778            || rc == ERROR_ACCESS_DENIED)
     1779        && pszNativePath
     1780        && pszNativePath[0])
     1781    {
     1782        /*
     1783         * To write EAs using DosSetPathInfo, the system requires deny-all
     1784         * access, but when using DosSetFileInfo we're seemingly free to choose
     1785         * this ourselves. So, try open the file with write access and retry.
     1786         */
     1787        APIRET rc2;
     1788        HFILE  hFile2      = (HFILE)-1;
     1789        ULONG  ulAction    = 0;
     1790        ULONG  flOpenFlags = OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_FAIL_IF_NEW;
     1791        ULONG  flOpenMode  = OPEN_ACCESS_WRITEONLY | OPEN_FLAGS_NOINHERIT | OPEN_SHARE_DENYNONE;
     1792
     1793#if OFF_MAX > LONG_MAX
     1794        if (__libc_gpfnDosOpenL)
     1795            rc2 = __libc_gpfnDosOpenL((PCSZ)pszNativePath, &hFile2, &ulAction, 0, FILE_NORMAL, flOpenFlags, flOpenMode, NULL);
     1796        else
     1797#endif
     1798            rc2 = DosOpen((PCSZ)pszNativePath, &hFile2, &ulAction, 0, FILE_NORMAL, flOpenFlags, flOpenMode, NULL);
     1799        if (rc2 == NO_ERROR)
     1800        {
     1801            memcpy(pEAs, pCopyEAs, pCopyEAs->cbList);
     1802            pEaOp2->fpGEA2List = NULL;
     1803            pEaOp2->fpFEA2List = pEAs;
     1804            pEaOp2->oError     = 0;
     1805            rc = DosSetFileInfo(hFile2, FIL_QUERYEASIZE, pEaOp2, sizeof(*pEaOp2));
     1806
     1807            DosClose(hFile2);
     1808        }
     1809    }
     1810
     1811    return rc;
     1812}
     1813
     1814
     1815/**
    17431816 * Reads the content of a symbolic link.
    17441817 *
  • trunk/libc/src/libc/io/fchown.c

    r2739 r3816  
    11/* $Id$ */
    22/** @file
    3  *
    43 * fchown()
    5  *
    6  * Copyright (c) 2004 knut st. osmundsen <bird-srcspam@anduin.net>
     4 */
     5
     6/*
     7 * Copyright (c) 2004-2014 knut st. osmundsen <bird-srcspam@anduin.net>
    78 *
    89 *
     
    2627
    2728
    28 
    2929/*******************************************************************************
    3030*   Header Files                                                               *
     
    3434#include <unistd.h>
    3535#include <errno.h>
    36 #include <klibc/io.h>
    37 #include <klibc/logstrict.h>
    38 
     36#include <klibc/backend.h>
     37#include <InnoTekLIBC/logstrict.h>
    3938
    4039
    4140/**
    42  * Change the owner and group over a file.
    43  *
    44  * This is a stub. It only validates the filehandle and returns without changing anything.
     41 * Change the owner and group if an open file.
    4542 *
    4643 * @returns 0 on success.
    4744 * @returns -1 and errno on failure.
    4845 * @param   fh      Handle to the file.
    49  * @param   owner   Owner id.
    50  * @param   group   Group id.
     46 * @param   uid     The user id of the new owner, pass -1 to not change it.
     47 * @param   gid     The group id of the new group, pass -1 to not change it.
    5148 */
    52 int _STD(fchown)(int fh, uid_t owner, gid_t group)
     49int _STD(fchown)(int fh, uid_t uid, gid_t gid)
    5350{
    54     LIBCLOG_ENTER("fh=%d owner=%d group=%d\n", fh, owner, group);
    55     __LIBC_PFH pFH;
    56     int rc = __libc_FHGet(fh, &pFH);
     51    LIBCLOG_ENTER("fh=%d uid=%ld gid=%ld\n", fh, (long)uid, (long)gid);
     52    int rc = __libc_Back_fsFileOwnerSetFH(fh, uid, gid);
    5753    if (!rc)
    58     {
    59         __libc_FHPut(pFH);
    6054        LIBCLOG_RETURN_INT(0);
    61     }
    6255    errno = -rc;
    6356    LIBCLOG_ERROR_RETURN_INT(-1);
  • trunk/libc/src/libc/io/lchown.c

    r2254 r3816  
    11/* $Id$ */
    22/** @file
    3  *
    43 * lchown()
    5  *
    6  * Copyright (c) 2004 knut st. osmundsen <bird-srcspam@anduin.net>
     4 */
     5
     6/*
     7 * Copyright (c) 2004-2014 knut st. osmundsen <bird-srcspam@anduin.net>
    78 *
    89 *
     
    2627
    2728
    28 
    2929/*******************************************************************************
    3030*   Header Files                                                               *
    3131*******************************************************************************/
     32#define __LIBC_LOG_GROUP __LIBC_LOG_GRP_IO
    3233#include "libc-alias.h"
    3334#include <unistd.h>
    34 #include <sys/stat.h>
    35 #define __LIBC_LOG_GROUP __LIBC_LOG_GRP_IO
     35#include <errno.h>
     36#include <klibc/backend.h>
    3637#include <InnoTekLIBC/logstrict.h>
    3738
    3839
    39 
    4040/**
    41  * Change the owner and group over a symbolic link.
    42  *
    43  * This is stub. It only validates the path and returns without changing anything.
     41 * Change the owner and group of a symbolic link or file.
    4442 *
    4543 * @returns 0 on success.
    4644 * @returns -1 and errno on failure.
    47  * @param   path    Path to the link to change owner/group of.
    48  * @param   owner   Owner id.
    49  * @param   group   Group id.
     45 * @param   pszPath Path to the file to change owner/group of, symbolic link in
     46 *                  the final component is not followed.
     47 * @param   uid     The user id of the new owner, pass -1 to not change it.
     48 * @param   gid     The group id of the new group, pass -1 to not change it.
    5049 */
    51 int      _STD(lchown)(const char *path, uid_t owner, gid_t group)
     50int      _STD(lchown)(const char *pszPath, uid_t uid, gid_t gid)
    5251{
    53     LIBCLOG_ENTER("path=%p:{%s} owner=%d group=%d\n", (void *)path, path, owner, group);
    54     struct stat st;
    55     int rc = lstat(path, &st);
     52    LIBCLOG_ENTER("pszPath=%p:{%s} uid=%ld gid=%ld\n", (void *)pszPath, pszPath, (long)uid, (long)gid);
     53    int rc = __libc_Back_fsSymlinkOwnerSet(pszPath, uid, gid);
    5654    if (!rc)
    5755        LIBCLOG_RETURN_INT(0);
     56    errno = -rc;
    5857    LIBCLOG_ERROR_RETURN_INT(-1);
    5958}
  • trunk/libc/src/libc/misc/chown.c

    r2254 r3816  
    11/* $Id$ */
    22/** @file
    3  *
    43 * chown()
    5  *
    6  * Copyright (c) 2004 knut st. osmundsen <bird-srcspam@anduin.net>
     4 */
     5
     6/*
     7 * Copyright (c) 2004-2014 knut st. osmundsen <bird-srcspam@anduin.net>
    78 *
    89 *
     
    2627
    2728
    28 
    2929/*******************************************************************************
    3030*   Header Files                                                               *
    3131*******************************************************************************/
     32#define __LIBC_LOG_GROUP __LIBC_LOG_GRP_IO
    3233#include "libc-alias.h"
    3334#include <unistd.h>
    34 #include <sys/stat.h>
    35 #define __LIBC_LOG_GROUP __LIBC_LOG_GRP_IO
     35#include <errno.h>
     36#include <klibc/backend.h>
    3637#include <InnoTekLIBC/logstrict.h>
    3738
    3839
    39 
    4040/**
    41  * Change the owner and group over a file.
    42  *
    43  * This is stub. It only validates the path and returns without changing anything.
     41 * Change the owner and group of a file.
    4442 *
    4543 * @returns 0 on success.
    4644 * @returns -1 and errno on failure.
    47  * @param   path    Path to the file to change owner/group of.
    48  * @param   owner   Owner id.
    49  * @param   group   Group id.
     45 * @param   pszPath Path to the file to change owner/group of, all symbolic
     46 *                  links are followed.
     47 * @param   uid     The user id of the new owner, pass -1 to not change it.
     48 * @param   gid     The group id of the new group, pass -1 to not change it.
    5049 */
    51 int      _STD(chown)(const char *path, uid_t owner, gid_t group)
     50int      _STD(chown)(const char *pszPath, uid_t uid, gid_t gid)
    5251{
    53     LIBCLOG_ENTER("path=%p:{%s} owner=%d group=%d\n", (void *)path, path, owner, group);
    54     struct stat st;
    55     int rc = stat(path, &st);
     52    LIBCLOG_ENTER("pszPath=%p:{%s} uid=%ld gid=%ld\n", (void *)pszPath, pszPath, (long)uid, (long)gid);
     53    int rc = __libc_Back_fsFileOwnerSet(pszPath, uid, gid);
    5654    if (!rc)
    57         LIBCLOG_RETURN_INT(rc);
    58     LIBCLOG_ERROR_RETURN_INT(rc);
     55        LIBCLOG_RETURN_INT(0);
     56    errno = -rc;
     57    LIBCLOG_ERROR_RETURN_INT(-1);
    5958}
    6059
  • trunk/libc/tests/libc/io/chown-1.c

    r3814 r3816  
    5656        } \
    5757        else \
    58             assertOwners(fd, szTmp, a_uidExpect, a_gidExpect, #a_OperationExpr " at line " XSTR(__LINE__)); \
     58            assertOwners(fd, szTmp, a_uidExpect, a_gidExpect, " " #a_OperationExpr " at line " XSTR(__LINE__)); \
    5959    } while (0)
    6060
     
    176176    TEST_CHANGE(chown(szTmp,  -1, st1.st_gid + 3), st1.st_uid + 2, st1.st_gid + 3);
    177177
     178    close(fd);
     179    fd = -1;
     180
     181    TEST_CHANGE(lchown(szTmp, st1.st_uid + 1, st1.st_gid + 1), st1.st_uid + 1, st1.st_gid + 1);
     182    TEST_CHANGE(chown(szTmp,  st1.st_uid + 1, st1.st_gid + 1), st1.st_uid + 1, st1.st_gid + 1);
     183
     184    TEST_CHANGE(lchown(szTmp, st1.st_uid + 2, -1), st1.st_uid + 2, st1.st_gid + 1);
     185    TEST_CHANGE(chown(szTmp,  st1.st_uid + 2, -1), st1.st_uid + 2, st1.st_gid + 1);
     186
     187    TEST_CHANGE(lchown(szTmp, -1, st1.st_gid + 3), st1.st_uid + 2, st1.st_gid + 3);
     188    TEST_CHANGE(chown(szTmp,  -1, st1.st_gid + 3), st1.st_uid + 2, st1.st_gid + 3);
     189
    178190    /*
    179191     * Cleanup.
    180192     */
    181     close(fd);
    182193    if (unlink(szTmp))
    183194    {
Note: See TracChangeset for help on using the changeset viewer.