Skip to content

Commit

Permalink
Fix support for u0
Browse files Browse the repository at this point in the history
  • Loading branch information
NyaMisty committed May 23, 2020
1 parent 78a814a commit 5569cf4
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 21 deletions.
1 change: 0 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
27 changes: 7 additions & 20 deletions preparerootfs.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,13 @@
#include "config.h"
#include "kernel.h"
#include "vnode_utils.h"
#include "utils.h"

#include <sys/syscall.h>
#include <sys/snapshot.h>
#include <dirent.h>
#include <sys/stat.h>

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];
Expand Down Expand Up @@ -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);
Expand Down
158 changes: 158 additions & 0 deletions utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
#include <dirent.h>

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:&copyError];
if (copyError != nil) {
NSPrint(@"%@", [copyError localizedDescription]);
return 1;
}
return 0;
}*/


int copy_dir(const char *from, const char *to);

#include <sys/stat.h>

#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;
}

0 comments on commit 5569cf4

Please sign in to comment.