#10 Process specific backup log files

A process will open outbuf/inbuf.pid.log and move it to the outbuf/inbuf.log upon close, allowing for multiple processes to be open at once
This commit is contained in:
Aaron Cook 2023-05-08 10:00:37 -05:00
commit 1989c36886
5 changed files with 96 additions and 21 deletions

View file

@ -414,10 +414,14 @@ read_state()
{
struct stat st;
char *buf;
char *cwd = getcwd(NULL, 0);
char *cwd;
char *home = getenv("HOME");
FILE *fd;
if (!home)
return 0;
cwd = getcwd(NULL, 0);
chdir(home);
fd = fopen(".config/bytenuts/inbuf.log", "r");

View file

@ -25,7 +25,6 @@ int
cheerios_start(bytenuts_t *bytenuts)
{
char *home = getenv("HOME");
char *cwd;
memset(&cheerios, 0, sizeof(cheerios_t));
@ -43,11 +42,25 @@ cheerios_start(bytenuts_t *bytenuts)
}
}
cwd = getcwd(NULL, 0);
chdir(home);
cheerios.backup = fopen(".config/bytenuts/outbuf.log", "w");
chdir(cwd);
free(cwd);
if (home) {
pid_t pid;
int name_len;
/* ensure that a process has a unique log */
pid = getpid();
name_len = snprintf(
NULL, 0,
"%s/.config/bytenuts/outbuf.%d.log",
home, pid
);
cheerios.backup_filename = calloc(1, name_len + 1);
snprintf(
cheerios.backup_filename, name_len + 1,
"%s/.config/bytenuts/outbuf.%d.log",
home, pid
);
cheerios.backup = fopen(cheerios.backup_filename, "w");
}
pthread_cond_init(&cheerios.cond, NULL);
pthread_mutex_init(&cheerios.lock, NULL);
@ -138,7 +151,6 @@ cheerios_stop()
{
cheerios.running = 0;
pthread_join(cheerios.thr, NULL);
return 0;
}
@ -338,8 +350,33 @@ cheerios_thread(void *arg)
if (cheerios.log)
fclose(cheerios.log);
if (cheerios.backup)
if (cheerios.backup) {
char *out_filename;
int out_filename_len;
char *home = getenv("HOME");
/* do not chdir as other threads can still be running */
out_filename_len = snprintf(
NULL, 0,
"%s/.config/bytenuts/outbuf.log",
home
);
out_filename = calloc(1, out_filename_len + 1);
snprintf(
out_filename, out_filename_len + 1,
"%s/.config/bytenuts/outbuf.log",
home
);
/* move this processes log to the path that can be loaded on resumption */
fclose(cheerios.backup);
rename(cheerios.backup_filename, out_filename);
free(out_filename);
free(cheerios.backup_filename);
}
pthread_exit(NULL);
return NULL;
}

View file

@ -35,8 +35,9 @@ typedef struct cheerios_struct {
pthread_mutex_t *term_lock;
int ser_fd;
line_buffer_t lines;
FILE *log;
FILE *backup;
FILE *log; /* log file which was opened with -l */
char *backup_filename; /* realpath to the backup outbuf.pid.log */
FILE *backup; /* backup log file object */
bytenuts_config_t *config;
volatile int mode;
pthread_cond_t cond;

View file

@ -40,19 +40,29 @@ ingest_start(bytenuts_t *bytenuts)
HOME = getenv("HOME");
if (HOME) {
char *cwd = getcwd(NULL, 0);
int idx = 0;
chdir(HOME);
int name_len;
pid_t pid;
while (!read_cmd_page(idx)) {
idx++;
}
ingest.history_fd = fopen(".config/bytenuts/inbuf.log", "w");
/* ensure that a process has a unique log */
pid = getpid();
name_len = snprintf(
NULL, 0,
"%s/.config/bytenuts/inbuf.%d.log",
HOME, pid
);
ingest.history_filename = calloc(1, name_len + 1);
snprintf(
ingest.history_filename, name_len + 1,
"%s/.config/bytenuts/inbuf.%d.log",
HOME, pid
);
chdir(cwd);
free(cwd);
ingest.history_fd = fopen(ingest.history_filename, "w");
}
if (ingest.cmd_pgs_n > 0) {
@ -73,10 +83,6 @@ ingest_stop()
{
ingest.running = 0;
pthread_join(ingest.thr, NULL);
if (ingest.history_fd)
fclose(ingest.history_fd);
return 0;
}
@ -419,6 +425,32 @@ ingest_thread(void *arg)
sched_yield();
}
if (ingest.history_fd) {
char *in_filename;
int in_filename_len;
char *home = getenv("HOME");
/* do not chdir as other threads can still be running */
in_filename_len = snprintf(
NULL, 0,
"%s/.config/bytenuts/inbuf.log",
home
);
in_filename = calloc(1, in_filename_len + 1);
snprintf(
in_filename, in_filename_len + 1,
"%s/.config/bytenuts/inbuf.log",
home
);
/* move this processes history to the path that can be loaded on resumption */
fclose(ingest.history_fd);
rename(ingest.history_filename, in_filename);
free(in_filename);
free(ingest.history_filename);
}
pthread_exit(NULL);
return NULL;
}

View file

@ -24,6 +24,7 @@ typedef struct ingest_struct {
int inpos; /* cursor position within the inbuf itself */
int inlen; /* length of the input buffer */
char **history; /* array of strings representing the command history */
char *history_filename; /* realpath of inbuf.pid.log */
FILE *history_fd;
char tmp_history[4096]; /* copy of inbuf before we load in history */
int history_len;