feat: collect portable project media with transactional cleanup and qualify composition workflows

This commit is contained in:
Johannes Findeisen 2026-11-06 00:49:37 +01:00
commit b0cfa79131
15 changed files with 225 additions and 10 deletions

View file

@ -140,6 +140,7 @@ MainWindow::MainWindow(bool prompt_unsaved,const char *configuration)
menu->add("View/Previous marker",0,[](Fl_Widget *,void *c){auto *w=static_cast<MainWindow *>(c);auto r=w->navigate_marker(-1);if(r!=XTC_OK)w->report_error("Marker navigation failed",r);},this);
menu->add("View/Next marker",0,[](Fl_Widget *,void *c){auto *w=static_cast<MainWindow *>(c);auto r=w->navigate_marker(1);if(r!=XTC_OK)w->report_error("Marker navigation failed",r);},this);
menu->add("View/Loop current section",0,[](Fl_Widget *,void *c){auto *w=static_cast<MainWindow *>(c);auto r=w->loop_section();if(r!=XTC_OK)w->report_error("Section loop unchanged",r);},this);
menu->add("File/Collect project media...",0,[](Fl_Widget *,void *c){auto *w=static_cast<MainWindow *>(c);const char *path=fl_input("New absolute project folder (must not exist)","");if(path){auto r=w->collect_project(path);if(r!=XTC_OK)w->report_error("Project collection failed",r);}},this);
menu->add("View/Commands",FL_CTRL+'p',[](Fl_Widget *,void *c){static_cast<MainWindow *>(c)->show_commands();},this);
menu->add("View/Fit song",FL_CTRL+'0',[](Fl_Widget *,void *c){static_cast<MainWindow *>(c)->fit_song();},this);
menu->add("View/Piano roll", FL_CTRL + '4', piano_callback, this);
@ -2587,6 +2588,12 @@ xtc_result MainWindow::loop_section()
uint64_t begin=0,end=0;xtc_result r=xtc_project_section(project_.get(),sequencer_->position(),&begin,&end);
return r==XTC_OK ? set_loop(begin,end,true) : r;
}
xtc_result MainWindow::collect_project(const char *directory)
{
if(recorder_)return XTC_ERR_INVALID_STATE;
BusyOperation busy(*this,status_,"Collecting project media…");
return xtc_project_collect(project_.get(),directory);
}
void MainWindow::show_commands()
{
std::vector<std::string> commands;

View file

@ -35,6 +35,7 @@ class MainWindow : public Fl_Double_Window {
* the current document. No engine/model changes happen in audio callbacks. */
xtc_result create_track(bool midi);
xtc_result set_tempo(unsigned int milli_bpm);
xtc_result collect_project(const char *directory);
xtc_result navigate_marker(int direction);
xtc_result loop_section();
void show_commands();

View file

@ -1,5 +1,4 @@
#include <cmath>
#include <sstream>
#include "sequencer_widget.hpp"
#include "theme.hpp"
#include "snap.hpp"
@ -107,6 +106,12 @@ void SequencerWidget::extent()
xtc_project_get_clip(project_, i, &c);
end = std::max(end, c.start_tick + c.length_tick + 3840U);
}
size_t markers=0;
if(xtc_project_marker_count(project_,&markers)==XTC_OK && markers) {
xtc_marker marker{};
if(xtc_project_get_marker(project_,markers-1,&marker)==XTC_OK)
end=std::max(end,std::min(XTC_PROJECT_MAX_TICK,marker.tick)+UINT64_C(3840));
}
size(static_cast<int>(end * static_cast<unsigned int>(scale_) / 960U + static_cast<unsigned int>(header_width_+20)),
std::max(400, static_cast<int>(tracks) * 56 + 100));
refresh_instruments();

82
src/persistence/collect.c Normal file
View 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,&copy);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;
}