-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfimexu.c
201 lines (182 loc) · 4.92 KB
/
fimexu.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include <stdio.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/stat.h>
#include "coverett.h"
#include "devices/file_import_export.h"
int main(int argc, char* argv[]){
if (argc < 2){
puts("File IMport/EXport Utility");
puts("Usage: fimexu [-i or -e] <filename>");
puts(" -i - Import file (you can save it by custom filename).");
puts(" -e - Export file.");
return 0;
}
bus_t stdbus = openBus("/dev/hvc0");
device_t dev = findDev(stdbus, "file_import_export");
if (!dev.exists){
fputs("This program requires a File Import/Export Card.\n", stderr);
return -1;
}
resetTransfer(&dev);
char* lowarg;
if (strlen(argv[1]) > 1){
lowarg = strdup(argv[1]);
lowarg[1] = tolower(lowarg[1]);
}
if (strcmp(lowarg, "-i") == 0){
result_t status = requestImportFile(&dev);
if (status.type == CO_BOOLEAN && !status.retNumber){
fputs("No users present to request file from.\n", stderr);
return -1;
}
puts("Waiting for file...");
fileinfo_t fil;
int gotfile = 0;
while (!gotfile){
fil = beginImportFile(&dev);
if (fil.type == CO_ERROR){
if (strcmp(fil.errString, "import was canceled") == 0){
fputs("File transfer canceled.\n", stderr);
return -1;
}
fprintf(stderr, "Error while waiting for file: %s.\n", fil.errString);
return -1;
}
else if (fil.type == CO_OK){
printf("Remote file: '%s', Size: %ld bytes.\n", fil.filename, fil.size);
gotfile = 1;
}
}
char* fname = argc > 2 ? argv[2] : fil.filename;
struct stat st;
stat(fname, &st);
if (argc > 2 && S_ISDIR(st.st_mode)){
size_t arlen = strlen(argv[2]);
fname = (char*)malloc(arlen+strlen(fil.filename)+2);
strcpy(fname, argv[2]);
if (fname[arlen-1] != '/'){
fname[arlen] = '/';
fname[arlen+1] = 0;
}
strcat(fname, fil.filename);
}
if (access(fname, F_OK) != -1){
printf("File '%s' exists.\n[O]verwrite/[U]se another name/[C]ancel? ", fname);
fflush(stdout);
char ans = tolower(getc(stdin));
if (ans == 'o'){
puts("Overwriting.");
}
else if (ans == 'u'){
int namsiz = 256;
char* newnam = (char*)malloc(namsiz);
printf("Enter new name: ");
fflush(stdout);
int i = 0;
getc(stdin);
char nsym = getc(stdin);
while (nsym != '\n') {
newnam[i++] = nsym;
if (i > 255){
namsiz+= 256;
char* newnam = (char*)realloc(newnam, namsiz);
}
nsym = getc(stdin);
}
newnam[i] = '\0';
if (strlen(newnam) == 0){
puts("Canceled (empty filename).");
return 0;
}
fname = newnam;
}
else{
puts("Canceled.");
return 0;
}
}
FILE* local = fopen(fname, "wb");
if (local == NULL){
perror("Error opening file");
return -1;
}
fflush(stdout);
size_t recv = 0;
result_t data = readImportFile(&dev);
if (data.type != CO_VOID){
while (data.type != CO_VOID){
if (data.type == CO_ERROR){
fclose(local);
putc('\n',stdout);
fprintf(stderr, "Error while file importing: %s.\n", data.errString);
return -1;
}
size_t wrt = fwrite(data.retString, 1, (size_t)data.retNumber, local);
if (wrt != (size_t)data.retNumber){
fclose(local);
putc('\n',stdout);
fprintf(stderr, "Error while file importing: expected %d bytes, but only %d bytes were written.", (size_t)data.retNumber, wrt);
return -1;
}
free(data.retString);
recv += (size_t)data.retNumber;
printf("\rImporting... %.2f%% (%d/%d bytes)", (double)(recv * 100) / fil.size, recv, fil.size);
fflush(stdout);
data = readImportFile(&dev);
}
}
else{
printf("Importing... 100.00%% (%d/%d bytes)", fil.size, fil.size);
}
fclose(local);
}
else if (strcmp(lowarg, "-e") == 0){
if (argc < 3){
fputs("No filename specified to export.\n", stderr);
return -1;
}
FILE* local = fopen(argv[2], "rb");
if (local == NULL){
perror("Error opening file");
return -1;
}
struct stat st;
fstat(fileno(local), &st);
if (S_ISDIR(st.st_mode)){
fputs("Error: this is a directory.\n", stderr);
return -1;
}
fflush(stdout);
beginExportFile(&dev, argv[2]);
char rbuf[500];
size_t readed = fread(rbuf, 1, 500, local);
size_t sent = 0;
if (readed != 0){
while (readed != 0){
usleep(500000);
result_t writed = writeExportFile(&dev, rbuf, readed); //ACHTUNG!!! Looks like some values changing to 0 by unknown reason!
if (writed.type == CO_ERROR) {
putc('\n',stdout);
fprintf(stderr, "Error while file exporting: %s.\n", writed.errString);
return -1;
}
sent += readed;
printf("\rExporting... %.2f%% (%d/%d bytes)", (double)(sent * 100) / st.st_size, sent, st.st_size);
fflush(stdout);
readed = fread(rbuf, 1, 500, local);
}
}
else{
printf("Exporting... 100.00%% (%d/%d bytes)", st.st_size, st.st_size);
}
fclose(local);
finishExportFile(&dev);
}
else{
fprintf(stderr, "Incorrect parameter - '%s'.\n", argv[1]);
return -1;
}
puts("\nDone!");
return 0;
}