Beispiel #1
0
/*++
Function:
  GetCurrentDirectoryW

See MSDN doc.
--*/
DWORD
PALAPI
GetCurrentDirectoryW(
             IN DWORD nBufferLength,
             OUT LPWSTR lpBuffer)
{
    DWORD dwWideLen = 0;
    DWORD dwLastError = 0;

    char  *current_dir;
    int   dir_len;

    PERF_ENTRY(GetCurrentDirectoryW);
    ENTRY("GetCurrentDirectoryW(nBufferLength=%u, lpBuffer=%p)\n",
          nBufferLength, lpBuffer);

    current_dir = PAL__getcwd( NULL, MAX_LONGPATH + 1 );

    if ( !current_dir )
    {
        WARN( "PAL__getcwd returned NULL\n" );
        dwLastError = DIRGetLastErrorFromErrno();
        goto done;
    }

    dir_len = strlen( current_dir );
    dwWideLen = MultiByteToWideChar( CP_ACP, 0,
                 current_dir, dir_len,
                 NULL, 0 );

    /* if the supplied buffer isn't long enough, return the required
       length, including room for the NULL terminator */
    if ( nBufferLength > dwWideLen )
    {
        if(!MultiByteToWideChar( CP_ACP, 0, current_dir, dir_len + 1,
                     lpBuffer, nBufferLength ))
        {
            ASSERT("MultiByteToWideChar failure!\n");
            dwWideLen = 0;
            dwLastError = ERROR_INTERNAL_ERROR;
        }
    }
    else
    {
        ++dwWideLen; /* include space for the NULL */
    }

done:
    PAL_free( current_dir );

    if ( dwLastError )
    {
        SetLastError(dwLastError);
    }

    LOGEXIT("GetCurrentDirectoryW returns DWORD %u\n", dwWideLen);
    PERF_EXIT(GetCurrentDirectoryW);
    return dwWideLen;
}
Beispiel #2
0
/*++
Function:
  GetCurrentDirectoryW

See MSDN doc.
--*/
DWORD
PALAPI
GetCurrentDirectoryW(
    IN DWORD nBufferLength,
    OUT LPWSTR lpBuffer)
{
    DWORD dwWideLen = 0;
    DWORD dwLastError = ERROR_BAD_PATHNAME;
    int   dir_len;
    PathCharString  current_dir;

    PERF_ENTRY(GetCurrentDirectoryW);
    ENTRY("GetCurrentDirectoryW(nBufferLength=%u, lpBuffer=%p)\n",
          nBufferLength, lpBuffer);


    dir_len = GetCurrentDirectoryA(current_dir);

    if( dir_len == 0)
    {
        dwLastError = DIRGetLastErrorFromErrno();
        goto done;
    }

    dwWideLen = MultiByteToWideChar( CP_ACP, 0,
                                     current_dir, dir_len,
                                     NULL, 0 );

    /* if the supplied buffer isn't long enough, return the required
       length, including room for the NULL terminator */
    if ( nBufferLength > dwWideLen )
    {
        if(!MultiByteToWideChar( CP_ACP, 0, current_dir, dir_len + 1,
                                 lpBuffer, nBufferLength ))
        {
            ASSERT("MultiByteToWideChar failure!\n");
            dwWideLen = 0;
            dwLastError = ERROR_INTERNAL_ERROR;
        }
    }
    else
    {
        ++dwWideLen; /* include the  space for the NULL */
    }

done:

    if ( dwLastError )
    {
        SetLastError(dwLastError);
    }

    LOGEXIT("GetCurrentDirectoryW returns DWORD %u\n", dwWideLen);
    PERF_EXIT(GetCurrentDirectoryW);
    return dwWideLen;
}
Beispiel #3
0
/*++
Function:
  GetCurrentDirectoryA

See MSDN doc.
--*/
DWORD
PALAPI
GetCurrentDirectoryA(
             IN DWORD nBufferLength,
             OUT LPSTR lpBuffer)
{
    DWORD dwDirLen = 0;
    DWORD dwLastError = 0;

    char  *current_dir;

    PERF_ENTRY(GetCurrentDirectoryA);
    ENTRY("GetCurrentDirectoryA(nBufferLength=%u, lpBuffer=%p)\n", nBufferLength, lpBuffer);

    /* NULL first arg means getcwd will allocate the string */
    current_dir = PAL__getcwd( NULL, MAX_LONGPATH + 1 );

    if ( !current_dir )
    {
        WARN( "PAL__getcwd returned NULL\n" );
        dwLastError = DIRGetLastErrorFromErrno();
        goto done;
    }

    dwDirLen = strlen( current_dir );

    /* if the supplied buffer isn't long enough, return the required
       length, including room for the NULL terminator */
    if ( nBufferLength <= dwDirLen )
    {
        ++dwDirLen; /* include space for the NULL */
        goto done;
    }
    else
    {
        strcpy_s( lpBuffer, nBufferLength, current_dir );
    }

done:
    PAL_free( current_dir );

    if ( dwLastError )
    {
        SetLastError(dwLastError);
    }

    LOGEXIT("GetCurrentDirectoryA returns DWORD %u\n", dwDirLen);
    PERF_EXIT(GetCurrentDirectoryA);
    return dwDirLen;
}
Beispiel #4
0
/*++
Function:
  GetCurrentDirectoryA

--*/
DWORD
GetCurrentDirectoryA(PathCharString& lpBuffer)
{
    DWORD dwDirLen = 0;
    DWORD dwLastError = 0;

    char  *current_dir;

    PERF_ENTRY(GetCurrentDirectoryA);
    ENTRY("GetCurrentDirectoryA(lpBuffer=%p)\n", lpBuffer.GetString());

    current_dir = lpBuffer.OpenStringBuffer(MAX_PATH);
    /* NULL first arg means getcwd will allocate the string */
    current_dir = PAL__getcwd( current_dir, MAX_PATH);

    if (current_dir != NULL )
    {
        dwDirLen = strlen( current_dir );
        lpBuffer.CloseBuffer(dwDirLen);
        goto done;
    }
    else if ( errno == ERANGE )
    {
        lpBuffer.CloseBuffer(0);
        current_dir = PAL__getcwd( NULL, 0);
    }

    if ( !current_dir )
    {
        WARN("Getcwd failed with errno=%d [%s]\n", errno, strerror(errno));
        dwLastError = DIRGetLastErrorFromErrno();
        dwDirLen = 0;
        goto done;
    }

    dwDirLen = strlen( current_dir );
    lpBuffer.Set(current_dir, dwDirLen);
    PAL_free(current_dir);
done:

    if ( dwLastError )
    {
        SetLastError(dwLastError);
    }

    LOGEXIT("GetCurrentDirectoryA returns DWORD %u\n", dwDirLen);
    PERF_EXIT(GetCurrentDirectoryA);
    return dwDirLen;
}
Beispiel #5
0
/*++
Function:
  CreateDirectoryA

Note:
  lpSecurityAttributes always NULL.

See MSDN doc.
--*/
BOOL
PALAPI
CreateDirectoryA(
         IN LPCSTR lpPathName,
         IN LPSECURITY_ATTRIBUTES lpSecurityAttributes)
{
    BOOL  bRet = FALSE;
    DWORD dwLastError = 0;
    char *realPath;
    LPSTR UnixPathName = NULL;
    int pathLength;
    int i;
    const int mode = S_IRWXU | S_IRWXG | S_IRWXO;

    PERF_ENTRY(CreateDirectoryA);
    ENTRY("CreateDirectoryA(lpPathName=%p (%s), lpSecurityAttr=%p)\n",
          lpPathName?lpPathName:"NULL",
          lpPathName?lpPathName:"NULL", lpSecurityAttributes);

    if ( lpSecurityAttributes )
    {
        ASSERT("lpSecurityAttributes is not NULL as it should be\n");
        dwLastError = ERROR_INVALID_PARAMETER;
        goto done;
    }

    // Windows returns ERROR_PATH_NOT_FOUND when called with NULL.
    // If we don't have this check, strdup(NULL) segfaults.
    if (lpPathName == NULL)
    {
        ERROR("CreateDirectoryA called with NULL pathname!\n");
        dwLastError = ERROR_PATH_NOT_FOUND;
        goto done;
    }

    UnixPathName = PAL__strdup(lpPathName);
    if (UnixPathName == NULL )
    {
        ERROR("PAL__strdup() failed\n");
        dwLastError = ERROR_NOT_ENOUGH_MEMORY;
        goto done;
    }
    FILEDosToUnixPathA( UnixPathName );
    // Remove any trailing slashes at the end because mkdir might not
    // handle them appropriately on all platforms.
    pathLength = strlen(UnixPathName);
    i = pathLength;
    while(i > 1)
    {
        if(UnixPathName[i - 1] =='/')
        {
            UnixPathName[i - 1]='\0';
            i--;
        }
        else
        {
            break;
        }
    }

    // Check the constraint for the real path length (should be < MAX_LONGPATH).

    // Get an absolute path.
    if (UnixPathName[0] == '/')
    {
        realPath = UnixPathName;
    }
    else
    {
        const char *cwd = PAL__getcwd(NULL, MAX_LONGPATH);        
        if (NULL == cwd)
        {
            WARN("Getcwd failed with errno=%d [%s]\n", errno, strerror(errno));
            dwLastError = DIRGetLastErrorFromErrno();
            goto done;
        }

        // Copy cwd, '/', path
        int iLen = strlen(cwd) + 1 + pathLength + 1;
        realPath = static_cast<char *>(alloca(iLen));
        sprintf_s(realPath, iLen, "%s/%s", cwd, UnixPathName);

        PAL_free((char *)cwd);
    }
    
    // Canonicalize the path so we can determine its length.
    FILECanonicalizePath(realPath);

    if (strlen(realPath) >= MAX_LONGPATH)
    {
        WARN("UnixPathName is larger than MAX_LONGPATH (%d)!\n", MAX_LONGPATH);
        dwLastError = ERROR_FILENAME_EXCED_RANGE;
        goto done;
    }

    if ( mkdir(realPath, mode) != 0 )
    {
        TRACE("Creation of directory [%s] was unsuccessful, errno = %d.\n",
              UnixPathName, errno);

        switch( errno )
        {
        case ENOTDIR:
            /* FALL THROUGH */
        case ENOENT:
            FILEGetProperNotFoundError( realPath, &dwLastError );
            goto done;
        case EEXIST:
            dwLastError = ERROR_ALREADY_EXISTS;
            break;
        default:
            dwLastError = ERROR_ACCESS_DENIED;
        }
    }
    else
    {
        TRACE("Creation of directory [%s] was successful.\n", UnixPathName);
        bRet = TRUE;
    }

done:
    if( dwLastError )
    {
        SetLastError( dwLastError );
    }
    PAL_free( UnixPathName );
    LOGEXIT("CreateDirectoryA returns BOOL %d\n", bRet);
    PERF_EXIT(CreateDirectoryA);
    return bRet;
}
Beispiel #6
0
/*++
Function:
  CreateDirectoryA

Note:
  lpSecurityAttributes always NULL.

See MSDN doc.
--*/
BOOL
PALAPI
CreateDirectoryA(
    IN LPCSTR lpPathName,
    IN LPSECURITY_ATTRIBUTES lpSecurityAttributes)
{
    BOOL  bRet = FALSE;
    DWORD dwLastError = 0;
    PathCharString realPath;
    char* realPathBuf;
    LPSTR unixPathName = NULL;
    int pathLength;
    int i;
    const int mode = S_IRWXU | S_IRWXG | S_IRWXO;

    PERF_ENTRY(CreateDirectoryA);
    ENTRY("CreateDirectoryA(lpPathName=%p (%s), lpSecurityAttr=%p)\n",
          lpPathName?lpPathName:"NULL",
          lpPathName?lpPathName:"NULL", lpSecurityAttributes);

    if ( lpSecurityAttributes )
    {
        ASSERT("lpSecurityAttributes is not NULL as it should be\n");
        dwLastError = ERROR_INVALID_PARAMETER;
        goto done;
    }

    // Windows returns ERROR_PATH_NOT_FOUND when called with NULL.
    // If we don't have this check, strdup(NULL) segfaults.
    if (lpPathName == NULL)
    {
        ERROR("CreateDirectoryA called with NULL pathname!\n");
        dwLastError = ERROR_PATH_NOT_FOUND;
        goto done;
    }

    unixPathName = PAL__strdup(lpPathName);
    if (unixPathName == NULL )
    {
        ERROR("PAL__strdup() failed\n");
        dwLastError = ERROR_NOT_ENOUGH_MEMORY;
        goto done;
    }
    FILEDosToUnixPathA( unixPathName );
    // Remove any trailing slashes at the end because mkdir might not
    // handle them appropriately on all platforms.
    pathLength = strlen(unixPathName);
    i = pathLength;
    while(i > 1)
    {
        if(unixPathName[i - 1] =='/')
        {
            unixPathName[i - 1]='\0';
            i--;
        }
        else
        {
            break;
        }
    }


    // Get an absolute path.
    if (unixPathName[0] == '/')
    {
        realPathBuf = unixPathName;
    }
    else
    {

        DWORD len = GetCurrentDirectoryA(realPath);
        if (len == 0 || !realPath.Reserve(realPath.GetCount() + pathLength + 1 ))
        {
            dwLastError = DIRGetLastErrorFromErrno();
            WARN("Getcwd failed with errno=%d \n", dwLastError);
            goto done;
        }

        realPath.Append("/", 1);
        realPath.Append(unixPathName, pathLength);
        realPathBuf = realPath.OpenStringBuffer(realPath.GetCount());
    }

    // Canonicalize the path so we can determine its length.
    FILECanonicalizePath(realPathBuf);

    if ( mkdir(realPathBuf, mode) != 0 )
    {
        TRACE("Creation of directory [%s] was unsuccessful, errno = %d.\n",
              unixPathName, errno);

        switch( errno )
        {
        case ENOTDIR:
        /* FALL THROUGH */
        case ENOENT:
            FILEGetProperNotFoundError( realPathBuf, &dwLastError );
            goto done;
        case EEXIST:
            dwLastError = ERROR_ALREADY_EXISTS;
            break;
        default:
            dwLastError = ERROR_ACCESS_DENIED;
        }
    }
    else
    {
        TRACE("Creation of directory [%s] was successful.\n", unixPathName);
        bRet = TRUE;
    }

    realPath.CloseBuffer(0); //The PathCharString usage is done
done:
    if( dwLastError )
    {
        SetLastError( dwLastError );
    }
    PAL_free( unixPathName );
    LOGEXIT("CreateDirectoryA returns BOOL %d\n", bRet);
    PERF_EXIT(CreateDirectoryA);
    return bRet;
}