Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/clib-install.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "commander/commander.h"
#include "common/clib-cache.h"
#include "common/clib-package.h"
#include "common/clib-validate.h"
#include "debug/debug.h"
#include "fs/fs.h"
#include "http-get/http-get.h"
Expand Down Expand Up @@ -128,8 +129,7 @@ static void setopt_skip_cache(command_t *self) {
}

static int install_local_packages_with_package_name(const char *file) {
if (-1 == fs_exists(file)) {
logger_error("error", "Missing clib.json or package.json");
if (0 != clib_validate(file)) {
return 1;
}

Expand Down
4 changes: 2 additions & 2 deletions src/clib-update.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "commander/commander.h"
#include "common/clib-cache.h"
#include "common/clib-package.h"
#include "common/clib-validate.h"
#include "debug/debug.h"
#include "fs/fs.h"
#include "http-get/http-get.h"
Expand Down Expand Up @@ -98,8 +99,7 @@ static void setopt_concurrency(command_t *self) {
#endif

static int install_local_packages_with_package_name(const char *file) {
if (-1 == fs_exists(file)) {
logger_error("error", "Missing clib.json or package.json");
if (0 != clib_validate(file)) {
return 1;
}

Expand Down
16 changes: 8 additions & 8 deletions src/common/clib-package.c
Original file line number Diff line number Diff line change
Expand Up @@ -395,14 +395,14 @@ static inline int install_packages(list_t *list, const char *dir, int verbose) {
list_iterator_destroy(iterator);

if (freelist) {
iterator = list_iterator_new(freelist, LIST_HEAD);
while ((node = list_iterator_next(iterator))) {
clib_package_t *pkg = node->val;
if (pkg)
clib_package_free(pkg);
}
list_iterator_destroy(iterator);
list_destroy(freelist);
iterator = list_iterator_new(freelist, LIST_HEAD);
while ((node = list_iterator_next(iterator))) {
clib_package_t *pkg = node->val;
if (pkg)
clib_package_free(pkg);
}
list_iterator_destroy(iterator);
list_destroy(freelist);
}
return rc;
}
Expand Down
95 changes: 95 additions & 0 deletions src/common/clib-validate.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@

//
// clib-validate: `clib-validate.c`
//
// Copyright (c) 2014 Stephen Mathieson
// Copyright (c) 2021 clib authors
//
// MIT licensed
//

#include "fs/fs.h"
#include "logger/logger.h"
#include "parse-repo/parse-repo.h"
#include "parson/parson.h"
#include "path-join/path-join.h"
#include "strdup/strdup.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define ERROR_FORMAT(err, ...) \
({ \
rc = 1; \
logger_error("error", err, __VA_ARGS__); \
goto done; \
})

#define WARN(warning) ({ logger_warn("warning", warning); });

#define WARN_FORMAT(warning, ...) \
({ logger_warn("warning", warning, __VA_ARGS__); });

#define WARN_MISSING(key, file) \
({ WARN_FORMAT("missing " #key " in %s", file); })

#define require_string(name, file) \
({ \
const char *__##name = json_object_get_string(obj, #name); \
if (!(__##name)) \
WARN_MISSING(#name, file); \
})

int clib_validate(const char *file) {
const char *repo = NULL;
char *repo_owner = NULL;
char *repo_name = NULL;
int rc = 0;
JSON_Value *root = NULL;
JSON_Object *obj = NULL;
JSON_Value *src = NULL;

if (-1 == fs_exists(file))
ERROR_FORMAT("no such file: %s", file);
if (!(root = json_parse_file(file)))
ERROR_FORMAT("malformed file: %s", file);
if (!(obj = json_value_get_object(root)))
ERROR_FORMAT("malformed file: %s", file);

require_string(name, file);
require_string(version, file);
// TODO: validate semver

repo = json_object_get_string(obj, "repo");
if (!repo) {
WARN_MISSING("repo", file);
} else {
if (!(repo_name = parse_repo_name(repo)))
WARN("invalid repo");
if (!(repo_owner = parse_repo_owner(repo, NULL)))
WARN("invalid repo");
}

require_string(description, file);
require_string(license, file);

if (!json_object_get_array(obj, "keywords")) {
WARN_MISSING("keywords", file);
}

src = json_object_get_value(obj, "src");
if (!src) {

if (!json_object_get_string(obj, "install"))
ERROR_FORMAT("Must have either src or install defined in %s", file);

} else if (json_value_get_type(src) != JSONArray) {
WARN("src should be an array")
}

done:
if (root)
json_value_free(root);
return rc;
}
16 changes: 16 additions & 0 deletions src/common/clib-validate.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// clib-validate.h
//
// Copyright (c) 2014-2021 clib authors
// MIT licensed
//

#ifndef CLIB_VALIDATE_H
#define CLIB_VALIDATE_H

/**
* @return 0 if the file is valid
*/
int clib_validate(const char *file);

#endif