diff --git a/include/session.h b/include/session.h index 3b50948..e5a41f1 100644 --- a/include/session.h +++ b/include/session.h @@ -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; @@ -123,4 +124,11 @@ SessionClientSave +void +SessionSave( + void + ); + + + #endif diff --git a/src/session.c b/src/session.c index 084cbe2..8741c43 100644 --- a/src/session.c +++ b/src/session.c @@ -19,7 +19,8 @@ extern WM _wm; static int -__SESSION__CREATE__PATH( +SessionGetConfigPath +( char *buff, unsigned int buff_len, unsigned int *len_return @@ -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; @@ -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 diff --git a/tools/file_util.h b/tools/file_util.h index df9e216..15b83aa 100644 --- a/tools/file_util.h +++ b/tools/file_util.h @@ -8,6 +8,8 @@ #include + + /* * * NOTE: Recomended buff length atleast 2550 bytes @@ -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; } /* @@ -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; +}