-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopy.c
106 lines (101 loc) · 2.3 KB
/
copy.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
#include "config.h"
#include <pspkernel.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "common/utils.h"
#include "copy.h"
extern bool copy_file(const char * src, const char * dest,t_copy_cb cb, t_copy_overwritecb ocb, void * data)
{
byte * buf = (byte *)malloc(1024 * 1024);
if(buf == NULL)
return false;
int fd2;
if(ocb != NULL)
{
fd2 = sceIoOpen(dest, PSP_O_RDONLY, 0777);
if(fd2 >= 0)
{
if(!ocb(dest, data))
{
sceIoClose(fd2);
return false;
}
sceIoClose(fd2);
}
}
int fd1 = sceIoOpen(src, PSP_O_RDONLY, 0777);
if(fd1 < 0)
{
if(cb != NULL)
cb(src, dest, false, data);
return false;
}
fd2 = sceIoOpen(dest, PSP_O_CREAT | PSP_O_RDWR, 0777);
if(fd2 < 0)
{
if(cb != NULL)
cb(src, dest, false, data);
sceIoClose(fd1);
return false;
}
int readbytes;
while((readbytes = sceIoRead(fd1, buf, 1024 * 1024)) > 0)
if(sceIoWrite(fd2, buf, readbytes) != readbytes)
{
if(cb != NULL)
cb(src, dest, false, data);
sceIoClose(fd1);
sceIoClose(fd2);
return true;
}
free((void *)buf);
if(cb != NULL)
cb(src, dest, true, data);
sceIoClose(fd1);
sceIoClose(fd2);
return true;
}
extern dword copy_dir(const char * src, const char * dest,t_copy_cb cb, t_copy_overwritecb ocb, void * data)
{
int dl = sceIoDopen(src);
if(dl < 0)
{
if(cb != NULL)
cb(src, dest, false, data);
return 0;
}
sceIoMkdir(dest, 0777);
dword result = 0;
SceIoDirent sid;
memset(&sid, 0, sizeof(SceIoDirent));
while(sceIoDread(dl, &sid))
{
if(sid.d_name[0] == '.') continue;
char copysrc[260], copydest[260];
sprintf(copysrc, "%s/%s", src, sid.d_name);
sprintf(copydest, "%s/%s", dest, sid.d_name);
if(FIO_S_ISDIR(sid.d_stat.st_mode))
{
result += copy_dir(copysrc, copydest, cb, ocb, data);
continue;
}
if(copy_file(copysrc, copydest, cb, ocb, data))
++ result;
memset(&sid, 0, sizeof(SceIoDirent));
}
sceIoDclose(dl);
return result;
}
extern bool move_file(const char * src, const char * dest,t_copy_cb cb, t_copy_overwritecb ocb, void * data)
{
bool result = copy_file(src, dest, cb, ocb, data);
utils_del_file(src);
return result;
}
extern dword move_dir(const char * src, const char * dest,t_copy_cb cb, t_copy_overwritecb ocb, void * data)
{
dword result = copy_dir(src, dest, cb, ocb, data);
utils_del_dir(src);
return result;
}