-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathfsclient.c
More file actions
351 lines (243 loc) · 11.3 KB
/
fsclient.c
File metadata and controls
351 lines (243 loc) · 11.3 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "utility.h"
#include "ps2netfs.h"
#include "network.h"
#include "fsclient.h"
char hostname[256] = { "192.168.0.10" };
int main(int argc, char **argv, char **env) { int loop0 = 0;
// Turn off stdout buffering.
setbuf(stdout, NULL);
// Parse the environment list for optional arguments.
for (loop0=0; env[loop0]; loop0++) {
// A hostname has been specified...
if (strncmp(env[loop0], "PS2HOSTNAME", 11) == 0) { strncpy(hostname, &env[loop0][12], sizeof(hostname)); }
}
// Check the number of arguments.
if (argc < 2) { print_usage(); return -1; }
// Parse the argument list for optional arguments.
for (loop0=1; argv[loop0]; loop0++) {
// If an optional hostname has been specified...
if (strncmp(argv[loop0], "-h", 2) == 0) { loop0++;
// Check to make sure the hostname value was actually supplied.
if (argc == loop0) { printf("Error: No hostname was supplied the '-h' option.\n"); print_usage(); return -1; }
// Set the hostname to the supplied value.
strncpy(hostname, argv[loop0], sizeof(hostname));
}
// Else, the end of the options has been reached...
else { break; }
}
// Increment the argument counters past any optional arguments.
loop0++; argc -= loop0; argv += loop0;
// Check to make sure a command was actually supplied.
if (argc < 0) { printf("Error: No command was supplied.\n"); print_usage(); return -1; }
#ifdef _WIN32
// Startup network, under windows.
if (network_startup() < 0) { printf("Error: Could not start up winsock.\n"); return 1; }
#endif
// Connect to the ps2netfs server.
if (ps2netfs_connect(hostname) < 0) { printf("Error: Could not connect to the ps2netfs server. (%s)\n", hostname); return -1; }
// Perform the requested command.
if (strcmp(argv[-1], "copyfrom") == 0) { return fsclient_copyfrom(argv[0], argv[1]); } else
if (strcmp(argv[-1], "copyto") == 0) { return fsclient_copyto(argv[0], argv[1]); } else
if (strcmp(argv[-1], "delete") == 0) { return fsclient_delete(argv[0]); } else
if (strcmp(argv[-1], "devlist") == 0) { return fsclient_devlist(); } else
if (strcmp(argv[-1], "dir") == 0) { return fsclient_dir(argv[0]); } else
if (strcmp(argv[-1], "mkdir") == 0) { return fsclient_mkdir(argv[0]); } else
if (strcmp(argv[-1], "mount") == 0) { return fsclient_mount(argv[0], argv[1]); } else
if (strcmp(argv[-1], "rmdir") == 0) { return fsclient_rmdir(argv[0]); } else
if (strcmp(argv[-1], "sync") == 0) { return fsclient_sync(argv[0]); } else
if (strcmp(argv[-1], "umount") == 0) { return fsclient_umount(argv[0]); } else
// An unknown command was requested.
{ printf("Error: Unknown command requested. (%s)\n", argv[-1]); print_usage(); return -1; }
// Disconnect from the ps2netfs server.
if (ps2netfs_disconnect() < 0) { printf("Error: Could not disconnect from the ps2netfs server. (%s)\n", hostname); return -1; }
// End program.
return 0;
}
////////////////////////
// FSCLIENT FUNCTIONS //
////////////////////////
int fsclient_copyfrom(char *source, char *destination) { int result = 0;
int fd0, fd1, size, total = 0; char buffer[28000];
// Open the source file.
fd0 = ps2netfs_command_open(source, OPEN_READ);
if (fd0 < 0) { printf("Error: Open source file failed. (%d)\n", fd0); return -1; }
// Get the source file size.
size = ps2netfs_command_lseek(fd0, 0, LSEEK_END); ps2netfs_command_lseek(fd0, 0, LSEEK_SET);
if (size < 0) { printf("Error: Get source file size failed. (%d)\n", size); return -1; }
// Open the destination file.
fd1 = open(destination, O_RDWR | O_CREAT, 0644);
if (fd0 < 1) { printf("Error: Open destination file failed. (%d)\n", fd1); return -1; }
// Output the display header.
printf("\n [%s --> %s]\n\n Progress: ", source, destination);
// Repeat until the copy is finished.
while (total < size) { printf("#");
// Read the source data.
result = ps2netfs_command_read(fd0, buffer, sizeof(buffer));
if (result < 0) { printf("Error: Read source data failed. (%d)\n", result); return -1; }
// Write the destination data.
result = write(fd1, buffer, result);
if (result < 0) { printf("Error: Write destination data failed. (%d)\n", result); return -1; }
// Increment the counter.
total += result;
}
// Output the display footer.
printf("\n\n [%d Bytes Copied]\n\n", total);
// Close the source file.
result = ps2netfs_command_close(fd0);
if (result < 0) { printf("Error: Close source file failed. (%d)\n", result); return -1; }
// Close the destination file.
result = close(fd1);
if (result < 0) { printf("Error: Close destination file failed. (%d)\n", result); return -1; }
// End function.
return 0;
}
int fsclient_copyto(char *source, char *destination) { int result = 0;
int fd0, fd1, size, total = 0; char buffer[28000];
// Open the source file.
fd0 = open(source, O_RDONLY);
if (fd0 < 0) { printf("Error: Open source file failed. (%d)\n", fd0); return -1; }
// Get the source file size.
size = lseek(fd0, 0, SEEK_END); lseek(fd0, 0, SEEK_SET);
if (size < 0) { printf("Error: Get source file size failed. (%d)\n", size); return -1; }
// Open the destination file.
fd1 = ps2netfs_command_open(destination, OPEN_WRITE | OPEN_CREATE);
if (fd0 < 1) { printf("Error: Open destination file failed. (%d)\n", fd1); return -1; }
// Output the display header.
printf("\n [%s --> %s]\n\n Progress: ", source, destination);
// Repeat until the copy is finished.
while (total < size) { printf("#");
// Read the source data.
result = read(fd0, buffer, sizeof(buffer));
if (result < 0) { printf("Error: Read source data failed. (%d)\n", result); return -1; }
// Write the destination data.
result = ps2netfs_command_write(fd1, buffer, result);
if (result < 0) { printf("Error: Write destination data failed. (%d)\n", result); return -1; }
// Increment the counter.
total += result;
}
// Output the display footer.
printf("\n\n [%d Bytes Copied]\n\n", total);
// Close the source file.
result = close(fd0);
if (result < 0) { printf("Error: Close source file failed. (%d)\n", result); return -1; }
// Close the destination file.
result = ps2netfs_command_close(fd1);
if (result < 0) { printf("Error: Close destination file failed. (%d)\n", result); return -1; }
// End function.
return 0;
}
int fsclient_delete(char *pathname) { int result = 0;
// Delete the file.
result = ps2netfs_command_delete(pathname, 0);
if (result < 0) { printf("Error: Delete file failed. (%d)\n", result); return -1; }
// End function.
return 0;
}
int fsclient_devlist(void) { int result = 0;
int loop0, devcount; char devlist[256], *temp = devlist;
// Get the device listing.
devcount = ps2netfs_command_devlist("", 0, devlist);
if (devcount < 0) { printf("Error: Get device listing failed. (%d)\n", result); return -1; }
// Output the display header.
printf("\n [Active Devices]\n\n");
// Output each available device.
for(loop0=0;loop0<devcount;loop0++) {
// Output the device description.
if (!strcmp(temp, "rom")) { printf(" rom - Onboard rom device.\n"); } else
if (!strcmp(temp, "cdrom")) { printf(" cdrom - Standard cd/dvd device. (cdrom:)\n"); } else
if (!strcmp(temp, "host")) { printf(" host - Host file system. (host:)\n"); } else
if (!strcmp(temp, "mc")) { printf(" mc - Memory card driver. (mc0: mc1:)\n"); } else
if (!strcmp(temp, "hdd")) { printf(" hdd - Internal HDD unit.\n"); } else
if (!strcmp(temp, "pfs")) { printf(" pfs - Playstation File System.\n"); } else
if (!strcmp(temp, "dev9x")) { printf(" dev9x - Blah blah blah.\n"); } else
//
{ printf(" %s\n", temp); }
// Increment temp.
temp += strlen(temp) + 1;
}
// Output the display footer.
printf("\n [%d Devices Found]\n\n", devcount);
// End function.
return 0;
}
int fsclient_dir(char *pathname) { int result = 0;
int dd, files = 0, size = 0; DIRENT dirent;
// Open the directory.
dd = ps2netfs_command_dopen(pathname, 0);
if (dd < 0) { printf("Error: Open directory failed. (%d)\n", dd); return -1; }
// Output the display header.
printf("\n [Contents of %s]\n\n", pathname);
// Read in each of the entries.
while (ps2netfs_command_dread(dd, &dirent) > 0) { printf(" ");
// Output the mode information.
if (ntohl(dirent.mode) & 0x4000) { printf("l"); } else
if (ntohl(dirent.mode) & 0x2000) { printf("-"); } else
if (ntohl(dirent.mode) & 0x1000) { printf("d"); } else { printf("-"); }
if (ntohl(dirent.mode) & 0x0100) { printf("r"); } else { printf("-"); }
if (ntohl(dirent.mode) & 0x0080) { printf("w"); } else { printf("-"); }
if (ntohl(dirent.mode) & 0x0040) { printf("x"); } else { printf("-"); }
if (ntohl(dirent.mode) & 0x0020) { printf("r"); } else { printf("-"); }
if (ntohl(dirent.mode) & 0x0010) { printf("w"); } else { printf("-"); }
if (ntohl(dirent.mode) & 0x0008) { printf("x"); } else { printf("-"); }
if (ntohl(dirent.mode) & 0x0004) { printf("r"); } else { printf("-"); }
if (ntohl(dirent.mode) & 0x0002) { printf("w"); } else { printf("-"); }
if (ntohl(dirent.mode) & 0x0001) { printf("x"); } else { printf("-"); }
// Output the file size.
printf(" %10d", (int)ntohl(dirent.size));
// Output the date.
printf(" %02d-%02d-%04d", dirent.mtime[5], dirent.mtime[4], (2048 + dirent.mtime[6]));
// Output the time.
printf(" %02d:%02d:%02d", dirent.mtime[3], dirent.mtime[2], dirent.mtime[1]);
// Output the name.
printf(" %s\n", dirent.name);
// Update the counters.
files++; size += ntohl(dirent.size);
}
// Output the display footer.
printf("\n [%d Files - %d Bytes]\n\n", files, size);
// Close the directory.
result = ps2netfs_command_dclose(dd);
if (result < 0) { printf("Error: Close directory failed. (%d)\n", result); return -1; }
// End function.
return 0;
}
int fsclient_mkdir(char *pathname) { int result = 0;
// Make the directory.
result = ps2netfs_command_mkdir(pathname, 0);
if (result < 0) { printf("Error: Make directory failed. (%d)\n", result); return -1; }
// End function.
return 0;
}
int fsclient_mount(char *device, char *fsname) { int result = 0;
// Mount the device.
result = ps2netfs_command_mount(device, fsname, MOUNT_READWRITE, "", 0);
if (result < 0) { printf("Error: Mount device failed. (%d)\n", result); return -1; }
// End function.
return 0;
}
int fsclient_rmdir(char *pathname) { int result = 0;
// Remove the directory.
result = ps2netfs_command_rmdir(pathname, 0);
if (result < 0) { printf("Error: Remove directory failed. (%d)\n", result); return -1; }
// End function.
return 0;
}
int fsclient_sync(char *device) { int result = 0;
// Sync the device.
result = ps2netfs_command_sync(device, 0);
if (result < 0) { printf("Error: Sync device failed. (%d)\n", result); return -1; }
// End function.
return 0;
}
int fsclient_umount(char *device) { int result = 0;
// Umount the device.
result = ps2netfs_command_umount(device, 0);
if (result < 0) { printf("Error: Umount device failed. (%d)\n", result); return -1; }
// End function.
return 0;
}