aboutsummaryrefslogtreecommitdiff
path: root/src/pow_lua.c
blob: 1d2f81a1f497e8ccb9d58f90e218a80d2ec6324c (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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
 
  pow! - the pow!erful package manager

  Copyright (C) 2026 forever2k38
  
  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program. If not, see <https://www.gnu.org/licenses/>.

*/

#include "pow_lua.h"
#include "config.h"
#include "log.h"

#include "archive.h"
#include "stdio.h"
#include "archive_entry.h"
#include "lauxlib.h"
#include "stdlib.h"
#include "unistd.h"
#include "string.h"
#include "sys/sendfile.h"

int pow_lua_extract(lua_State *L) {
  int r;
  ssize_t size;

  struct archive *a;
  struct archive_entry *entry;

  if (!lua_isstring(L, 1)) {
    lua_pushnil(L);
    lua_pushstring(L, "arg 1 must be string (src)");

    return 2;
  }

  if (!lua_isstring(L, 2)) {
    lua_pushnil(L);
    lua_pushstring(L, "arg 2 must be string (dest)");

    return 2;
  }

  const char *filename = lua_tostring(L, 1);
  const char *dest = lua_tostring(L, 2);

  a = archive_read_new();

  archive_read_support_filter_all(a);
  archive_read_support_format_all(a);

  if ((r = archive_read_open_filename(a, filename, 10240))) {
    return 0;
  }

  for (;;) {
    r = archive_read_next_header(a, &entry);

    if (r == ARCHIVE_EOF)
      break;
    if (r < ARCHIVE_OK)
      pow_log_info("warn while unpacking: %s", archive_error_string(a));
    if (r < ARCHIVE_WARN) {
      pow_log_error("error while unpacking: %s", archive_error_string(a));

      lua_pop(L, 1);

      char *error_msg = malloc(256);

      lua_pushnil(L);

      if (!error_msg) {
        lua_pushstring(L, "error while unpacking and failed to malloc to push "
                          "real error message");
        pow_log_error("error while unpacking and failed to push real error message");

        return 2;
      }

      snprintf(error_msg, 256, "error while unpacking: %s",
               archive_error_string(a));

      lua_pushstring(L, error_msg);

      free(error_msg);

      return 2;
    }

    const char *cur = archive_entry_pathname(entry);

    if (cur) {
      size_t need = strlen(dest) + strlen(cur) + 2;

      char *full = malloc(need);

      if (!full) {
        lua_pushnil(L);
        lua_pushstring(L, "failed to malloc entry path\n");

        archive_read_close(a);
        archive_read_free(a);

        return 2;
      }

      snprintf(full, need, "%s/%s", dest, cur);

      archive_entry_set_pathname(entry, full);

      free(full);
    }

    r = archive_read_extract(a, entry, 0);

    if (r < ARCHIVE_WARN) {
      lua_pushnil(L);
      lua_pushfstring(
          L, "failed to extract '%s': %s", archive_entry_pathname(entry),
          archive_error_string(a) ? archive_error_string(a) : "unknown");
      archive_read_close(a);
      archive_read_free(a);

      return 2;
    }
  }

  archive_read_close(a);
  archive_read_free(a);

  lua_pushboolean(L, 1);

  return 1;
}


int pow_lua_install(lua_State *L) {
  if (!lua_isstring(L, 1)) {
    lua_pushnil(L);
    lua_pushstring(L, "arg 1 must be string (src)");

    return 2;
  }

  if (!lua_isstring(L, 2)) {
    lua_pushnil(L);
    lua_pushstring(L, "arg 2 must be string (dest)");

    return 2;
  }

  char *src = strdup(lua_tostring(L, 1));

  if (!src) {
    pow_log_error("failed to strdup source string");

    return 2;
  }

  char *dest = strdup(lua_tostring(L, 2));

  if (!dest) {
    free(src);

    pow_log_error("failed to strdup dest string");

    return 2;
  }

  pow_log_info("installing: %s to %s", src, dest);

  FILE *src_stream = fopen(src, "r");

  if (!src_stream) {
    free(src);
    free(dest);

    pow_log_error("failed to open source file %s while installing", src);

    return 2;
  }

  FILE *dest_stream = fopen(dest, "w");

  if (!dest_stream) {
    free(src);
    free(dest);

    fclose(src_stream);

    pow_log_error("failed to open destination file %s for writing while installing", dest);

    return 2;
  }

  int src_fd = fileno(src_stream);
  int dest_fd = fileno(dest_stream);

  struct stat src_stat = {0};
  int result = fstat(src_fd, &src_stat);

  off_t copied = 0;
  while (result == 0 && copied < src_stat.st_size) {
    ssize_t written = sendfile(dest_fd, src_fd, &copied, SIZE_MAX);
    copied += written;

    if (written == -1) {
      result = -1;
    }
  }

  if (fchmod(dest_fd, src_stat.st_mode) == -1) {
    pow_log_error("failed to set permission bits, expect breakage!");
  }

  return 1;
}

luaL_Reg libpow[] = {{"extract", pow_lua_extract}, {"install", pow_lua_install}, {NULL, NULL}};

int pow_lua_load(lua_State *L, pow_config *cfg) {
  luaL_newlib(L, libpow);

  lua_createtable(L, 0, 1);

  lua_pushstring(L, cfg->makeopts);
  lua_setfield(L, -2, "makeopts");

  lua_setfield(L, -2, "config");

  lua_setglobal(L, "pow");

  return 0;
}