aboutsummaryrefslogtreecommitdiff
path: root/src/repo.c
blob: 93995797ef2841655e3fe14a32e29e272117c91b (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
/*
 
  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 "repo.h"
#include "config.h"
#include "log.h"

#include "dirent.h"
#include "limits.h"
#include "malloc.h"
#include "package.h"
#include "stdio.h"
#include <lua.h>

int pow_repo_get_package(pow_config *cfg, pow_package *pkg,
                         const char *query) {
  if (!cfg->repos) {
    pow_log_error("no repos defined in your config, please define them");

    return 1;
  }

  DIR *dp;
  struct dirent *ep;

  for (size_t i = 0; i < cfg->repos_count; i++) {
    char *package_path = malloc(PATH_MAX);

    if (!package_path) {
      pow_log_error("failed to allocate package_path, sizeof %d", PATH_MAX);

      return 1;
    }

    int written =
        snprintf(package_path, PATH_MAX, "%s/%s", cfg->repos[i]->path, query);

    if (written >= PATH_MAX) {
      pow_log_error("something went seriously wrong. the path for the package %s is "
             "too long?",
             query);

      free(package_path);

      return 1;
    }

    dp = opendir(package_path);

    if (!dp) {
      free(package_path);

      continue;
    }

    pow_log_info("found package %s in repo %s", query, cfg->repos[i]->path);

    char *full_path = malloc(PATH_MAX);

    if (!full_path) {
      free(package_path);

      pow_log_error("failed to malloc full path for build script");

      return 1;
    }

    written = snprintf(full_path, PATH_MAX, "%s/%s", package_path, "build.lua");

    if (written >= PATH_MAX) {
      pow_log_error("something went seriously wrong. the path for the package build "
             "script %s is "
             "too long?",
             query);

      free(package_path);
      free(full_path);

      return 1;
    }

    free(package_path);

    pow_package_open(pkg, cfg, full_path);

    free(full_path);

    closedir(dp);

    return 0;
  }

  return 1;
}

int pow_repo_check_package(pow_config *cfg,
                           const char *query) {
  int err;

  pow_package *pkg = malloc(sizeof(pow_package));

  if (!pkg) {
    pow_log_error("failed to malloc pow_package");

    return 1;
  }

  err = pow_repo_get_package(cfg, pkg, query);

  if (err) {
    return 1;
  }

  printf("Name: %s\n"
         "Version: %s\n",
         pkg->name, pkg->version);

  pow_package_close(pkg);

  return 0;
}