feat: collect portable project media with transactional cleanup and qualify composition workflows
This commit is contained in:
parent
d676d74a11
commit
b0cfa79131
15 changed files with 225 additions and 10 deletions
82
src/persistence/collect.c
Normal file
82
src/persistence/collect.c
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
#define _XOPEN_SOURCE 700
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#include "../project/project_internal.h"
|
||||
#include "../graph/graph_internal.h"
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#define COLLECT_ASSETS 128U
|
||||
#define FILE_LIMIT UINT64_C(268435456)
|
||||
#define TOTAL_LIMIT UINT64_C(1073741824)
|
||||
typedef struct {char source[4096],target[512];int owned;} media_entry;
|
||||
typedef struct {size_t count;uint64_t total;media_entry media[COLLECT_ASSETS];} collection;
|
||||
|
||||
static xtc_result reference(collection *c,char *path,int rewrite)
|
||||
{
|
||||
char canonical[4096];size_t i;
|
||||
if(!path[0])return XTC_OK;
|
||||
if(!realpath(path,canonical))return XTC_ERR_IO;
|
||||
for(i=0;i<c->count;++i)if(!strcmp(c->media[i].source,canonical))break;
|
||||
if(rewrite){if(i==c->count)return XTC_ERR_IO;strcpy(path,c->media[i].target);return XTC_OK;}
|
||||
if(i<c->count)return XTC_OK;
|
||||
if(c->count==COLLECT_ASSETS)return XTC_ERR_CAPACITY;
|
||||
strcpy(c->media[c->count++].source,canonical);return XTC_OK;
|
||||
}
|
||||
static xtc_result references(collection *c,xtc_project *p,int rewrite)
|
||||
{
|
||||
size_t i;xtc_result r=XTC_OK;
|
||||
for(i=0;r==XTC_OK && i<p->clip_count;++i)r=reference(c,p->clips[i].asset_path,rewrite);
|
||||
for(i=0;r==XTC_OK && i<p->track_count;++i)r=reference(c,p->instruments[i].sample_path,rewrite);
|
||||
for(i=0;r==XTC_OK && i<p->graph->node_count;++i){
|
||||
if(p->graph->nodes[i].type==XTC_NODE_NATIVE)return XTC_ERR_UNSUPPORTED;
|
||||
if(p->graph->nodes[i].type==XTC_NODE_PLUGIN)r=reference(c,p->graph->nodes[i].plugin.sample_path,rewrite);
|
||||
}
|
||||
return r;
|
||||
}
|
||||
static xtc_result copy_media(collection *c,media_entry *entry)
|
||||
{
|
||||
int input=-1,output=-1;struct stat st;unsigned char buffer[65536];uint64_t left;xtc_result r=XTC_ERR_IO;
|
||||
input=open(entry->source,O_RDONLY|O_CLOEXEC|O_NOFOLLOW|O_NONBLOCK);if(input<0)return XTC_ERR_IO;
|
||||
if(fstat(input,&st) || !S_ISREG(st.st_mode))goto done;
|
||||
if(st.st_size<0 || (uint64_t)st.st_size>FILE_LIMIT || c->total>TOTAL_LIMIT-(uint64_t)st.st_size){r=XTC_ERR_CAPACITY;goto done;}
|
||||
left=(uint64_t)st.st_size;
|
||||
output=open(entry->target,O_WRONLY|O_CREAT|O_EXCL|O_CLOEXEC,0600);if(output<0)goto done;
|
||||
entry->owned=1;
|
||||
while(left) {
|
||||
size_t want=left>sizeof(buffer) ? sizeof(buffer) : (size_t)left,used=0;ssize_t got;
|
||||
do {got=read(input,buffer,want);}while(got<0 && errno==EINTR);
|
||||
if(got<=0)goto done;
|
||||
while(used<(size_t)got){ssize_t n;do {n=write(output,buffer+used,(size_t)got-used);}while(n<0 && errno==EINTR);if(n<=0)goto done;used+=(size_t)n;}
|
||||
left-=(uint64_t)got;
|
||||
}
|
||||
{ssize_t n;do {n=read(input,buffer,1);}while(n<0 && errno==EINTR);if(n!=0)goto done;}
|
||||
if(fsync(output))goto done;
|
||||
c->total+=(uint64_t)st.st_size;r=XTC_OK;
|
||||
done:
|
||||
if(input>=0 && close(input) && r==XTC_OK)r=XTC_ERR_IO;
|
||||
if(output>=0 && close(output) && r==XTC_OK)r=XTC_ERR_IO;
|
||||
return r;
|
||||
}
|
||||
xtc_result xtc_project_collect(const xtc_project *p,const char *directory)
|
||||
{
|
||||
xtc_project *copy=NULL;collection *c=NULL;char canonical[4096],root[480],project[512]={0};size_t i;int created=0;xtc_result r;
|
||||
if(!p || !directory || directory[0]!='/' || !directory[1] || strnlen(directory,480)>=480)return XTC_ERR_INVALID_ARGUMENT;
|
||||
r=xtc_project_validate(p);if(r!=XTC_OK)return r;
|
||||
r=xtc_project_clone(p,©);if(r!=XTC_OK)return r;
|
||||
c=calloc(1,sizeof(*c));if(!c){xtc_project_destroy(copy);return XTC_ERR_OUT_OF_MEMORY;}
|
||||
r=references(c,copy,0);if(r!=XTC_OK)goto done;
|
||||
if(mkdir(directory,0700)){r=XTC_ERR_IO;goto done;}created=1;
|
||||
if(!realpath(directory,canonical)){r=XTC_ERR_IO;goto done;}
|
||||
if(strlen(canonical)>=sizeof(root)){r=XTC_ERR_CAPACITY;goto done;}strcpy(root,canonical);
|
||||
for(i=0;r==XTC_OK && i<c->count;++i){snprintf(c->media[i].target,sizeof(c->media[i].target),"%s/media-%03zu.wav",root,i);r=copy_media(c,c->media+i);}
|
||||
if(r==XTC_OK)r=references(c,copy,1);
|
||||
if(r==XTC_OK){snprintf(project,sizeof(project),"%s/project.xtc",root);r=xtc_project_save(copy,project);}
|
||||
done:
|
||||
if(r!=XTC_OK && created){if(project[0])unlink(project);for(i=0;i<c->count;++i)if(c->media[i].owned)unlink(c->media[i].target);rmdir(directory);}
|
||||
free(c);xtc_project_destroy(copy);return r;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue