core: Dump directory contents
Signed-off-by: Chloe M. <chloe@mirocom.org>
This commit is contained in:
+81
-1
@@ -3,9 +3,18 @@
|
|||||||
* Provided under the BSD-3 clause
|
* Provided under the BSD-3 clause
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#define _DEFAULT_SOURCE
|
||||||
|
#include <sys/stat.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <dirent.h>
|
||||||
#include "blobchain/common.h"
|
#include "blobchain/common.h"
|
||||||
|
#include "blobchain/stream.h"
|
||||||
|
|
||||||
|
/* Input directory path */
|
||||||
|
static char *input_dir = NULL;
|
||||||
|
|
||||||
static void
|
static void
|
||||||
help(void)
|
help(void)
|
||||||
@@ -13,6 +22,7 @@ help(void)
|
|||||||
printf("usage: ./blobchain <flags>\n");
|
printf("usage: ./blobchain <flags>\n");
|
||||||
printf("... [-h] Display this help menu\n");
|
printf("... [-h] Display this help menu\n");
|
||||||
printf("... [-v] Display the version\n");
|
printf("... [-v] Display the version\n");
|
||||||
|
printf("... [-i] Input directory\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@@ -21,6 +31,60 @@ version(void)
|
|||||||
printf("Version v%s\n", BLOBCHAIN_VERSION);
|
printf("Version v%s\n", BLOBCHAIN_VERSION);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
pack_foreach(const char *input_dir)
|
||||||
|
{
|
||||||
|
char pathbuf[512];
|
||||||
|
struct dirent *dirent;
|
||||||
|
DIR *dir;
|
||||||
|
|
||||||
|
if (input_dir == NULL) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ((dir = opendir(input_dir)) == NULL) {
|
||||||
|
perror("opendir");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
while ((dirent = readdir(dir)) != NULL) {
|
||||||
|
if (dirent->d_name[0] == '.') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (dirent->d_type) {
|
||||||
|
case DT_DIR:
|
||||||
|
snprintf(pathbuf, sizeof(pathbuf), "%s/%s", input_dir, dirent->d_name);
|
||||||
|
pack_foreach(pathbuf);
|
||||||
|
printf("[d] %s\n", pathbuf);
|
||||||
|
break;
|
||||||
|
case DT_REG:
|
||||||
|
snprintf(pathbuf, sizeof(pathbuf), "%s/%s", input_dir, dirent->d_name);
|
||||||
|
printf("[r] %s\n", pathbuf);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
pack_dir(void)
|
||||||
|
{
|
||||||
|
struct stat sb;
|
||||||
|
|
||||||
|
if (stat(input_dir, &sb) < 0) {
|
||||||
|
perror("stat");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Must be a directory */
|
||||||
|
if (!S_ISDIR(sb.st_mode)) {
|
||||||
|
printf("fatal: given path is not a directory!\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
pack_foreach(input_dir);
|
||||||
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
main(int argc, char **argv)
|
main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
@@ -32,7 +96,7 @@ main(int argc, char **argv)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
while ((opt = getopt(argc, argv, "hv")) != -1) {
|
while ((opt = getopt(argc, argv, "hvi:")) != -1) {
|
||||||
switch (opt) {
|
switch (opt) {
|
||||||
case 'h':
|
case 'h':
|
||||||
help();
|
help();
|
||||||
@@ -40,8 +104,24 @@ main(int argc, char **argv)
|
|||||||
case 'v':
|
case 'v':
|
||||||
version();
|
version();
|
||||||
return -1;
|
return -1;
|
||||||
|
case 'i':
|
||||||
|
input_dir = strdup(optarg);
|
||||||
|
if (input_dir == NULL) {
|
||||||
|
printf("fatal: out of memory\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (input_dir == NULL) {
|
||||||
|
printf("fatal: expected input directory\n");
|
||||||
|
help();
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
pack_dir();
|
||||||
|
free(input_dir);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user