Skip to content

Commit

Permalink
Added more general filesystem helper functions.
Browse files Browse the repository at this point in the history
Added functions to create directories, create and remove both files and
directories, and get the status for a path for both file and resource
streams. This replaces the functions in FileUtils.h, which were only for
raw filesystem paths and incomplete.
  • Loading branch information
akb825 committed Jan 5, 2025
1 parent 6332ad0 commit d1bd73d
Show file tree
Hide file tree
Showing 10 changed files with 298 additions and 147 deletions.
41 changes: 38 additions & 3 deletions modules/Core/Core/include/DeepSea/Core/Streams/FileStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,42 @@ extern "C"
* @see dsFileStream
*/

/**
* @brief Gets the status of a file or directory on the filesystem.
* @remark errno will be set on failure.
* @param path The path to a file or directory.
* @return The status of the file.
*/
DS_CORE_EXPORT dsPathStatus dsFileStream_getPathStatus(const char* path);

/**
* @brief Creates a directory on the filesystem.
* @remark errno will be set on failure.
* @param path The path to the directory.
* @return False if the directory couldn't be created. If errno is EEXIST, the directory already
* existed.
*/
DS_CORE_EXPORT bool dsFileStream_createDirectory(const char* path);

/**
* @brief Removes a file from the filesystem.
* @remark errno will be set on failure.
* @param path The path to remove.
* @return False if the file couldn't be removed.
*/
DS_CORE_EXPORT bool dsFileStream_removeFile(const char* path);

/**
* @brief Removes a directory from the filesystem.
*
* The directory must be empty before removal.
*
* @remark errno will be set on failure.
* @param path The path to remove.
* @return False if the path couldn't be removed.
*/
DS_CORE_EXPORT bool dsFileStream_removeDirectory(const char* path);

/**
* @brief Starts iterating over a directory on the filesystem.
* @remark errno will be set on failure.
Expand Down Expand Up @@ -64,12 +100,11 @@ DS_CORE_EXPORT bool dsFileStream_closeDirectory(dsDirectoryIterator iterator);
* @brief Opens a file stream with a file path.
* @remark errno will be set on failure.
* @param stream The stream to open.
* @param filePath The file path to open.
* @param path The file path to open.
* @param mode The mode to open the file with. See fopen.
* @return False if the file couldn't be opened.
*/
DS_CORE_EXPORT bool dsFileStream_openPath(dsFileStream* stream, const char* filePath,
const char* mode);
DS_CORE_EXPORT bool dsFileStream_openPath(dsFileStream* stream, const char* path, const char* mode);

/**
* @brief Opens a file stream with a FILE pointer.
Expand Down
52 changes: 0 additions & 52 deletions modules/Core/Core/include/DeepSea/Core/Streams/FileUtils.h

This file was deleted.

41 changes: 41 additions & 0 deletions modules/Core/Core/include/DeepSea/Core/Streams/ResourceStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,47 @@ DS_CORE_EXPORT const char* dsResourceStream_getDirectory(dsFileResourceType type
DS_CORE_EXPORT bool dsResourceStream_getPath(char* outResult, size_t resultSize,
dsFileResourceType type, const char* path);

/**
* @brief Gets the status of a file or directory on a resource path.
* @remark errno will be set on failure.
* @param type The resource type.
* @param path The path to a file or directory.
* @return The status of the file.
*/
DS_CORE_EXPORT dsPathStatus dsResourceStream_getPathStatus(
dsFileResourceType type, const char* path);

/**
* @brief Creates a directory on a resource path.
* @remark errno will be set on failure.
* @param type The resource type.
* @param path The path to the directory.
* @return False if the directory couldn't be created. If errno is EEXIST, the directory already
* existed.
*/
DS_CORE_EXPORT bool dsResourceStream_createDirectory(dsFileResourceType type, const char* path);

/**
* @brief Removes a file from a resource path.
* @remark errno will be set on failure.
* @param type The resource type.
* @param path The path to remove.
* @return False if the file couldn't be removed.
*/
DS_CORE_EXPORT bool dsResourceStream_removeFile(dsFileResourceType type, const char* path);

/**
* @brief Removes a directory from a resource path.
*
* The directory must be empty before removal.
*
* @remark errno will be set on failure.
* @param type The resource type.
* @param path The path to remove.
* @return False if the path couldn't be removed.
*/
DS_CORE_EXPORT bool dsResourceStream_removeDirectory(dsFileResourceType type, const char* path);

/**
* @brief Starts iterating over a directory from a resource.
* @remark errno will be set on failure.
Expand Down
14 changes: 7 additions & 7 deletions modules/Core/Core/include/DeepSea/Core/Streams/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,16 @@ typedef enum dsFileResourceType
} dsFileResourceType;

/**
* @brief Enum to determine if a file exists, and if so, if it's a file or directory.
* @brief Enum to determine if a path exists, and if so, if it's a file or directory.
* @see FileUtils.h
*/
typedef enum dsFileStatus
typedef enum dsPathStatus
{
dsFileStatus_Error, ///< An error occurred in accessing the file.
dsFileStatus_DoesntExist, ///< File doesn't exist.
dsFileStatus_ExistsFile, ///< File exists as a file or file-like object.
dsFileStatus_ExistsDirectory ///< File exists as a directory.
} dsFileStatus;
dsPathStatus_Error, ///< An error occurred in accessing the path.
dsPathStatus_Missing, ///< Path doesn't exist.
dsPathStatus_ExistsFile, ///< Path exists as a file or file-like object.
dsPathStatus_ExistsDirectory ///< Path exists as a directory.
} dsPathStatus;

/**
* @brief Enum for the result of retrieving a directory entry.
Expand Down
73 changes: 69 additions & 4 deletions modules/Core/Core/src/Streams/FileStream.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,21 @@
#include <DeepSea/Core/Assert.h>
#include <DeepSea/Core/Error.h>

#include <sys/stat.h>
#include <string.h>

#if DS_WINDOWS
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>

#include <direct.h>
#include <io.h>
#include <malloc.h>
#include <stdio.h>
#define S_ISDIR(x) (((x) & S_IFMT) == S_IFDIR)
#else
#include <dirent.h>
#include <unistd.h>
#endif

#if DS_WINDOWS
Expand Down Expand Up @@ -75,6 +82,65 @@ static void initFileStream(dsFileStream* stream, FILE* file)
stream->file = file;
}

dsPathStatus dsFileStream_getPathStatus(const char* path)
{
if (!path || *path == 0)
{
errno = EINVAL;
return dsPathStatus_Error;
}

#if DS_WINDOWS
// Use stat64 in case stat would fail on a large file.
struct _stat64 info;
int result = _stat64(path, &info);
#else
struct stat info;
int result = stat(path, &info);
#endif

if (result == 0)
return S_ISDIR(info.st_mode) ? dsPathStatus_ExistsDirectory : dsPathStatus_ExistsFile;
return errno == ENOENT ? dsPathStatus_Missing : dsPathStatus_Error;
}

bool dsFileStream_createDirectory(const char* path)
{
if (!path || *path == 0)
{
errno = EINVAL;
return false;
}

#if DS_WINDOWS
return mkdir(path) == 0;
#else
return mkdir(path, 0755) == 0;
#endif
}

bool dsFileStream_removeFile(const char* path)
{
if (!path || *path == 0)
{
errno = EINVAL;
return false;
}

return unlink(path) == 0;
}

bool dsFileStream_removeDirectory(const char* path)
{
if (!path || *path == 0)
{
errno = EINVAL;
return false;
}

return rmdir(path) == 0;
}

dsDirectoryIterator dsFileStream_openDirectory(const char* path)
{
if (!path || *path == 0)
Expand Down Expand Up @@ -182,16 +248,15 @@ bool dsFileStream_closeDirectory(dsDirectoryIterator iterator)
#endif
}

bool dsFileStream_openPath(dsFileStream* stream, const char* filePath,
const char* mode)
bool dsFileStream_openPath(dsFileStream* stream, const char* path, const char* mode)
{
if (!stream || !filePath || !mode)
if (!stream || !path || *path == 0 || !mode)
{
errno = EINVAL;
return false;
}

FILE* file = fopen(filePath, mode);
FILE* file = fopen(path, mode);
if (!file)
return false;

Expand Down
44 changes: 0 additions & 44 deletions modules/Core/Core/src/Streams/FileUtils.c

This file was deleted.

Loading

0 comments on commit d1bd73d

Please sign in to comment.