> ## Documentation Index
> Fetch the complete documentation index at: https://devdocs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# XtfGetAvailableSpaceForAppInstallation

> XtfGetAvailableSpaceForAppInstallation

# XtfGetAvailableSpaceForAppInstallation

開発本体で指定されたストレージ デバイス上の利用可能な合計バイト数を取得します。

<a id="syntaxSection" />

## 構文

```cpp theme={null}
HRESULT XtfGetAvailableSpaceForAppInstallation(
         LPCWSTR address,
         LPCWSTR storageName,
         UINT64 *bytes
)  
```

<a id="parametersSection" />

### パラメーター

*address*\
型: LPCWSTR

\[in] 確認する開発本体のアドレスです。

*storageName*\
型: LPCWSTR

\[in] 確認するストレージ デバイスの名前です。 次の値を指定できます。

| 値         | 説明                  |
| --------- | ------------------- |
| `nullptr` | デフォルトのストレージ デバイスです。 |
| internal  | 内部ストレージ デバイスです。     |

*bytes*\
型: UINT64\*

\[out] *address* で指定された開発本体で、*storageName* で指定されたストレージ デバイス上の利用可能な合計バイト数です。

<a id="retvalSection" />

### 戻り値

型: HRESULT

成功した場合は `S_OK` を返します。それ以外の場合は HRESULT エラー コードを返します。

<a id="remarksSection" />

## 解説

この関数は開発本体で指定されたストレージ デバイス上の利用可能な合計バイト数を取得します。これにより、本体用のカスタム デプロイ ツールを作成しているツール開発者は、インストール プロセスを開始する前にアプリ パッケージをインストールするための十分な領域があるかどうかを確認できます。 XBOX Tools Framework (XTF) ツールの詳細については、[コマンドライン ツール (NDA トピック)](/tools/tools-console/commandlinetools/commandlinetools) を参照してください。

次のコード サンプルは、アプリ パッケージに必要な領域を計算する方法を示しています。

```cpp theme={null}
#include <stdio.h>
#include <conio.h>
#include <Windows.h>
#include <xtfapi.h>

//Round up file sizes to the nearest 4K boundary to get a better estimate of the actual size of disk
#define ESTIMATED_FILE_SIZE_ON_DISK(fileSize) ((fileSize + ((4UI64 * 1024UI64) - 1)) & ~((4UI64 * 1024UI64) - 1))

//Size of the buffer (string) that we use to contain file/directory names - MAX_PATH + 1 for the terminating null
#define DIR_BUFFER_LENGTH (MAX_PATH + 1)

//Forward declaration of the function that figures out the game size on disk
UINT64 EstimateRequiredSpace(LPWSTR targetDir) ;

//Calculates whether there is enough disk space to deploy the game
//Returns an ERRORLEVEL:
//   0 - OK - Enough space
//   1 - ERROR - Not enough space
//   2 - ERROR - Other error
int wmain(int argc, wchar_t **argv)
{
    HRESULT hr              = S_OK;
    UINT64  availableBytes  = 0;
    UINT64  requiredBytes   = 0;
    LPWSTR  consoleAddress  = nullptr;
    LPWSTR  targetDir       = nullptr;

    if (argc != 3)
    {
        wprintf(L"\nUsage: %s <consoleAddress> <directory name>\n", argv[0]);
        return 2;
    }

    consoleAddress = argv[1];
    targetDir = argv[2];

    wprintf(L"\nTarget console is %s\n", consoleAddress);
    wprintf(L"\nTarget directory is %s\n\n",targetDir);

    hr = XtfGetAvailableSpaceForAppInstallation(consoleAddress, L"Internal", &availableBytes);
    if (hr != S_OK)
    {
        wprintf(L"\n\n*** Error %d", hr);
        return 2;
    }

    requiredBytes = EstimateRequiredSpace(targetDir);
    if (hr != S_OK)
    {
        wprintf(L"\n\n*** Error calculating size");
        return 2;
    }

    wprintf(L"*** Space available on console is %I64u \n", availableBytes);
    wprintf(L"*** Space required is %I64u \n", requiredBytes);

    if (requiredBytes > availableBytes)
    {
        wprintf(L"*** DO NOT DEPLOY \n");
        return 1;
    }

    wprintf(L"*** OK TO DEPLOY \n");
    return 0;

}

//Function that figures out the game size on disk
UINT64 EstimateRequiredSpace(LPWSTR targetDir)
{
    WIN32_FIND_DATA  ffd;
    LARGE_INTEGER    filesize;
    TCHAR            szDir[DIR_BUFFER_LENGTH];
    size_t           targetDirLength    = 0;
    HANDLE           hFind              = INVALID_HANDLE_VALUE;
    DWORD            dwError            = 0;
    UINT64           requiredBytes      = 0;

    // Check that the input path plus 3 is not longer than the size of szDir.
    // Three characters are for the "\*" plus NULL appended below.
    targetDirLength = wcsnlen_s(targetDir, DIR_BUFFER_LENGTH);

    if (targetDirLength + 3 > DIR_BUFFER_LENGTH)
    {
        wprintf(L"\nDirectory path is too long.\n");
        return -1;
    }

    // Create the search string for FindFile functions.
    wcscpy_s(szDir, DIR_BUFFER_LENGTH, targetDir);
    wcscat_s(szDir, DIR_BUFFER_LENGTH, L"\\*");

    // Find the first file in the directory.
    hFind = FindFirstFile(szDir, &ffd);
    if (INVALID_HANDLE_VALUE == hFind)
    {
        dwError = GetLastError();
        wprintf(L"\nError: FindFirstFile failed %d.\n", dwError);
        return -1;
    }

    // Iterate over all the files in the game collecting file size information
    do
    {
        //If is a directory drill into it
        if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
        {
            if (wcscmp(ffd.cFileName, L".") != 0 && wcscmp(ffd.cFileName, L"..") != 0) //There is probably a better way to do this...
            {
                size_t newDirLength = wcsnlen_s(ffd.cFileName, DIR_BUFFER_LENGTH);
                if (targetDirLength + 1 + newDirLength + 1 > DIR_BUFFER_LENGTH)
                {
                    wprintf(L"\nDirectory path is too long.\n");
                    return -1;
                }

                wcscpy_s(szDir, DIR_BUFFER_LENGTH, targetDir);
                wcscat_s(szDir, DIR_BUFFER_LENGTH, L"\\");
                wcscat_s(szDir, DIR_BUFFER_LENGTH, ffd.cFileName);
                UINT64 result = EstimateRequiredSpace(szDir);
                if (result == -1)
                {
                    return -1;
                }
                requiredBytes += result;
            }
        }
        else
        {
            filesize.LowPart = ffd.nFileSizeLow;
            filesize.HighPart = ffd.nFileSizeHigh;

            //Round up file sizes to the nearest 4K boundary to get a better estimate of the actual size of disk
            requiredBytes += ESTIMATED_FILE_SIZE_ON_DISK(filesize.QuadPart);
        }
    }
    while (FindNextFile(hFind, &ffd) != 0);

    dwError = GetLastError();
    if (dwError != ERROR_NO_MORE_FILES)
    {
        wprintf(L"\nError: FindNextFile failed %d.\n", dwError);
        return -1;
    }

    FindClose(hFind);
    return requiredBytes;
}  
```

<a id="requirementsSection" />

## 要件

**ヘッダー:** xtfapi.h

**ライブラリ:** XtfApi.lib

**サポートされているプラットフォーム:** Windows (XBOX 本体ツール用)

<a id="seealsoSection" />

## 関連項目

[XtfConsoleControl](/reference/tools/xtf/xtfconsolecontrol/xtfconsolecontrol_members)\
[XBOX Tools Framework ネイティブ API リファレンス](/reference/tools/xtf/atoc-xtf-native)


## Related topics

- [XtfGetCountofAppUserModelIds](/ja-jp/reference/tools/xtf/xtfapi/functions/xtfgetcountofappusermodelids-xtfapi-xbox-windows-m.md)
- [IXtfApplicationClient::GetDlc](/ja-jp/reference/tools/xtf/xtfapplication/classes/IXtfApplicationClient/methods/getdlc-ixtfapplicationclient-xtfapplication-xbox-microsoft-m.md)
- [IXtfApplicationClient::GetInstalled](/ja-jp/reference/tools/xtf/xtfapplication/classes/IXtfApplicationClient/methods/getinstalled-ixtfapplicationclient-xtfapplication-xbox-microsoft-m.md)
- [XPersistentLocalStorageSpaceInfo](/ja-jp/reference/system/xpersistentlocalstorage/structs/xpersistentlocalstoragespaceinfo.md)
- [XAppCaptureMetadataRemainingStorageBytesAvailable](/ja-jp/reference/system/xappcapture/functions/xappcapturemetadataremainingstoragebytesavailable.md)
