storage: reverse (--limit) now wraps backward accross montly boundaries

This commit is contained in:
Jan-Piet Mens 2015-08-29 20:50:13 +02:00
parent a2ea41d4a5
commit 54610a9332
3 changed files with 23 additions and 12 deletions

5
ocat.c
View File

@ -290,7 +290,7 @@ int main(int argc, char **argv)
if (list) { if (list) {
char *js; char *js;
json = lister(username, device, 0, s_hi); json = lister(username, device, 0, s_hi, FALSE);
if (json == NULL) { if (json == NULL) {
fprintf(stderr, "%s: cannot list\n", progname); fprintf(stderr, "%s: cannot list\n", progname);
exit(2); exit(2);
@ -350,10 +350,11 @@ int main(int argc, char **argv)
* process each and build the JSON `obj' with an array of locations. * process each and build the JSON `obj' with an array of locations.
*/ */
if ((json = lister(username, device, s_lo, s_hi)) != NULL) { if ((json = lister(username, device, s_lo, s_hi, (limit > 0) ? TRUE : FALSE)) != NULL) {
if ((arr = json_find_member(json, "results")) != NULL) { // get array if ((arr = json_find_member(json, "results")) != NULL) { // get array
json_foreach(f, arr) { json_foreach(f, arr) {
locations(f->string_, obj, locs, s_lo, s_hi, otype, limit); locations(f->string_, obj, locs, s_lo, s_hi, otype, limit);
// printf("%s\n", f->string_);
} }
} }
json_delete(json); json_delete(json);

View File

@ -325,7 +325,7 @@ static int cmp( const struct dirent **a, const struct dirent **b)
return strcmp((*a)->d_name, (*b)->d_name); return strcmp((*a)->d_name, (*b)->d_name);
} }
static void lsscan(char *pathpat, time_t s_lo, time_t s_hi, JsonNode *obj) static void lsscan(char *pathpat, time_t s_lo, time_t s_hi, JsonNode *obj, int reverse)
{ {
struct dirent **namelist; struct dirent **namelist;
int i, n; int i, n;
@ -346,11 +346,20 @@ static void lsscan(char *pathpat, time_t s_lo, time_t s_hi, JsonNode *obj)
return; return;
} }
for (i = 0; i < n; i++) { if (reverse) {
utstring_clear(path); for (i = n - 1; i >= 0; i--) {
utstring_printf(path, "%s/%s", pathpat, namelist[i]->d_name); utstring_clear(path);
json_append_element(jarr, json_mkstring(utstring_body(path))); utstring_printf(path, "%s/%s", pathpat, namelist[i]->d_name);
free(namelist[i]); json_append_element(jarr, json_mkstring(utstring_body(path)));
free(namelist[i]);
}
} else {
for (i = 0; i < n; i++) {
utstring_clear(path);
utstring_printf(path, "%s/%s", pathpat, namelist[i]->d_name);
json_append_element(jarr, json_mkstring(utstring_body(path)));
free(namelist[i]);
}
} }
free(namelist); free(namelist);
@ -361,10 +370,11 @@ static void lsscan(char *pathpat, time_t s_lo, time_t s_hi, JsonNode *obj)
* If `user' and `device' are both NULL, return list of users. * If `user' and `device' are both NULL, return list of users.
* If `user` is specified, and device is NULL, return user's devices * If `user` is specified, and device is NULL, return user's devices
* If both user and device are specified, return list of .rec files; * If both user and device are specified, return list of .rec files;
* in that case, limit with `s_lo` and `s_hi` * in that case, limit with `s_lo` and `s_hi`. `reverse' is TRUE if
* list should be sorted in descending order.
*/ */
JsonNode *lister(char *user, char *device, time_t s_lo, time_t s_hi) JsonNode *lister(char *user, char *device, time_t s_lo, time_t s_hi, int reverse)
{ {
JsonNode *json = json_mkobject(); JsonNode *json = json_mkobject();
UT_string *path = NULL; UT_string *path = NULL;
@ -390,7 +400,7 @@ JsonNode *lister(char *user, char *device, time_t s_lo, time_t s_hi)
} else { } else {
utstring_printf(path, "%s/rec/%s/%s", utstring_printf(path, "%s/rec/%s/%s",
STORAGEDIR, user, device); STORAGEDIR, user, device);
lsscan(utstring_body(path), s_lo, s_hi, json); lsscan(utstring_body(path), s_lo, s_hi, json, reverse);
} }
return (json); return (json);

View File

@ -11,7 +11,7 @@ typedef enum {
RAW, RAW,
} output_type; } output_type;
JsonNode *lister(char *username, char *device, time_t s_lo, time_t s_hi); JsonNode *lister(char *username, char *device, time_t s_lo, time_t s_hi, int reverse);
void locations(char *filename, JsonNode *obj, JsonNode *arr, time_t s_lo, time_t s_hi, output_type otype, int limit); void locations(char *filename, JsonNode *obj, JsonNode *arr, time_t s_lo, time_t s_hi, output_type otype, int limit);
int make_times(char *time_from, time_t *s_lo, char *time_to, time_t *s_to); int make_times(char *time_from, time_t *s_lo, char *time_to, time_t *s_to);
JsonNode *geo_json(JsonNode *json); JsonNode *geo_json(JsonNode *json);