7814
Ta
tiny-c-agent / files
tiny-c-agent / src / session.c
5.1 KB
1#include "session.h" 2 3#include <errno.h> 4#include <fcntl.h> 5#include <limits.h> 6#include <stdio.h> 7#include <stdlib.h> 8#include <string.h> 9#include <sys/random.h> 10#include <sys/stat.h> 11#include <time.h> 12#include <unistd.h> 13 14#include "util.h" 15 16struct tiny_session { 17 int fd; 18 char id[128]; 19 char path[PATH_MAX + 16]; 20}; 21 22static void stamp_now(char out[32]) { 23 time_t t = time(NULL); 24 struct tm tm; 25 gmtime_r(&t, &tm); 26 strftime(out, 32, "%Y%m%d-%H%M%S", &tm); 27} 28 29static void iso_now(char out[32]) { 30 time_t t = time(NULL); 31 struct tm tm; 32 gmtime_r(&t, &tm); 33 strftime(out, 32, "%Y-%m-%dT%H:%M:%SZ", &tm); 34} 35 36static void short_id(char out[7]) { 37 unsigned char b[6] = {0}; 38 size_t got = 0; 39 while (got < sizeof b) { 40 ssize_t n = getrandom(b + got, sizeof b - got, 0); 41 if (n < 0) { 42 if (errno == EINTR) continue; 43 break; 44 } 45 got += (size_t)n; 46 } 47 static const char alnum[] = "0123456789abcdefghijklmnopqrstuvwxyz"; 48 for (size_t i = 0; i < 6; i++) out[i] = alnum[b[i] % 36]; 49 out[6] = '\0'; 50} 51 52// Lowercases text into a dash-separated slug, collapsing runs and trimming. 53static void slugify(const char *text, char *out, size_t cap) { 54 size_t n = 0; 55 bool dash = false; 56 for (const unsigned char *p = (const unsigned char *)text; *p; p++) { 57 int c = *p; 58 if (c >= 'A' && c <= 'Z') c = c - 'A' + 'a'; 59 else if (!((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9'))) { 60 dash = n > 0; 61 continue; 62 } 63 if (dash && n + 1 < cap) out[n++] = '-'; 64 dash = false; 65 if (n + 1 < cap) out[n++] = (char)c; 66 } 67 out[n] = '\0'; 68 if (n == 0) snprintf(out, cap, "session"); 69} 70 71// Writes one line and fsyncs, so a committed record survives a crash. 72static int write_record(tiny_session *s, const char *line, size_t len, char **err) { 73 if (tiny_write_all(s->fd, line, len) != 0 || tiny_write_all(s->fd, "\n", 1) != 0) 74 return tiny_fail(err, "session %s: write failed: %s", s->path, strerror(errno)); 75 if (fsync(s->fd) != 0) 76 return tiny_fail(err, "session %s: fsync failed: %s", s->path, strerror(errno)); 77 return 0; 78} 79 80tiny_session *tiny_session_open(const char *sessions_dir, const char *cwd, 81 const char *title, char **err) { 82 if (err) *err = NULL; 83 84 char slug[48]; 85 slugify(title ? title : "", slug, sizeof slug); 86 if (tiny_mkdirs(sessions_dir) != 0) { 87 tiny_fail(err, "session: cannot create %s: %s", sessions_dir, strerror(errno)); 88 return NULL; 89 } 90 91 char id[128]; 92 char dir[PATH_MAX]; 93 bool created = false; 94 for (int attempt = 0; attempt < 16 && !created; attempt++) { 95 char sid[7]; 96 short_id(sid); 97 char stamp[32]; 98 stamp_now(stamp); 99 snprintf(id, sizeof id, "%s-%s-%s", stamp, slug, sid); 100 snprintf(dir, sizeof dir, "%s/%s", sessions_dir, id); 101 if (mkdir(dir, 0755) == 0) created = true; 102 else if (errno != EEXIST) { 103 tiny_fail(err, "session: cannot create %s: %s", dir, strerror(errno)); 104 return NULL; 105 } 106 } 107 if (!created) { 108 tiny_fail(err, "session: could not create a unique directory in %s", sessions_dir); 109 return NULL; 110 } 111 112 char log_path[PATH_MAX + 16]; 113 snprintf(log_path, sizeof log_path, "%s/session.jsonl", dir); 114 int fd = open(log_path, O_WRONLY | O_CREAT | O_APPEND, 0644); 115 if (fd < 0) { 116 tiny_fail(err, "session: cannot open %s: %s", log_path, strerror(errno)); 117 return NULL; 118 } 119 120 tiny_session *s = xmalloc(sizeof *s); 121 s->fd = fd; 122 snprintf(s->id, sizeof s->id, "%s", id); 123 snprintf(s->path, sizeof s->path, "%s", log_path); 124 125 yyjson_mut_doc *doc = yyjson_mut_doc_new(NULL); 126 yyjson_mut_val *obj = yyjson_mut_obj(doc); 127 yyjson_mut_doc_set_root(doc, obj); 128 char created_at[32]; 129 iso_now(created_at); 130 yyjson_mut_obj_add_str(doc, obj, "type", "session"); 131 yyjson_mut_obj_add_int(doc, obj, "version", 1); 132 yyjson_mut_obj_add_str(doc, obj, "id", id); 133 yyjson_mut_obj_add_str(doc, obj, "cwd", cwd ? cwd : ""); 134 yyjson_mut_obj_add_str(doc, obj, "createdAt", created_at); 135 yyjson_mut_obj_add_str(doc, obj, "title", slug); 136 size_t len = 0; 137 char *line = yyjson_mut_val_write(obj, 0, &len); 138 yyjson_mut_doc_free(doc); 139 if (!line) { 140 tiny_fail(err, "session %s: cannot serialize header", s->path); 141 close(fd); 142 free(s); 143 return NULL; 144 } 145 146 int rc = write_record(s, line, len, err); 147 free(line); 148 if (rc != 0) { 149 close(fd); 150 free(s); 151 return NULL; 152 } 153 return s; 154} 155 156const char *tiny_session_id(const tiny_session *s) { 157 return s->id; 158} 159 160int tiny_session_append(tiny_session *s, yyjson_mut_val *record, char **err) { 161 if (err) *err = NULL; 162 size_t len = 0; 163 char *line = yyjson_mut_val_write(record, 0, &len); 164 if (!line) return tiny_fail(err, "session %s: cannot serialize record", s->path); 165 int rc = write_record(s, line, len, err); 166 free(line); 167 return rc; 168} 169 170void tiny_session_close(tiny_session *s) { 171 if (!s) return; 172 close(s->fd); 173 free(s); 174}