Skip to content

Commit d68100f

Browse files
Added getdir command to cf-net, recursively copies directory
Ticket: CFE-2986 Changelog: Title Signed-off-by: Simon Halvorsen <simon.halvorsen@northern.tech>
1 parent 7789b21 commit d68100f

1 file changed

Lines changed: 289 additions & 0 deletions

File tree

cf-net/cf-net.c

Lines changed: 289 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#include <cleanup.h>
4545
#include <protocol.h>
4646
#include <sequence.h>
47+
#include <files_lib.h>
4748

4849
#define ARG_UNUSED __attribute__((unused))
4950

@@ -100,6 +101,8 @@ static const Description COMMANDS[] =
100101
"\t\t\t(%d can be used in both the remote and output file paths when '-j' is used)"},
101102
{"opendir", "List files and folders in a directory",
102103
"cf-net opendir masterfiles"},
104+
{"getdir", "Recursively downloads files and folders in a directory",
105+
"cf-net getdir masterfiles/ -o /tmp/ [-lRECURSIONLIMIT]"},
103106
{NULL, NULL, NULL}
104107
};
105108

@@ -144,6 +147,7 @@ static const char *const HINTS[] =
144147
generator_macro(STAT) \
145148
generator_macro(GET) \
146149
generator_macro(OPENDIR) \
150+
generator_macro(GETDIR) \
147151
generator_macro(MULTI) \
148152
generator_macro(MULTITLS) \
149153
generator_macro(HELP) \
@@ -197,6 +201,7 @@ static int CFNetGet(CFNetOptions *opts, const char *hostname, char **args);
197201
static int CFNetOpenDir(CFNetOptions *opts, const char *hostname, char **args);
198202
static int CFNetMulti(const char *server);
199203
static int CFNetMultiTLS(const char *server, const char *use_protocol_version);
204+
static int CFNetGetDir(CFNetOptions *opts, const char *hostname, char **args);
200205

201206

202207
//*******************************************************************
@@ -411,6 +416,8 @@ static int CFNetCommandSwitch(CFNetOptions *opts, const char *hostname,
411416
return CFNetGet(opts, hostname, args);
412417
case CFNET_CMD_OPENDIR:
413418
return CFNetOpenDir(opts, hostname, args);
419+
case CFNET_CMD_GETDIR:
420+
return CFNetGetDir(opts, hostname, args);
414421
case CFNET_CMD_MULTI:
415422
return CFNetMulti(hostname);
416423
case CFNET_CMD_MULTITLS:
@@ -591,6 +598,16 @@ static int CFNetHelpTopic(const char *topic)
591598
"\nbasename in current working directory (cwd). Override this"
592599
"\nusing the -o filename option (-o - for stdout).\n");
593600
}
601+
else if (strcmp("getdir", topic) == 0)
602+
{
603+
printf("\ncf-net getdir recursively downloads a directory from a remote host."
604+
"\nIt uses OPENDIR to list contents, STAT to check file types, and GET"
605+
"\nto download files. By default the directory is saved with its basename"
606+
"\nin the current working directory (cwd). Override the destination using"
607+
"\nthe -o path option. Use --limit N to control recursion depth (default: 10)."
608+
"\n\nUsage: cf-net getdir [-o output_path] [--limit N] <remote_directory>"
609+
"\n\nExample: cf-net getdir -o /tmp/backup --limit 5 masterfiles/\n");
610+
}
594611
else
595612
{
596613
if (found == false)
@@ -976,6 +993,278 @@ static int CFNetOpenDir(ARG_UNUSED CFNetOptions *opts, const char *hostname, cha
976993
return 0;
977994
}
978995

996+
// Helper: Get a single file with permissions
997+
static bool CFNetGetWithPerms(AgentConnection *conn, const char *remote_path,
998+
const char *local_path, bool print_stats)
999+
{
1000+
assert(conn != NULL && remote_path != NULL && local_path != NULL);
1001+
1002+
struct stat perms;
1003+
if (!ProtocolStat(conn, remote_path, &perms))
1004+
{
1005+
Log(LOG_LEVEL_ERR, "Failed to stat remote file: %s:%s",
1006+
conn->this_server, remote_path);
1007+
return false;
1008+
}
1009+
1010+
if (!ProtocolGet(conn, remote_path, local_path, perms.st_size, perms.st_mode, print_stats))
1011+
{
1012+
Log(LOG_LEVEL_ERR, "Failed to get remote file: %s:%s",
1013+
conn->this_server, remote_path);
1014+
return false;
1015+
}
1016+
1017+
return true;
1018+
}
1019+
1020+
// Helper: Create local directory path
1021+
static bool create_local_dir(const char *local_base, const char *subdir,
1022+
bool has_output_path, mode_t perms)
1023+
{
1024+
char path[PATH_MAX];
1025+
int written;
1026+
1027+
if (has_output_path)
1028+
{
1029+
written = snprintf(path, sizeof(path), "%s/%s/", local_base, subdir);
1030+
}
1031+
else
1032+
{
1033+
char cwd[PATH_MAX];
1034+
if (!getcwd(cwd, sizeof(cwd)))
1035+
{
1036+
Log(LOG_LEVEL_ERR, "Failed to get current working directory");
1037+
return false;
1038+
}
1039+
written = snprintf(path, sizeof(path), "%s/%s/%s/", cwd, local_base, subdir);
1040+
}
1041+
1042+
if (written < 0 || written >= sizeof(path))
1043+
{
1044+
Log(LOG_LEVEL_ERR, "Path too long for new directory: %s", subdir);
1045+
return false;
1046+
}
1047+
1048+
bool* created = NULL;
1049+
bool force = false;
1050+
MakeParentDirectoryPerms(path, force, created, perms);
1051+
return true;
1052+
}
1053+
1054+
// Helper: Recursively process directory entries
1055+
static void process_dir_recursive(AgentConnection *conn,
1056+
const char *remote_path,
1057+
const char *local_path,
1058+
bool has_output_path,
1059+
bool print_stats,
1060+
int limit,
1061+
bool *failure)
1062+
{
1063+
int lim = limit - 1;
1064+
if (0 > lim)
1065+
{
1066+
Log(LOG_LEVEL_ERR, "Recursion limit reached");
1067+
*failure = true;
1068+
return;
1069+
}
1070+
1071+
int written;
1072+
Seq *items = ProtocolOpenDir(conn, remote_path);
1073+
if (!items)
1074+
{
1075+
*failure = true;
1076+
return;
1077+
}
1078+
1079+
for (size_t i = 0; i < SeqLength(items); i++)
1080+
{
1081+
char *item = SeqAt(items, i);
1082+
1083+
if (strcmp(".", item) == 0 || strcmp("..", item) == 0)
1084+
{
1085+
continue;
1086+
}
1087+
1088+
char remote_full[PATH_MAX];
1089+
written = snprintf(remote_full, sizeof(remote_full), "%s/%s", remote_path, item);
1090+
if (written < 0 || written >= sizeof(remote_full))
1091+
{
1092+
Log(LOG_LEVEL_ERR,
1093+
"Path too long for building full remote path: %s and %s",
1094+
remote_path, item);
1095+
*failure = true;
1096+
continue;
1097+
}
1098+
1099+
char local_full[PATH_MAX];
1100+
written = snprintf(local_full, sizeof(local_full), "%s/%s", local_path, item);
1101+
if (written < 0 || written >= sizeof(local_full))
1102+
{
1103+
Log(LOG_LEVEL_ERR,
1104+
"Path too long for building full local path: %s and %s",
1105+
local_path, item);
1106+
*failure = true;
1107+
continue;
1108+
}
1109+
1110+
struct stat sb;
1111+
if (!ProtocolStat(conn, remote_full, &sb))
1112+
{
1113+
Log(LOG_LEVEL_ERR, "Could not stat: %s", remote_full);
1114+
*failure = true;
1115+
continue;
1116+
}
1117+
1118+
if (S_ISDIR(sb.st_mode)) // Is directory
1119+
{
1120+
if (!create_local_dir(local_path, item, has_output_path, sb.st_mode))
1121+
{
1122+
// Error already logged
1123+
*failure = true;
1124+
continue;
1125+
}
1126+
process_dir_recursive(conn, remote_full, local_full, has_output_path, print_stats, lim, failure);
1127+
}
1128+
else
1129+
{
1130+
CFNetGetWithPerms(conn, remote_full, local_full, print_stats);
1131+
}
1132+
}
1133+
1134+
SeqDestroy(items);
1135+
}
1136+
1137+
static int CFNetGetDir(CFNetOptions *opts, const char *hostname, char **args)
1138+
{
1139+
assert(opts != NULL);
1140+
assert(hostname != NULL);
1141+
assert(args != NULL);
1142+
char *local_dir = NULL;
1143+
int limit = 10;
1144+
1145+
int argc = 0;
1146+
while (args[argc] != NULL)
1147+
{
1148+
++argc;
1149+
}
1150+
1151+
static struct option longopts[] = {
1152+
{ "output", required_argument, NULL, 'o' },
1153+
{ "limit", required_argument, NULL, 'l' },
1154+
{ NULL, 0, NULL, 0 }
1155+
};
1156+
if (argc <= 1)
1157+
{
1158+
return invalid_command("getdir");
1159+
}
1160+
extern int optind;
1161+
optind = 0;
1162+
extern char *optarg;
1163+
int c = 0;
1164+
const char *optstr = "o:l:";
1165+
bool specified_path = false;
1166+
while ((c = getopt_long(argc, args, optstr, longopts, NULL))
1167+
!= -1)
1168+
{
1169+
switch (c)
1170+
{
1171+
case 'o':
1172+
{
1173+
if (local_dir != NULL)
1174+
{
1175+
Log(LOG_LEVEL_INFO,
1176+
"Warning: multiple occurrences of -o in command, "\
1177+
"only last one will be used.");
1178+
free(local_dir);
1179+
}
1180+
local_dir = xstrdup(optarg);
1181+
specified_path = true;
1182+
break;
1183+
}
1184+
case 'l':
1185+
{
1186+
char *lim;
1187+
long val = strtol(optarg, &lim, 10);
1188+
if (*lim != '\0' || val < 0)
1189+
{
1190+
Log(LOG_LEVEL_ERR, "Invalid limit value: %s", optarg);
1191+
return invalid_command("getdir");
1192+
}
1193+
limit = (int)val;
1194+
break;
1195+
}
1196+
case ':':
1197+
case '?':
1198+
{
1199+
return invalid_command("getdir");
1200+
}
1201+
default:
1202+
{
1203+
printf("Default optarg = '%s', c = '%c' = %i\n",
1204+
optarg, c, (int)c);
1205+
break;
1206+
}
1207+
}
1208+
}
1209+
args = &(args[optind]);
1210+
argc -= optind;
1211+
char *remote_dir = args[0];
1212+
char *base = basename(remote_dir);
1213+
if (specified_path)
1214+
{
1215+
char temp[PATH_MAX];
1216+
1217+
int written = snprintf(temp, sizeof(temp), "%s/%s", local_dir, base);
1218+
if (written < 0 || written >= sizeof(temp))
1219+
{
1220+
Log(LOG_LEVEL_ERR, "Path too long for local path: %s/%s", local_dir, base);
1221+
free(local_dir);
1222+
return -1;
1223+
}
1224+
free(local_dir);
1225+
local_dir = xstrdup(temp);
1226+
}
1227+
1228+
if (local_dir == NULL)
1229+
{
1230+
local_dir = xstrdup(base);
1231+
}
1232+
1233+
AgentConnection *conn = CFNetOpenConnection(hostname, opts->use_protocol_version);
1234+
if (conn == NULL)
1235+
{
1236+
free(local_dir);
1237+
return -1;
1238+
}
1239+
struct stat sb;
1240+
bool ret = ProtocolStat(conn, remote_dir, &sb);
1241+
if (!ret)
1242+
{
1243+
printf("Could not stat: '%s'\n", remote_dir);
1244+
free(local_dir);
1245+
CFNetDisconnect(conn);
1246+
return -1;
1247+
}
1248+
if (!S_ISDIR(sb.st_mode))
1249+
{
1250+
printf("'%s' is not a directory, use 'get' for single file download\n", remote_dir);
1251+
free(local_dir);
1252+
CFNetDisconnect(conn);
1253+
return -1;
1254+
}
1255+
1256+
bool failure = false;
1257+
process_dir_recursive(conn, remote_dir, local_dir, specified_path, opts->print_stats, limit, &failure);
1258+
if (failure)
1259+
{
1260+
Log(LOG_LEVEL_INFO, "Recursion limit(%d) reached or one or more entries were not copied", limit);
1261+
}
1262+
1263+
free(local_dir);
1264+
CFNetDisconnect(conn);
1265+
return failure ? -1 : 0;
1266+
}
1267+
9791268
static int CFNetMulti(const char *server)
9801269
{
9811270
time_t start;

0 commit comments

Comments
 (0)