Skip to content

Commit

Permalink
Added better file-util handling + Session update (#431)
Browse files Browse the repository at this point in the history
  • Loading branch information
DerjenigeUberMensch authored Sep 22, 2024
1 parent b397cb4 commit e33ea30
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 46 deletions.
10 changes: 9 additions & 1 deletion include/session.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
#include "settings.h"
#include "parser.h"

#define _WM_DIR_NAME_ "vox-wm"
#define _WM_DIR_NAME_ "vox-wm"
#define _WM_SESSION_FILE_NAME_ "session.cfg"


typedef struct SessionMonSaveID SessionMonSaveID;
Expand Down Expand Up @@ -123,4 +124,11 @@ SessionClientSave



void
SessionSave(
void
);



#endif
70 changes: 37 additions & 33 deletions src/session.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ extern WM _wm;


static int
__SESSION__CREATE__PATH(
SessionGetConfigPath
(
char *buff,
unsigned int buff_len,
unsigned int *len_return
Expand All @@ -28,6 +29,7 @@ __SESSION__CREATE__PATH(
/* \0 */
const int nullbytesize = sizeof(char);
const unsigned int WM_DIR_NAME_LENGTH = sizeof(_WM_DIR_NAME_) - 1;
const unsigned int WM_FILE_NAME_LENGTH = sizeof(_WM_SESSION_FILE_NAME_) - 1;

unsigned int len = 0;
unsigned int usablelen = 0;
Expand All @@ -45,54 +47,56 @@ __SESSION__CREATE__PATH(
{ return EXIT_FAILURE;
}

strncat(buff, _WM_DIR_NAME_, usablelen);
memcpy(buff + len, _WM_DIR_NAME_, WM_DIR_NAME_LENGTH);
usablelen -= WM_DIR_NAME_LENGTH;
len += WM_DIR_NAME_LENGTH;

status = FFCreateDir(buff);

if(status == EXIT_FAILURE)
if(usablelen - WM_FILE_NAME_LENGTH - sizeof('/') - sizeof('\0')< 0)
{ return EXIT_FAILURE;
}

/* get correct path ie: home/user/xxx/ */
buff[len] = '/';
++len;
--usablelen;
memcpy(buff + len, _WM_SESSION_FILE_NAME_, WM_FILE_NAME_LENGTH);
len += WM_FILE_NAME_LENGTH;
usablelen -= WM_FILE_NAME_LENGTH;
buff[len] = '\0';

*len_return = buff_len - usablelen;

return EXIT_SUCCESS;
}

static int
__SESSION__GET__PATH(
char *buff,
unsigned int buff_len,
unsigned int *len_return
)
{
return __SESSION__CREATE__PATH(buff, buff_len, len_return);
}

static const inline unsigned int
__GET__CONFIG__BUFF__SIZE(
void
)
{
/* *Most filenames can only have upto 255 characters */
const unsigned int MAX_FILENAME = 255;
/* Assuming /home/user/.config/mydir/dirname/filename
* x * 2 to allow for upto x2 layers of config directories (incase the user does some wacky stuff)
*/
const unsigned int PROBABLE_DEPTH = 6;
const unsigned int MAX_LAYERS = PROBABLE_DEPTH * 2;
const unsigned int ret = MAX_FILENAME * MAX_LAYERS * sizeof(char);
return ret;
}


void
SessionSave(
void
)
{
const unsigned int BUFF_SIZE = __GET__CONFIG__BUFF__SIZE();
const unsigned int BUFF_SIZE = FFSysGetConfigPathLengthMAX();

char buff[BUFF_SIZE];
unsigned int length = 0;
int status;

status = SessionGetConfigPath(buff, BUFF_SIZE, &length);

if(status == EXIT_FAILURE)
{
Debug0("Failed to save data FIXME");
return;
}
Debug("%s", buff);

status = FFCreateFile(buff);

if(status != EXIT_SUCCESS)
{
/* This should not occur, but may due to out of memory, low storage, etc... */
Debug0("Failed to create session save file.");
return;
}
}

int
Expand Down
56 changes: 44 additions & 12 deletions tools/file_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
#include <errno.h>




/*
*
* NOTE: Recomended buff length atleast 2550 bytes
Expand Down Expand Up @@ -48,21 +50,25 @@ FFGetSysConfigPath(
return EXIT_FAILURE;
}

/* pray to god the compiler inlines this */
static inline const unsigned int
FFSysGetConfigPathLengthMAX(
void
)
{

const unsigned int MAX_FILENAME = 255;
enum
{
MAX_FILENAME = 255,
#ifdef __linux__
/* Assuming /home/user/.config/mydir/dirname/filename
* x * 2 to allow for upto x2 layers of config directories for wacky stuff
*/
const unsigned int PROBABLE_DEPTH = 6;
const unsigned int MAX_LAYERS = PROBABLE_DEPTH * 2;
const unsigned int ret = MAX_FILENAME * MAX_LAYERS * sizeof(char);
/* Assuming /home/user/.config/mydir/dirname/filename
* x * 2 to allow for upto x2 layers of config directories for wacky stuff
*/
PROBABLE_DEPTH = 6,
#else
#error "Unknown operating system type"
#endif
ret = MAX_FILENAME * PROBABLE_DEPTH * sizeof(char),
};
return ret;
}
/*
Expand Down Expand Up @@ -117,14 +123,40 @@ FFCreateDir(
return EXIT_SUCCESS;
}

static int
FFCreatePath(
const char *const FULL_PATH
)
{
return FFCreateDir(FULL_PATH);
}

static int
FFPathExists(
const char *const FULL_PATH
)
{
return FFDirExists(FULL_PATH);
}


static int
FFCreateFile(
const char *const FILE_NAME
)
{
int ret = EXIT_SUCCESS;





if(!FFPathExists(FILE_NAME))
{
FILE *f = fopen(FILE_NAME, "ab+");
if(!f)
{ ret = EXIT_FAILURE;
}
fclose(f);
}
return ret;
}



Expand Down

0 comments on commit e33ea30

Please sign in to comment.