diff --git a/Makefile b/Makefile index 2f39e02..07f2bda 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,6 @@ LIB_DIR := lib preparerootfs_FILES = preparerootfs.m preparerootfs_CFLAGS = $(CFLAGS) -fobjc-arc -Wno-error=unused-variable -Wno-error=unused-function - changerootfs_FILES = changerootfs.m changerootfs_CFLAGS = $(CFLAGS) -fobjc-arc -Wno-error=unused-variable -Wno-error=unused-function diff --git a/preparerootfs.m b/preparerootfs.m index 8ed72de..cfa3856 100644 --- a/preparerootfs.m +++ b/preparerootfs.m @@ -5,29 +5,13 @@ #include "config.h" #include "kernel.h" #include "vnode_utils.h" +#include "utils.h" #include #include #include #include -bool is_empty(const char* path){ - - DIR* dir = opendir(path); - struct dirent* ent; - int count = 0; - - while ((ent = readdir(dir)) != NULL) { - count++; - } - - if(count == 2){ - return YES; - }else{ - return NO; - } - -} void hardlink_var(const char *path) { char src[1024]; @@ -163,15 +147,18 @@ int link_folders() { }*/ //forceWritablePath(FAKEROOTDIR); - printf("Making final fakevar dir: %s\n", FINAL_FAKEVARDIR); + /*printf("Making final fakevar dir: %s\n", FINAL_FAKEVARDIR); if (mkdir(FINAL_FAKEVARDIR, 0755)) { return 1; - } + }*/ //printf("Copyiny fakevar dir from: %s\n", FAKEVAR_TMPMOUNT); //system("cp -r -a "FAKEVAR_TMPMOUNT"/* "FINAL_FAKEVARDIR"/"); printf("Copyiny fakevar dir from: %s\n", FAKEVARDIR); - system("cp -r "FAKEVARDIR"/* "FINAL_FAKEVARDIR"/"); + //system("cp -r "FAKEVARDIR"/* "FINAL_FAKEVARDIR"/"); + if (copy_dir(FAKEVARDIR, FINAL_FAKEVARDIR)) { + return 1; + } printf("Linking fakevar dir!\n"); listdir(FINAL_FAKEVARDIR, 0); diff --git a/utils.h b/utils.h new file mode 100644 index 0000000..e38bed3 --- /dev/null +++ b/utils.h @@ -0,0 +1,158 @@ +#include + +bool is_empty(const char* path){ + + DIR* dir = opendir(path); + struct dirent* ent; + int count = 0; + + while ((ent = readdir(dir)) != NULL) { + count++; + } + + if(count == 2){ + return YES; + }else{ + return NO; + } + +} + +static void NSPrint(NSString *format, ...) + { + va_list args; + + va_start(args, format); + NSString *string = [[NSString alloc] initWithFormat:format arguments:args]; + va_end(args); + + fprintf(stdout, "%s\n", [string UTF8String]); + +#if !__has_feature(objc_arc) + [string release]; +#endif +} +/* +int copy_dir(const char *from, const char *to) { + NSError *copyError = nil; + [[NSFileManager defaultManager] + copyItemAtPath:[NSString stringWithUTF8String:from] + toPath:[NSString stringWithUTF8String:to] error:©Error]; + + if (copyError != nil) { + NSPrint(@"%@", [copyError localizedDescription]); + return 1; + } + return 0; +}*/ + + +int copy_dir(const char *from, const char *to); + +#include + +#define BUFFERSIZE 0x4000 +#define COPYMORE 0644 + +int file_exists(const char* filename){ + struct stat buffer; + int exist = stat(filename,&buffer); + if(exist == 0) + return 1; + else // -1 + return 0; +} + +void oops(const char *msg, char *s2) { + fprintf(stderr, "%s", msg); + perror(s2); +} + +int copyFiles(char *source, char *destination) +{ + int in_fd, out_fd, n_chars; + char buf[BUFFERSIZE]; + + if ((in_fd = open(source, O_RDONLY)) == -1) + { + oops("Cannot open ", source); + return 1; + } + + if ((out_fd = creat(destination, COPYMORE)) == -1) + { + oops("Cannot creat ", destination); + return 1; + } + + while ((n_chars = read(in_fd, buf, BUFFERSIZE)) > 0) + { + if (write(out_fd, buf, n_chars) != n_chars) + { + oops("Write error to ", destination); + return 1; + } + + if (n_chars == -1) + { + oops("Read error from ", source); + return 1; + } + } + + if (close(in_fd) == -1 || close(out_fd) == -1) + { + oops("Error closing files", ""); + return 1; + } + + return 0; +} + +int copy_dir(const char *name, const char *target) +{ + DIR *dir; + struct dirent *entry; + + if (!(dir = opendir(name))) + return 1; + + mkdir(target, 0755); + char path[1024]; + char targetpath[1024]; + int child = 0; + while ((entry = readdir(dir)) != NULL) + { + snprintf(path, sizeof(path), "%s/%s", name, entry->d_name); + snprintf(targetpath, sizeof(targetpath), "%s/%s", target, entry->d_name); + if (entry->d_type == DT_DIR) + if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) + continue; + printf("Copying: %s...", path); + child++; + if (entry->d_type == DT_DIR) + { + if (mkdir(targetpath, 0755)) { + perror("create folder"); + } + if (!file_exists(targetpath)) { + printf("Failed\n"); + continue; + } + printf("\n"); + copy_dir(path, targetpath); + } + else + { + if (copyFiles(path, targetpath) != 0) { + printf("Failed\n"); + } + printf("\n"); + } + } + closedir(dir); + if (child == 0) { + return 1; + } + return 0; +}