# FS_HasEnoughSpaceToCreateFile

## Syntax

    #include <nitro/fs.h>

    BOOL FS_HasEnoughSpaceToCreateFile(FSArchiveResource *resource, const char *path, u32 size);

## Arguments

`resource` | Archive information that was obtained beforehand using the `FS_GetArchiveResource` function.
`path` | Pathname of the file you want to create. If a directory is to be created, you must append a forward slash (/) to the end of the entry. If you specify an empty string, it is taken to mean that the file already exists, and the pathname will be omitted.
`size` | Size of the file to create. Specify 0 when creating a directory.

## Return Values

Returns `TRUE` if the file with the specified path and size can be created with certainty, and `FALSE` otherwise.

## Description

Calculates a theoretical value to determine whether a file of the specified pathname and size can be created in the space available.
This function calculates the theoretically required size, taking into account the number of clusters consumed in media that contain FAT structures, such as NAND archives and SD Card archives. In making the calculation, this function always assumes that the directory levels in the specified path consume one cluster each, regardless of whether directories at the levels actually exist.

The purpose of this function is to quickly determine whether a file can definitely be created, without taking the time to check the actual internal status of the drive. Therefore, there will be times when it would be possible to create the file, but the function returns FALSE because the calculation did not yield 100% certainty. If you require an exact determination and time is not a factor, use this function in combination with the `FS_GetPathInfo` function and omit the process of determining the size of existing directories.

To determine the Save Data size of a NAND application, also see the information about the Save Data Size List.

The archive information that is passed to this function as an argument must have been obtained in advance by calling the `FS_GetArchiveResource` function. If this function returns `TRUE`, the disk capacity that is expected based on the path and file size is reflected in the archive information, and the `availableSize` and `availableClusters` members are reduced. By reusing this archive information and repeatedly calling this function, you can determine whether an entire batch of multiple files can be created. An example of this usage is shown below.

`
// First, get the current archive information.
FSArchiveResource resource[1];
if (FS_GetArchiveResource("sdmc:/", resource))
{
// Individually determine pathname and size of files you want to create.
BOOL enough = TRUE;
enough &= FS_HasEnoughSpaceToCreateFile(resource, "sdmc:/foo/file1.txt", 1000);
enough &= FS_HasEnoughSpaceToCreateFile(resource, "sdmc:/bar/file2.txt", 10000);
if (enough)
{
// If completely successful, there is enough space to create all of the specified files.
}
}
return retval;
`

This function immediately calculates the size that would logically be sufficient, without actually accessing the archive. As a result, if a path that actually indicates the same directory is specified several times, the size required to create that directory is counted multiple times. Directories consume a single cluster of capacity for each level, so the duplicated size will be extremely large if a deep path is specified. To avoid this kind of error, the common path component should be collectively called only once. An example of this usage is shown below.

`
// Specifying "sdmc:/foo/bar", as follows, causes the size required to create it to be counted several times.
// enough &= FS_HasEnoughSpaceToCreateFile(resource, "sdmc:/foo/bar/file1.txt", 1000);
// enough &= FS_HasEnoughSpaceToCreateFile(resource, "sdmc:/foo/bar/file2.txt", 10000);
// enough &= FS_HasEnoughSpaceToCreateFile(resource, "sdmc:/foo/bar/file3.txt", 10);
// To avoid duplicate size calculations, first test the common directory once.
enough &= FS_HasEnoughSpaceToCreateFile(resource, "sdmc:/foo/bar/", 0);
// The rest can be omitted, assuming sdmc:/foo/bar/ exists.
enough &= FS_HasEnoughSpaceToCreateFile(resource, "file1.txt", 1000);
enough &= FS_HasEnoughSpaceToCreateFile(resource, "file2.txt", 10000);
enough &= FS_HasEnoughSpaceToCreateFile(resource, "file3.txt", 10);
`

## See Also

Save Data Size List
`FSArchiveResource`
`FS_GetArchiveResource`

## Revision History

2009/02/12 Slightly adjusted the layout.
2009/01/13 Added supplemental information to Description. Added a link to the Save Data Size List.
2008/12/12 Explained that the returned value is not an exact result and gave an example of an alternate method.
2008/09/16 Added explanations and examples for avoiding duplicate calculations of the consumed size.
2008/02/21 Changed the specifications so that preparations will be made by the caller of `FSArchiveResource`.
2008/01/24 Initial version.

CONFIDENTIAL