VirtualBox

Changeset 2681 in kBuild for trunk/src/kash/shfile.c


Ignore:
Timestamp:
Apr 14, 2013 6:41:58 PM (12 years ago)
Author:
bird
Message:

shfile.c: Fixed windows problem with stat() failing if the directory ends with a slash.\

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/kash/shfile.c

    r2653 r2681  
    427427
    428428# if K_OS == K_OS_WINDOWS
     429
     430/**
     431 * Adjusts the file name if it ends with a trailing directory slash.
     432 *
     433 * Windows APIs doesn't like trailing slashes.
     434 *
     435 * @returns 1 if it has a directory slash, 0 if not.
     436 *
     437 * @param   abspath     The path to adjust (SHFILE_MAX_PATH).
     438 */
     439static int shfile_trailing_slash_hack(char *abspath)
     440{
     441    /*
     442     * Anything worth adjust here?
     443     */
     444    size_t path_len = strlen(abspath);
     445    if (   path_len == 0
     446        || (   abspath[path_len - 1] != '/'
     447#  if K_OS == K_OS_WINDOWS || K_OS == K_OS_OS2
     448            && abspath[path_len - 1] != '\\'
     449#  endif
     450           )
     451       )
     452        return 0;
     453
     454    /*
     455     * Ok, make the adjustment.
     456     */
     457    if (path_len + 2 <= SHFILE_MAX_PATH)
     458    {
     459        /* Add a '.' to the end. */
     460        abspath[path_len++] = '.';
     461        abspath[path_len]   = '\0';
     462    }
     463    else
     464    {
     465        /* No space for a dot, remove the slash if it's alone or just remove
     466           one and add a dot like above. */
     467        if (   abspath[path_len - 2] != '/'
     468#  if K_OS == K_OS_WINDOWS || K_OS == K_OS_OS2
     469            && abspath[path_len - 2] != '\\'
     470#  endif
     471           )
     472            abspath[--path_len] = '\0';
     473        else
     474            abspath[path_len - 1] = '.';
     475    }
     476
     477    return 1;
     478}
     479
    429480
    430481/**
     
    15551606    {
    15561607# if K_OS == K_OS_WINDOWS
     1608        int dir_slash = shfile_trailing_slash_hack(abspath);
    15571609        rc = stat(abspath, pst); /** @todo re-implement stat. */
     1610        if (!rc && dir_slash && !S_ISDIR(pst->st_mode))
     1611        {
     1612            rc = -1;
     1613            errno = ENOTDIR;
     1614        }
    15581615# else
    15591616        rc = stat(abspath, pst);
     
    15771634    {
    15781635# if K_OS == K_OS_WINDOWS
    1579         rc = stat(abspath, pst); /** @todo implement lstat. */
     1636        int dir_slash = shfile_trailing_slash_hack(abspath);
     1637        rc = stat(abspath, pst); /** @todo re-implement stat. */
     1638        if (!rc && dir_slash && !S_ISDIR(pst->st_mode))
     1639        {
     1640            rc = -1;
     1641            errno = ENOTDIR;
     1642        }
    15801643# else
    15811644        rc = lstat(abspath, pst);
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette