forked from luigoalma/ctrcdnfetch
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSharedStorage.cpp
199 lines (147 loc) · 3.56 KB
/
SharedStorage.cpp
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
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <errno.h>
#include "SharedStorage.hpp"
#include "DirectoryManagement.hpp"
namespace
{
struct storage
{
const char* const path;
int getfullpath(char*& fullpath) const noexcept
{
const char* home_dir = getenv("HOME");
if (!home_dir) return errno = EADDRNOTAVAIL;
size_t length = snprintf(NULL, 0, path, home_dir) + 1;
fullpath = (char *)malloc(length);
if (!fullpath) return errno = ENOMEM;
snprintf(fullpath, length, path, home_dir);
return errno = 0;
}
int checkpath() const noexcept
{
char* fullpath = NULL;
int ret = 0;
if ((ret = getfullpath(fullpath)) != 0) return errno = ret;
ret = Utils::DirectoryManagement::PathIsDirectory(fullpath);
free(fullpath);
return errno = ret;
}
int createpath() const noexcept
{
char* fullpath = NULL;
int err = 0;
if ((err = getfullpath(fullpath)) != 0) return errno = err;
err = Utils::DirectoryManagement::MakeDirectory(fullpath);
free(fullpath);
return errno = err;
}
};
static const struct storage storages[] =
{
{ "%s/.3ds" },
{ "%s/3ds" }
};
}
int NintendoData::SharedStorage::Load(FILE*& out, const char* file) noexcept
{
if (!file) return errno = EFAULT;
if (!file[0]) return errno = EINVAL;
out = NULL;
bool storage_found = false;
int err = 0;
int i = 0;
int numofstorages = sizeof(storages) / sizeof(struct storage);
while (true)
{
for (; i < numofstorages; i++)
if ((err = storages[i].checkpath()) == 0) break;
if (i >= numofstorages) break;
storage_found = true;
char* fullstoragepath = NULL;
if ((err = storages[i].getfullpath(fullstoragepath)) != 0)
continue;
size_t length = snprintf(NULL, 0, "%s/%s", fullstoragepath, file) + 1;
char* fullpath = (char *)malloc(length);
if (!fullpath)
{
free(fullstoragepath);
err = ENOMEM;
continue;
}
snprintf(fullpath, length, "%s/%s", fullstoragepath, file);
free(fullstoragepath);
out = fopen(fullpath, "rb");
if (!out)
{
err = errno;
i++;
};
free(fullpath);
if (out)
{
err = 0;
break;
}
}
if (!storage_found)
{
for (i = 0; i < numofstorages; i++)
if (!storages[i].createpath()) break;
}
return errno = err;
}
int NintendoData::SharedStorage::Save(const void* in, size_t inlen, const char* file) noexcept
{
if (!file || (!in && inlen)) return errno = EFAULT;
if (!file[0]) return errno = EINVAL;
int err = 0;
int i;
int numofstorages = sizeof(storages) / sizeof(struct storage);
for (i = 0; i < numofstorages; i++)
if ((err = storages[i].checkpath()) == 0) break;
//failed, no storages.
if (i >= numofstorages)
{
for (i = 0; i < numofstorages; i++)
if ((err = storages[i].createpath()) == 0) break;
//failed, again?
if (i >= numofstorages) return errno = err;
}
err = 0;
char* storagepath = NULL;
if ((err = storages[i].getfullpath(storagepath)) != 0)
return errno = err;
if ((err = Utils::DirectoryManagement::MakeDirectory(storagepath)) != 0)
{
free(storagepath);
return errno = err;
}
size_t fullpath_len = strlen(storagepath) + strlen(file) + 2;
char *fullpath = (char *) malloc(fullpath_len);
if (!fullpath)
{
free(storagepath);
return errno = ENOMEM;
}
snprintf(fullpath, fullpath_len, "%s/%s", storagepath, file);
free(storagepath);
FILE* fp = fopen(fullpath, "wb");
if (!fp) return errno;
if (inlen)
{
fwrite(in, inlen, 1, fp);
if (fflush(fp) != 0)
{
err = errno;
fclose(fp);
remove(fullpath);
free(fullpath);
return errno = err;
}
}
fclose(fp);
free(fullpath);
return errno = 0;
}