Skip to content

Commit

Permalink
Added mget shell command
Browse files Browse the repository at this point in the history
Added the shell command 'mget' which takes as argument a pattern against which files are matched. Matching files are then "got" from the camera and saved to the current local directory.
  • Loading branch information
Bezierr committed Jun 5, 2023
1 parent 9552d9d commit 601f29c
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions gphoto2/shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fnmatch.h>

#ifdef HAVE_UNISTD_H
# include <unistd.h>
Expand Down Expand Up @@ -80,6 +81,7 @@ static int shell_cd (Camera *, const char *);
static int shell_lcd (Camera *, const char *);
static int shell_exit (Camera *, const char *);
static int shell_get (Camera *, const char *);
static int shell_mget (Camera *, const char *);
static int shell_put (Camera *, const char *);
static int shell_get_thumbnail (Camera *, const char *);
static int shell_get_raw (Camera *, const char *);
Expand Down Expand Up @@ -130,6 +132,7 @@ static const struct _ShellFunctionTable {
N_("directory"), 0},
{"exit", shell_exit, N_("Exit the gPhoto shell"), NULL, 0},
{"get", shell_get, N_("Download a file"), N_("[directory/]filename"), 1},
{"mget", shell_mget, N_("Download multiple files"), N_("[directory/]pattern"), 1},
{"put", shell_put, N_("Upload a file"), N_("[directory/]filename"), 1},
{"get-thumbnail", shell_get_thumbnail, N_("Download a thumbnail"),
N_("[directory/]filename"), 1},
Expand Down Expand Up @@ -745,6 +748,44 @@ shell_get (Camera __unused__ *camera, const char *arg)
return (GP_OK);
}

static int
shell_mget (Camera __unused__ *camera, const char *arg)
{
CameraList *list;
char folder[MAX_FOLDER_LEN];
int x;
int allcnt=0, getcnt=0;
const char *name;

/* Skip leading spaces of pattern */
while (arg[0] == ' ')
arg++;

/* Get file list of current directory */
CHECK (gp_list_new (&list));
CL (gp_camera_folder_list_files (p->camera, folder, list,
p->context), list);

/* Get all matching files */
for (x = 1; x <= gp_list_count (list); x++) {
gp_list_get_name (list, x - 1, &name);
if (fnmatch (arg, name, (1 << 4)) == 0) {
printf ("Getting file %s ... ", name);
fflush(stdout);
// get file
CHECK (shell_file_action (p->camera, p->context, p->folder, name, save_file_action));
getcnt++;
}
else if (!(p->flags & FLAGS_QUIET))
printf ("Skipping file %s\n", name);
allcnt++;
}

printf("Done. %d of %d files copied from current directory.\n", getcnt, allcnt);
gp_list_free (list);
return (GP_OK);
}

static int
shell_get_raw (Camera __unused__ *camera, const char *arg)
{
Expand Down

0 comments on commit 601f29c

Please sign in to comment.