Changeset 2542 for branches/libc-0.6


Ignore:
Timestamp:
Feb 7, 2006, 3:24:44 AM (20 years ago)
Author:
bird
Message:

#51: Made unlink able to delete readonly files.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/libc-0.6/src/emx/src/lib/sys/b_fsUnlink.c

    r2254 r2542  
    8080     * times.
    8181     */
    82     static int fUseForce = 0; /* state: 0 - uninit, 1 - DosForceDelete, -1 - DosDelete */
    83     if (fUseForce == 0)
     82    static int s_fUseForce = 0; /* state: 0 - uninit, 1 - DosForceDelete, -1 - DosDelete */
     83    if (s_fUseForce == 0)
    8484    {
    8585        PSZ psz = NULL;
    8686        if (DosScanEnv((PCSZ)"DELDIR", &psz) || !psz)
    87             fUseForce = 1;
     87            s_fUseForce = 1;
    8888        else
    89             fUseForce = -1;
     89            s_fUseForce = -1;
    9090    }
    9191
     
    9393     * We'll attempt delete it as a file first.
    9494     */
    95     if (fUseForce == 1)
     95    if (s_fUseForce == 1)
    9696        rc = DosForceDelete((PCSZ)&szNativePath[0]);
    9797    else
     
    100100    {
    101101        /*
    102          * Ok, try delete it as a directory then.
     102         * There are three causes here:
     103         *      1) the file is marked read-only.
     104         *      2) it's a directory.
     105         *      3) we are denied access - network, hpfs386 or SES.
     106         *
     107         * If it's either of the first two we are subject to race conditions, so we
     108         * have to retry. The third cause is distiguishable from the two othes by
     109         * the failing DosSetPathInfo.
    103110         */
    104         rc = DosDeleteDir((PCSZ)&szNativePath[0]);
     111        int fDirectory = 0;
     112        for (unsigned i = 0; (rc == ERROR_ACCESS_DENIED || rc == ERROR_PATH_NOT_FOUND) && i < 2; i++)
     113        {
     114            FILESTATUS3 fsts3;
     115            rc = DosQueryPathInfo((PCSZ)&szNativePath[0], FIL_STANDARD, &fsts3, sizeof(fsts3));
     116            if (!rc)
     117            {
     118                fDirectory = (fsts3.attrFile & FILE_DIRECTORY) != 0;
     119
     120                /* turn of the read-only attribute */
     121                if (fsts3.attrFile & FILE_READONLY)
     122                {
     123                    fsts3.attrFile &= ~FILE_READONLY;
     124                    rc = DosSetPathInfo((PCSZ)&szNativePath[0], FIL_STANDARD, &fsts3, sizeof(fsts3), 0);
     125                    LIBCLOG_MSG("attempt at disabling the R attribute -> %d\n", rc);
     126                    if (    rc == ERROR_ACCESS_DENIED
     127                        ||  rc == ERROR_SHARING_VIOLATION)
     128                    {
     129                        rc = ERROR_ACCESS_DENIED;
     130                        break;
     131                    }
     132                }
     133
     134                /* retry */
     135                if (fDirectory)
     136                    rc = DosDeleteDir((PCSZ)&szNativePath[0]);
     137                else if (s_fUseForce == 1)
     138                    rc = DosForceDelete((PCSZ)&szNativePath[0]);
     139                else
     140                    rc = DosDelete((PCSZ)&szNativePath[0]);
     141            }
     142            else
     143            {
     144                rc = ERROR_ACCESS_DENIED;
     145                break;
     146            }
     147        }
     148
     149        /*
     150         * OS/2 returns access denied when the directory
     151         * contains files or it is not a directory. Check for
     152         * directory/other and return failure accordingly.
     153         */
    105154        if (rc)
    106155        {
    107             /*
    108              * OS/2 returns access denied when the directory
    109              * contains files or it is not a directory. Check for
    110              * directory/other and return failure accordingly.
    111              */
    112156            if (rc == ERROR_ACCESS_DENIED)
    113             {
    114                 struct stat s;
    115                 rc = __libc_back_fsNativeFileStat(&szNativePath[0], &s);
    116                 if (!rc && S_ISDIR(s.st_mode))
    117                     rc = -ENOTEMPTY;
    118                 else
    119                     rc = -EACCES;
    120             }
     157                rc = fDirectory ? -ENOTEMPTY : -EACCES;
    121158            else
    122159                rc = -__libc_native2errno(rc);
Note: See TracChangeset for help on using the changeset viewer.