2022-01-13 17:30:15 +01:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdbool.h>
|
2022-01-14 11:26:54 +01:00
|
|
|
#include <string.h>
|
2022-01-13 17:30:15 +01:00
|
|
|
|
|
|
|
#include "cmd.h"
|
|
|
|
#include "error.h"
|
|
|
|
#include "uio.h"
|
|
|
|
#include "api.h"
|
|
|
|
#include "config.h"
|
|
|
|
#include "cache.h"
|
|
|
|
#include "util.h"
|
|
|
|
|
2022-01-14 11:26:54 +01:00
|
|
|
bool did_cache_init = false;
|
|
|
|
|
2022-01-14 22:12:50 +01:00
|
|
|
uint64_t cmd_modify_arg_parse(const char *str, uint64_t *out_wdate)
|
2022-01-14 11:26:54 +01:00
|
|
|
{
|
|
|
|
uint64_t val;
|
|
|
|
struct cache_entry ce;
|
|
|
|
enum error err;
|
2022-01-14 22:12:50 +01:00
|
|
|
const char *fn_st = strrchr(str, '/');
|
|
|
|
const char *size_st = strchr(str, '/');
|
2022-01-14 11:26:54 +01:00
|
|
|
|
2022-01-14 22:12:50 +01:00
|
|
|
*out_wdate = 0;
|
|
|
|
|
|
|
|
/* Skip the '/' */
|
|
|
|
if (fn_st)
|
|
|
|
fn_st++;
|
|
|
|
if (size_st)
|
|
|
|
size_st++;
|
|
|
|
|
|
|
|
if (fn_st == size_st) {
|
|
|
|
/* size/filename format */
|
|
|
|
size_st = str;
|
|
|
|
} else {
|
|
|
|
size_t timelen = size_st - str - 1; /* -1 for the skipped '/' */
|
|
|
|
char timstr[timelen + 1];
|
|
|
|
|
|
|
|
memcpy(timstr, str, timelen);
|
|
|
|
timstr[timelen] = '\0';
|
|
|
|
|
|
|
|
*out_wdate = util_iso2unix(timstr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sscanf(size_st, "%lu", &val) != 1)
|
2022-01-14 11:26:54 +01:00
|
|
|
return 0;
|
2022-01-14 22:12:50 +01:00
|
|
|
if (!fn_st && !size_st) /* Only lid format */
|
2022-01-14 11:26:54 +01:00
|
|
|
return val;
|
|
|
|
|
|
|
|
if (!cache_is_init()) {
|
|
|
|
if (cache_init() != NOERR)
|
|
|
|
return 0;
|
|
|
|
did_cache_init = true;
|
|
|
|
}
|
|
|
|
|
2022-01-14 22:12:50 +01:00
|
|
|
err = cache_get(fn_st, val, CACHE_S_LID, &ce);
|
2022-01-14 11:26:54 +01:00
|
|
|
if (err != NOERR)
|
|
|
|
return 0;
|
|
|
|
return ce.lid;
|
|
|
|
}
|
|
|
|
|
2022-01-13 17:30:15 +01:00
|
|
|
enum error cmd_modify(void *data)
|
|
|
|
{
|
|
|
|
struct api_mylistadd_opts mopt = {0};
|
|
|
|
bool *watched;
|
|
|
|
const char **wdate_str;
|
|
|
|
enum error err = NOERR;
|
|
|
|
int fcount;
|
|
|
|
|
|
|
|
fcount = config_get_nonopt_count();
|
|
|
|
if (fcount == 0) {
|
|
|
|
uio_error("No mylist ids specified");
|
|
|
|
return ERR_CMD_ARG;
|
|
|
|
}
|
|
|
|
|
2023-08-08 19:15:19 +02:00
|
|
|
if (config_get_subopt("modify", "watched", (void**)&watched) == NOERR && *watched) {
|
2022-01-13 17:30:15 +01:00
|
|
|
mopt.watched = *watched;
|
|
|
|
mopt.watched_set = true;
|
|
|
|
|
2023-08-08 19:15:19 +02:00
|
|
|
if (config_get_subopt("modify", "wdate", (void**)&wdate_str) == NOERR) {
|
2022-01-13 17:30:15 +01:00
|
|
|
uint64_t wdate = util_iso2unix(*wdate_str);
|
|
|
|
|
|
|
|
if (wdate == 0) {
|
|
|
|
uio_error("Invalid time value: '%s'", *wdate_str);
|
|
|
|
return ERR_CMD_ARG;
|
|
|
|
}
|
|
|
|
mopt.wdate = wdate;
|
|
|
|
mopt.wdate_set = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < fcount; i++) {
|
|
|
|
struct api_result res;
|
2022-01-14 22:12:50 +01:00
|
|
|
struct api_mylistadd_opts l_opts = mopt;
|
2022-01-13 17:30:15 +01:00
|
|
|
const char *arg = config_get_nonopt(i);
|
2022-01-14 22:12:50 +01:00
|
|
|
uint64_t wdate;
|
|
|
|
uint64_t lid = cmd_modify_arg_parse(arg, &wdate);
|
2022-01-13 17:30:15 +01:00
|
|
|
|
2022-01-14 11:26:54 +01:00
|
|
|
if (lid == 0) {
|
|
|
|
uio_error("Argument '%s' is not valid. Skipping", arg);
|
2022-01-13 17:30:15 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-01-14 22:12:50 +01:00
|
|
|
if (wdate != 0) {
|
|
|
|
l_opts.watched = true;
|
|
|
|
l_opts.watched_set = true;
|
|
|
|
|
|
|
|
l_opts.wdate = wdate;
|
|
|
|
l_opts.wdate_set = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
err = api_cmd_mylistmod(lid, &l_opts, &res);
|
2022-01-13 17:30:15 +01:00
|
|
|
if (err != NOERR)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (res.code == APICODE_NO_SUCH_MYLIST_ENTRY) {
|
|
|
|
uio_error("No mylist entry with id: '%lu'", lid);
|
2022-01-14 22:12:50 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!cache_is_init()) {
|
|
|
|
if ((err = cache_init()) != NOERR)
|
|
|
|
return err;
|
|
|
|
did_cache_init = true;
|
2022-01-13 17:30:15 +01:00
|
|
|
}
|
2022-01-14 22:12:50 +01:00
|
|
|
|
|
|
|
err = cache_update(lid, &l_opts);
|
|
|
|
if (err != NOERR)
|
|
|
|
break;
|
2022-01-13 17:30:15 +01:00
|
|
|
}
|
2022-01-14 11:26:54 +01:00
|
|
|
|
|
|
|
if (did_cache_init) {
|
|
|
|
did_cache_init = false;
|
|
|
|
cache_free();
|
|
|
|
}
|
2022-01-13 17:30:15 +01:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|