1
0
Fork 0
forked from hanez/xtc

Phase 18: preserve portable project media paths and verify format migrations

This commit is contained in:
Johannes Findeisen 2026-10-27 06:31:59 +01:00
commit 1369e4d463
15 changed files with 329 additions and 13 deletions

View file

@ -1,3 +1,4 @@
#define _XOPEN_SOURCE 700
#define _POSIX_C_SOURCE 200809L
#include "../project/project_internal.h"
#include "../graph/graph_internal.h"
@ -10,6 +11,7 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
typedef struct {
FILE *file;
@ -184,14 +186,14 @@ static int decode_float(const char *text, float *value)
* @param project Project model; borrowed for this call.
* @return XTC_OK on success, or an error code describing the failure.
*/
static xtc_result read_project(reader *input, xtc_project *project)
static xtc_result read_project(reader *input, xtc_project *project, const char *directory)
{
uint64_t version, value, next_id, node_count, edge_count, format_version;
size_t i, j;
xtc_graph *graph = project->graph;
READ("XTC_PROJECT", 2U);
NUM(1U, version);
if (version < 1U || version > 6U)
if (version < 1U || version > 7U)
return XTC_ERR_UNSUPPORTED;
format_version = version;
READ("APP", 4U);
@ -402,6 +404,15 @@ static xtc_result read_project(reader *input, xtc_project *project)
return XTC_ERR_FORMAT;
if (ferror(input->file))
return XTC_ERR_IO;
if (format_version >= 7) for (i = 0; i < project->clip_count; ++i) {
char *asset = project->clips[i].asset_path;
if (asset[0] && asset[0] != '/') {
size_t root = strlen(directory), length = strlen(asset);
if (root+length+2 > XTC_ASSET_PATH_BYTES) return XTC_ERR_CAPACITY;
memmove(asset+root+1,asset,length+1);
memcpy(asset,directory,root); asset[root] = '/';
}
}
return xtc_project_validate(project);
}
@ -428,7 +439,7 @@ static int write_name(FILE *file, const char *name)
* @param project Project model; borrowed for this call.
* @return XTC_OK on success, or an error code describing the failure.
*/
static xtc_result write_project(FILE *file, const xtc_project *project)
static xtc_result write_project(FILE *file, const xtc_project *project, const char *directory)
{
const xtc_graph *graph = project->graph;
size_t i;
@ -437,7 +448,7 @@ static xtc_result write_project(FILE *file, const xtc_project *project)
return XTC_ERR_UNSUPPORTED;
if (sizeof(float) != sizeof(uint32_t) || FLT_RADIX != 2 || FLT_MANT_DIG != 24 || FLT_MAX_EXP != 128)
return XTC_ERR_UNSUPPORTED;
if (fprintf(file, "XTC_PROJECT 6\nAPP %d %d %d\nTEMPO %u\nIDS %" PRIu64 " %" PRIu64 "\nGRAPH %d %zu %zu %zu\n",
if (fprintf(file, "XTC_PROJECT 7\nAPP %d %d %d\nTEMPO %u\nIDS %" PRIu64 " %" PRIu64 "\nGRAPH %d %zu %zu %zu\n",
XTC_VERSION_MAJOR, XTC_VERSION_MINOR, XTC_VERSION_PATCH, project->tempo, project->next_track_id,
project->next_clip_id, graph->require_output_nodes, graph->next_id, graph->node_count,
graph->edge_count) < 0)
@ -493,7 +504,10 @@ static xtc_result write_project(FILE *file, const xtc_project *project)
if (fputs("ASSET ", file) == EOF)
return XTC_ERR_IO;
if (clip->asset_path[0]) {
if (!write_name(file, clip->asset_path))
const char *asset = clip->asset_path;
size_t root = strlen(directory);
if (!strncmp(asset,directory,root) && asset[root] == '/' && asset[root+1]) asset += root+1;
if (!write_name(file, asset))
return XTC_ERR_IO;
} else if (fputs("-\n", file) == EOF)
return XTC_ERR_IO;
@ -537,6 +551,19 @@ static size_t path_length(const char *path)
return size == 4096U ? 0U : size;
}
/* Resolve only the containing folder: media need not exist. Save replaces the
* named entry (including a symlink), so do not resolve the final component. */
static char *project_folder(const char *path)
{
char *copy = malloc(strlen(path)+2), *slash, *folder;
if (!copy) return NULL;
strcpy(copy,path); slash = strrchr(copy,'/');
if (!slash) strcpy(copy,".");
else if (slash == copy) slash[1] = 0;
else *slash = 0;
folder = realpath(copy,NULL); free(copy); return folder;
}
/**
* @brief Loads and validates a project before transactionally replacing the current state.
*
@ -548,15 +575,19 @@ xtc_result xtc_project_load(xtc_project *project, const char *path)
{
xtc_project *candidate = NULL;
reader input = {0};
char *directory;
xtc_result result;
if (project == NULL || path_length(path) == 0U)
return XTC_ERR_INVALID_ARGUMENT;
input.file = fopen(path, "rb");
if (input.file == NULL)
return XTC_ERR_IO;
directory = project_folder(path);
if (!directory) { fclose(input.file); return XTC_ERR_IO; }
result = xtc_project_create(&candidate);
if (result == XTC_OK)
result = read_project(&input, candidate);
result = read_project(&input, candidate, directory);
free(directory);
if (fclose(input.file) != 0 && result == XTC_OK)
result = XTC_ERR_IO;
if (result == XTC_OK) {
@ -578,7 +609,7 @@ xtc_result xtc_project_load(xtc_project *project, const char *path)
xtc_result xtc_project_save(const xtc_project *project, const char *path)
{
size_t length = path_length(path);
char *temporary;
char *temporary, *directory;
FILE *file;
int descriptor;
xtc_result result;
@ -604,7 +635,8 @@ xtc_result xtc_project_save(const xtc_project *project, const char *path)
free(temporary);
return XTC_ERR_IO;
}
result = write_project(file, project);
directory = project_folder(path);
result = directory ? write_project(file, project, directory) : XTC_ERR_IO;
if (result == XTC_OK && fflush(file) != 0)
result = XTC_ERR_IO;
if (result == XTC_OK && fsync(descriptor) != 0)
@ -615,6 +647,12 @@ xtc_result xtc_project_save(const xtc_project *project, const char *path)
result = XTC_ERR_IO;
if (result != XTC_OK)
(void)unlink(temporary);
if (result == XTC_OK) {
int folder = open(directory,O_RDONLY);
if (folder < 0) result = XTC_ERR_IO;
else { if (fsync(folder)) result = XTC_ERR_IO; if (close(folder)) result = XTC_ERR_IO; }
}
free(directory);
free(temporary);
return result;
}