Skip to content

Commit

Permalink
possible fix for random directories in library (and everywhere)
Browse files Browse the repository at this point in the history
  • Loading branch information
SciLor committed May 7, 2024
1 parent 50c16e0 commit f8b8d90
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 13 deletions.
4 changes: 3 additions & 1 deletion include/contentJson.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ typedef struct
bool_t _updated;

bool_t _valid;
bool_t _create_if_missing;

} contentJson_t;

typedef struct
{
char *contentPath;
char *jsonPath;
bool_t exists;
bool_t valid;
bool_t updated;
Expand All @@ -56,6 +58,6 @@ typedef struct

error_t load_content_json(const char *content_path, contentJson_t *content_json, bool create_if_missing);
error_t load_content_json_settings(const char *content_path, contentJson_t *content_json, bool create_if_missing, settings_t *settings);
error_t save_content_json(const char *content_path, contentJson_t *content_json);
error_t save_content_json(const char *json_path, contentJson_t *content_json);
void content_json_update_model(contentJson_t *content_json, uint32_t audio_id, uint8_t *hash);
void free_content_json(contentJson_t *content_json);
3 changes: 2 additions & 1 deletion include/fs_ext.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ FsFile *fsOpenFileEx(const char_t *path, char *mode);
error_t fsCompareFiles(const char_t *source_path, const char_t *target_path, size_t *diff_position);
error_t fsCopyFile(const char_t *source_path, const char_t *target_path, bool_t overwrite);
error_t fsMoveFile(const char_t *source_path, const char_t *target_path, bool_t overwrite);
error_t fsCreateDirEx(const char_t *path, bool_t recursive);
error_t fsCreateDirEx(const char_t *path, bool_t recursive);
error_t fsRemoveFilename(char *dir);
18 changes: 11 additions & 7 deletions src/contentJson.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ error_t load_content_json_settings(const char *content_path, contentJson_t *cont
content_json->_has_cloud_auth = false;
content_json->tonie_model = NULL;
content_json->_valid = false;
content_json->_create_if_missing = create_if_missing;

osMemset(&content_json->_tap, 0, sizeof(tonie_audio_playlist_t));

if (fsFileExists(jsonPath))
Expand Down Expand Up @@ -147,7 +149,7 @@ error_t load_content_json_settings(const char *content_path, contentJson_t *cont

if (error != NO_ERROR && (error != ERROR_FILE_NOT_FOUND || create_if_missing))
{
error = save_content_json(content_path, content_json);
error = save_content_json(jsonPath, content_json);
if (error == NO_ERROR)
{
load_content_json_settings(content_path, content_json, true, settings);
Expand All @@ -159,10 +161,12 @@ error_t load_content_json_settings(const char *content_path, contentJson_t *cont
return error;
}

error_t save_content_json(const char *content_path, contentJson_t *content_json)
error_t save_content_json(const char *json_path, contentJson_t *content_json)
{
char *jsonPath = custom_asprintf("%s.json", content_path);
char *jsonPathTmp = custom_asprintf("%s.json.tmp", content_path);
char *content_path = osAllocMem(osStrlen(json_path) - 5 + 1);
osStrncpy(content_path, json_path, osStrlen(json_path) - 5);

char *jsonPathTmp = custom_asprintf("%s.tmp", json_path);
error_t error = NO_ERROR;
cJSON *contentJson = cJSON_CreateObject();

Expand All @@ -180,7 +184,7 @@ error_t save_content_json(const char *content_path, contentJson_t *content_json)
char *jsonRaw = cJSON_Print(contentJson);

char *dir = strdup(content_path);
dir[osStrlen(dir) - 8] = '\0';
fsRemoveFilename(dir);
if (!fsDirExists(dir))
{
fsCreateDir(dir);
Expand All @@ -200,7 +204,7 @@ error_t save_content_json(const char *content_path, contentJson_t *content_json)

if (error == NO_ERROR)
{
error = fsMoveFile(jsonPathTmp, jsonPath, true);
error = fsMoveFile(jsonPathTmp, json_path, true);
}

if (error == NO_ERROR)
Expand All @@ -211,7 +215,7 @@ error_t save_content_json(const char *content_path, contentJson_t *content_json)

cJSON_Delete(contentJson);
osFreeMem(jsonRaw);
osFreeMem(jsonPath);
osFreeMem(content_path);
osFreeMem(jsonPathTmp);
return error;
}
Expand Down
19 changes: 19 additions & 0 deletions src/fs_ext.c
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,23 @@ error_t fsCreateDirEx(const char_t *path, bool_t recursive)
free(path_copy);
}
return fsCreateDir(path);
}

error_t fsRemoveFilename(char *dir)
{
if (dir == NULL)
{
return ERROR_INVALID_PARAMETER;
}
if (dir[osStrlen(dir) - 1] == PATH_SEPARATOR)
{
return NO_ERROR;
}
char *last_slash = strrchr(dir, PATH_SEPARATOR);
if (last_slash == NULL)
{
return ERROR_INVALID_PARAMETER;
}
*last_slash = '\0';
return NO_ERROR;
}
12 changes: 9 additions & 3 deletions src/handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,7 @@ tonie_info_t *getTonieInfo(const char *contentPath, settings_t *settings)
tonieInfo->updated = false;
tonieInfo->tafHeader = NULL;
tonieInfo->contentPath = strdup(contentPath);
tonieInfo->jsonPath = custom_asprintf("%s.json", contentPath);
tonieInfo->exists = false;
osMemset(&tonieInfo->json, 0, sizeof(contentJson_t));

Expand Down Expand Up @@ -585,9 +586,9 @@ tonie_info_t *getTonieInfo(const char *contentPath, settings_t *settings)

void freeTonieInfo(tonie_info_t *tonieInfo)
{
if (tonieInfo->json._updated)
if (tonieInfo->json._updated && tonieInfo->json._create_if_missing)
{
save_content_json(tonieInfo->contentPath, &tonieInfo->json);
save_content_json(tonieInfo->jsonPath, &tonieInfo->json);
}

if (tonieInfo->tafHeader)
Expand All @@ -600,6 +601,11 @@ void freeTonieInfo(tonie_info_t *tonieInfo)
osFreeMem(tonieInfo->contentPath);
tonieInfo->contentPath = NULL;
}
if (tonieInfo->jsonPath)
{
osFreeMem(tonieInfo->jsonPath);
tonieInfo->jsonPath = NULL;
}

if (tonieInfo->valid)
{
Expand Down Expand Up @@ -773,7 +779,7 @@ error_t moveTAF2Lib(tonie_info_t *tonieInfo, settings_t *settings, bool_t rootDi
free(tonieInfo->json.source);
tonieInfo->json.source = strdup(libraryShortPath);

save_content_json(tonieInfo->contentPath, &tonieInfo->json);
save_content_json(tonieInfo->jsonPath, &tonieInfo->json);
TRACE_INFO(">> Successfully set to library %s\r\n", libraryShortPath);
}
else
Expand Down
4 changes: 3 additions & 1 deletion src/handler_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -2200,7 +2200,9 @@ error_t handleApiContentJsonSet(HttpConnection *connection, const char_t *uri, c

if (updated)
{
error = save_content_json(contentPath, &content_json);
char *json_path = custom_asprintf("%s.json", contentPath);
error = save_content_json(json_path, &content_json);
osFreeMem(json_path);
if (error != NO_ERROR)
{
return error;
Expand Down

0 comments on commit f8b8d90

Please sign in to comment.