Previous page

Next page

Locate page in Contents

Managing Files In The Host OS

The following file management operations can be performed using the Parallels C API on the host machine:

  • Obtaining a directory listing.
  • Creating directories.
  • Automatically generate unique names for new file system entries.
  • Rename file system entries.
  • Delete file system entries.

The file management functionality can be accessed through the PHT_SERVER handle. The file management functions are prefixed with "PrlSrv_Fs".

Obtaining the host OS directory listing

The directory listing is obtained using the PrlSrv_FsGetDirEntries function. The function returns a handle of type PHT_REMOTE_FILESYSTEM_INFO containing the information about the specified file system entry and its immediate child entries (if any). The child entries are returned as a list of handles of type PHT_REMOTE_FILESYSTEM_ENTRY which is included in the PHT_REMOTE_FILESYSTEM_INFO object. The sample function below demonstrates how to obtain a listing for the specified directory. On initial call, the function obtains a list of child entries (files and sub-directories) for the specified directory and is then called recursively for each file system entry returned. On completion, the entire directory tree will be displayed on the screen.

// Obtains the entire directory tree in the host OS

// starting at the specified path.

// The "levels" parameter specifies how many levels should the

// function traverse down the directory tree.

PRL_RESULT GetHostDirList(PRL_HANDLE hServer, PRL_CONST_STR path, int levels)

{

    PRL_HANDLE hJob = PRL_INVALID_HANDLE;

    PRL_HANDLE hJobResult = PRL_INVALID_HANDLE;

    PRL_HANDLE hParentDirectory = PRL_INVALID_HANDLE;

    PRL_HANDLE hChildElement = PRL_INVALID_HANDLE;

    

    PRL_RESULT ret = PRL_ERR_UNINITIALIZED;

    PRL_RESULT nJobReturnCode = PRL_ERR_UNINITIALIZED;

  

    // Get directory list from the host.

    // The second parameter specifies the absolute

    // path for which to get the directory listing.

    hJob = PrlSrv_FsGetDirEntries(hServer, path);

    

    // Wait for the job to complete.

    ret = PrlJob_Wait(hJob, 1000);

    if (PRL_FAILED(ret))

    {

        // Handle the error...

        return -1;

    }

    

    // Analyze the result of PrlSrv_FsGetDirEntries.

    ret = PrlJob_GetRetCode(hJob, &nJobReturnCode);

    if (PRL_FAILED(ret))

    {

        // Handle the error...

        PrlHandle_Free(hJob);

        return -1;

    }

    // Check the job return code.

    if (PRL_FAILED(nJobReturnCode))

    {

        // Handle the error...

        PrlHandle_Free(hJob);

        return -1;

    }

    

    // Get job result.

    ret = PrlJob_GetResult(hJob, &hJobResult);

    PrlHandle_Free(hJob);

    if (PRL_FAILED(ret))

    {

        // Handle the error...

        return -1;

    }

    

    // Get a handle to the parent directory.

    // This is the directory that we specified in the

    // PrlSrv_FsGetDirEntries call above.

    ret = PrlResult_GetParam(hJobResult, &hParentDirectory);

    PrlHandle_Free(hJobResult);

    if (PRL_FAILED(ret))

    {

        // Handle the error...

        return -1;

    }

    

    // Get parameter count (the number of child entries).

    PRL_UINT32 nParamCount = 0;

    ret = PrlFsInfo_GetChildEntriesCount(hParentDirectory, &nParamCount);

    if (PRL_FAILED(ret))

    {

        // Handle the error...

        PrlHandle_Free(hJob);

        return -1;

    }

    

    // Iterate through the list obtaining

    // a handle of type PHT_REMOTE_FILESYSTEM_ENTRY

    // for each child element of the parent directory.

    for (PRL_UINT32 i = 0; i < nParamCount; ++i)

    {

        // Get a handle to the child element.

        ret = PrlFsInfo_GetChildEntry(hParentDirectory, i, &hChildElement);

        if (PRL_FAILED(ret))

        {

            // Handle the error...

            continue;

        }

    

        // Get the filesystem element name.

        PRL_CHAR sBuf[1024];

        PRL_UINT32 nBufSize = sizeof(sBuf);

        ret = PrlFsEntry_GetAbsolutePath(hChildElement, sBuf, &nBufSize);

        if (PRL_FAILED(ret))

        {

            // Handle the error...

            PrlHandle_Free(hChildElement);

            continue;

        }

    

        printf("%s\n", sBuf);

        PrlHandle_Free(hChildElement);

    

        // Recursive call. Obtains directory listing for

        // the entry returned in this iteration.

        if (levels > 0 || levels <= -1)

        {

            int count = levels - 1;

            GetHostDirList(hServer, sBuf, count);

        }

    }

    PrlHandle_Free(hParentDirectory);

    PrlHandle_Free(hJob);

  

    return PRL_ERR_SUCCESS;

}