This repository has been archived by the owner on Mar 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
161 lines (148 loc) · 3.97 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
//
// main.cpp
// microramfs
//
// Created by Andre Zay on 14/08/2019.
// Copyright © 2019 Andre Zay. All rights reserved.
//
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
#include <errno.h>
#define CMD_DIR 0x01
#define CMD_FILE 0x02
uint64_t gsz=0;
uint64_t files=0;
uint64_t dirs=0;
void _add_str(uint8_t *str, size_t sz){
size_t i=0;
for(;i<sz;++i){
if(gsz%16==0){
printf("\n");
}
printf("0x%02x,",str[i]);
++gsz;
}
}
void _add_sz(size_t sz){
uint64_t _sz=sizeof(size_t)/sizeof(uint8_t);
uint8_t *sza=(uint8_t *)malloc(sizeof(size_t));
memcpy(sza, &sz, sizeof(size_t));
uint64_t i=0;
for(;i<_sz;++i){
if(gsz%16==0){
printf("\n");
}
printf("0x%02x,",sza[i]);
++gsz;
}
}
void add_dir(char *dirname){
if(gsz%16==0){
printf("\n");
}
printf("0x%02x,",CMD_DIR);
++gsz;
uint64_t sz=(strlen(dirname)+1)*sizeof(char);
_add_sz(sz);
_add_str((uint8_t*)dirname,sz);
++dirs;
}
void add_file(char *path,uint8_t *content, size_t sz_c){
if(gsz%16==0){
printf("\n");
}
printf("0x%02x,",CMD_FILE);
++gsz;
uint64_t sz_p=(strlen(path)+1)*sizeof(char);
_add_sz(sz_p);
_add_str((uint8_t*)path,sz_p);
_add_sz(sz_c);
_add_str((uint8_t*)content,sz_c+sizeof(char));
++files;
}
void listdir(const char *name)
{
DIR *dir;
struct dirent *entry;
if (!(dir = opendir(name)))
return;
while ((entry = readdir(dir)) != NULL) {
if (entry->d_type == DT_DIR) {
char path[1024];
if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
continue;
snprintf(path, sizeof(path), "%s/%s", name, entry->d_name);
if(!strcmp(entry->d_name, ".")){
++name;
}
char *_name=(char *)malloc(sizeof(char)*(strlen(entry->d_name)+strlen(name)+2));
char *__name=_name;
strcpy(_name, name);
strcat(_name,"/");
strcat(_name, entry->d_name);
if(!strcmp(_name,"./")){
strcpy(_name,"/");
}else{
_name+=2*sizeof(char);
}
//printf("Creating directory %s\n",_name);
add_dir(_name);
listdir(path);
free(__name);
} else {
if(!strcmp(entry->d_name, ".DS_Store")){
continue;
}
if(!strcmp(entry->d_name, ".")){
++name;
}
char * buffer = 0;
long length;
char *_name=(char *)malloc(sizeof(char)*(strlen(entry->d_name)+strlen(name)+2));
strcpy(_name, name);
strcat(_name, "/");
strcat(_name, entry->d_name);
FILE * f = fopen (_name, "rb");
if(!strcmp(_name,"./")){
strcpy(_name,"/");
}else{
_name+=2*sizeof(char);
}
if (f)
{
fseek (f, 0, SEEK_END);
length = ftell (f);
fseek (f, 0, SEEK_SET);
buffer = (char*)malloc (length);
if (buffer)
{
fread (buffer, 1, length, f);
}
fclose (f);
}else{
printf("%s(%d)\n",strerror(errno),errno);
continue;
}
assert(buffer);
//buffer[length]=0;
add_file(_name, (uint8_t *)buffer,length);
}
}
closedir(dir);
}
int main(int argc, const char * argv[]) {
if(argc<2){
printf("Usage: %s [PATH TO RAMFS DIR]\n",argv[0]);
return -1;
}
chdir(argv[1]);
printf("{");
listdir("./");
printf("}\nTotal size: %llu bytes\nFiles count: %llu\n Dirs count:%llu\n",gsz,files,dirs);
return 0;
}