Improved safety and reliability of the --add option

This commit is contained in:
Marc
2022-10-02 19:11:37 +02:00
parent 511a4ae352
commit bdb2f52d07
6 changed files with 135 additions and 45 deletions
+37 -1
View File
@@ -2,8 +2,11 @@
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
#include "file.h"
#include <stdio.h>
#include <dirent.h>
#include <sys/types.h>
#include <glib.h>
#include "file.h"
u_int32_t countSetBits(u_int32_t n) {
// base case
@@ -83,3 +86,36 @@ int move(const char * source, const char * destination) {
return returnValue;
}
}
//rename a folder and all subfolder and files to lowercase
//TODO: error handling
void casefold(const char * folder) {
DIR * d = opendir(folder);
struct dirent *dir;
if (d) {
while ((dir = readdir(d)) != NULL) {
//removes .. & . from the list
if(strcmp(dir->d_name, "..") != 0 && strcmp(dir->d_name, ".") != 0) {
//only look at ascii and hope for the best.
gchar * file = g_build_path("/", folder, dir->d_name, NULL);
gchar * destinationName = g_ascii_strdown(dir->d_name, -1);
gchar * destination = g_build_path("/", folder,destinationName, NULL);
int result = move(file, destination);
if(result != EXIT_SUCCESS) {
printf("Move failed: %s => %s \n", dir->d_name, destinationName);
}
g_free(destinationName);
g_free(destination);
if(dir->d_type == DT_DIR) {
casefold(file);
}
g_free(file);
}
}
closedir(d);
}
}