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) {
char *js;
json = lister(username, device, 0, s_hi);
json = lister(username, device, 0, s_hi, FALSE);
if (json == NULL) {
fprintf(stderr, "%s: cannot list\n", progname);
exit(2);
@ -350,10 +350,11 @@ int main(int argc, char **argv)
* 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
json_foreach(f, arr) {
locations(f->string_, obj, locs, s_lo, s_hi, otype, limit);
// printf("%s\n", f->string_);
}
}
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);
}
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;
int i, n;
@ -346,11 +346,20 @@ static void lsscan(char *pathpat, time_t s_lo, time_t s_hi, JsonNode *obj)
return;
}
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]);
if (reverse) {
for (i = n - 1; i >= 0; 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]);
}
} 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);
@ -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` is specified, and device is NULL, return user's devices
* 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();
UT_string *path = NULL;
@ -390,7 +400,7 @@ JsonNode *lister(char *user, char *device, time_t s_lo, time_t s_hi)
} else {
utstring_printf(path, "%s/rec/%s/%s",
STORAGEDIR, user, device);
lsscan(utstring_body(path), s_lo, s_hi, json);
lsscan(utstring_body(path), s_lo, s_hi, json, reverse);
}
return (json);

View File

@ -11,7 +11,7 @@ typedef enum {
RAW,
} 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);
int make_times(char *time_from, time_t *s_lo, char *time_to, time_t *s_to);
JsonNode *geo_json(JsonNode *json);