Skip to content

Commit

Permalink
os: add create_temp_dir() (creates a unique ephemeral directory sui…
Browse files Browse the repository at this point in the history
…table for storing temporary files)
  • Loading branch information
Wertzui123 committed Nov 14, 2024
1 parent 8175429 commit d89ea9a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
6 changes: 6 additions & 0 deletions stdlib/os/fs.aspl
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,10 @@ function chdir(string path){
[public]
function chmod(string path, int mode){
implement("os.change_mode", path, mode)
}

// create_temp_dir creates and returns a unique ephemeral directory suitable for storing temporary files
[public]
function create_temp_dir() returns string{
return string(implement("os.create_temporary_directory"))
}
34 changes: 34 additions & 0 deletions stdlib/os/implementations/implementations.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <limits.h>
#include <sys/stat.h>
#include <sys/utsname.h>
#include <stdlib.h>
#endif

ASPL_OBJECT_TYPE ASPL_IMPLEMENT_os$get_current_program_arguments()
Expand Down Expand Up @@ -123,6 +124,39 @@ ASPL_OBJECT_TYPE ASPL_IMPLEMENT_os$change_mode(ASPL_OBJECT_TYPE* path, ASPL_OBJE
return ASPL_UNINITIALIZED;
}

ASPL_OBJECT_TYPE ASPL_IMPLEMENT_os$create_temporary_directory()
{
char* tempDirPath;
#ifdef _WIN32
char tempPath[MAX_PATH];
if (GetTempPath(MAX_PATH, tempPath) == 0)
{
ASPL_PANIC("Failed to retrieve temporary path.");
}

tempDirPath = ASPL_MALLOC(MAX_PATH);
if (GetTempFileName(tempPath, "", 0, tempDirPath) == 0)
{
ASPL_PANIC("Failed to create temporary directory name.");
}

DeleteFile(tempDirPath);
if (!CreateDirectory(tempDirPath, NULL))
{
ASPL_PANIC("Failed to create temporary directory.");
}
#else
tempDirPath = ASPL_MALLOC(PATH_MAX);
snprintf(tempDirPath, PATH_MAX, "/tmp/XXXXXX");

if (mkdtemp(tempDirPath) == NULL)
{
ASPL_PANIC("Failed to create temporary directory.");
}
#endif
return ASPL_STRING_LITERAL_NO_COPY(tempDirPath);
}

ASPL_OBJECT_TYPE ASPL_IMPLEMENT_os$get_current_runtime_os_name()
{
// TODO
Expand Down

0 comments on commit d89ea9a

Please sign in to comment.