summaryrefslogtreecommitdiff
path: root/src/cli/export.c
blob: d7397c4b7f070c84d41035591f206787168d2161 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include "cli/cli.h"

#include "mcp/tools/common/common.h"
#include "storage/codec.h"
#include "storage/store.h"
#include "sys/system.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static bool
export_finish(FILE *output, const char *temporary_path, const char *destination)
{
  bool ok = fflush(output) == 0;
  if (output == stdout)
    {
      return ok;
    }
  if (fclose(output) != 0)
    {
      ok = false;
    }
  if (ok && !tiny_sys_path_replace(temporary_path, destination))
    {
      ok = false;
    }
  if (!ok)
    {
      (void)tiny_sys_file_remove(temporary_path);
    }
  return ok;
}

int
tiny_cli_export(tiny_store_t *store, const tiny_cli_options_t *options)
{
  size_t index;
  size_t emitted = 0;
  FILE *output = NULL;
  char *temporary_path = NULL;
  tiny_json_buffer_t item;

  if (!options || !options->export_path)
    {
      fprintf(stderr, "export: path is required\n");
      return 1;
    }

  if (strcmp(options->export_path, "-") == 0)
    {
      output = stdout;
    }
  else
    {
      size_t path_length = strlen(options->export_path);
      temporary_path = malloc(path_length + sizeof(".tmp"));
      if (!temporary_path)
        {
          fprintf(stderr, "export: out of memory\n");
          return 1;
        }
      memcpy(temporary_path, options->export_path, path_length);
      memcpy(temporary_path + path_length, ".tmp", sizeof(".tmp"));
      if (fopen_s(&output, temporary_path, "wb") != 0 || !output)
        {
          fprintf(stderr, "export: unable to open '%s'\n", options->export_path);
          free(temporary_path);
          return 1;
        }
    }

  if (fprintf(output, "{\"type\":\"tiny-convmem-manifest\",\"format_version\":2}\n") < 0)
    {
      fprintf(stderr, "export: failed to write manifest\n");
      if (output != stdout)
        {
          fclose(output);
          (void)tiny_sys_file_remove(temporary_path);
        }
      free(temporary_path);
      return 1;
    }

  for (index = 0; index < tiny_store_entry_count(store); ++index)
    {
      const tiny_store_entry_t *entry = tiny_store_entry(store, index);
      int write_failed;
      if (!entry)
        {
          continue;
        }
      if (entry->deleted && !options->include_deleted)
        {
          continue;
        }
      if (options->session_id && options->session_id[0]
          && strcmp(entry->message.session_id, options->session_id) != 0)
        {
          continue;
        }

      tiny_json_init(&item);
      if (entry->deleted)
        {
          tiny_json_raw(&item, "{\"type\":\"DEL\",\"message_id\":");
          tiny_json_string(&item, entry->message.message_id);
          tiny_json_raw(&item, "}");
        }
      else
        {
          tiny_mcp_tool_append_message(&item, &entry->message);
        }

      write_failed = item.failed || fprintf(output, "%s\n", item.data ? item.data : "") < 0;
      tiny_json_free(&item);
      if (write_failed)
        {
          fprintf(stderr, "export: failed to write record\n");
          if (output != stdout)
            {
              fclose(output);
              (void)tiny_sys_file_remove(temporary_path);
            }
          free(temporary_path);
          return 1;
        }
      emitted++;
    }

  if (!export_finish(output, temporary_path, options->export_path))
    {
      fprintf(stderr, "export: unable to complete '%s'\n", options->export_path);
      free(temporary_path);
      return 1;
    }
  free(temporary_path);
  (void)emitted;
  return 0;
}