Skip to content

Commit d776b6e

Browse files
authored
ftp improvements (#266)
* ftp improvements * Update http.c * fixes * Update exec_cmd.c
1 parent a7e108c commit d776b6e

4 files changed

Lines changed: 79 additions & 43 deletions

File tree

include/saves.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,9 @@ int ReadVmc2Codes(save_entry_t * save);
285285
int http_init(void);
286286
void http_end(void);
287287
int http_download(const char* url, const char* filename, const char* local_dst, int show_progress);
288+
void ftp_init(void);
289+
void ftp_end(void);
290+
int ftp_download(const char* url, const char* filename, const char* local_dst, int show_progress);
288291
int ftp_upload(const char* local_file, const char* url, const char* filename, int show_progress);
289292

290293
int extract_7zip(const char* zip_file, const char* dest_path);

source/exec_cmd.c

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,11 +1226,9 @@ static int _upload_save_ftp(const save_entry_t* save)
12261226

12271227
// Download existing FTP indexes
12281228
snprintf(remote, sizeof(remote), "%s%016" PRIX64 "/PS%d/", apollo_config.ftp_url, apollo_config.account_id, save->type);
1229-
http_download(remote, "games.txt", APOLLO_LOCAL_CACHE "games.ftp", 0);
1229+
ftp_download(remote, "games.txt", APOLLO_LOCAL_CACHE "games.ftp", 0);
12301230

12311231
snprintf(remote, sizeof(remote), "%s%016" PRIX64 "/PS%d/%s/", apollo_config.ftp_url, apollo_config.account_id, save->type, save->title_id);
1232-
http_download(remote, "saves.txt", APOLLO_LOCAL_CACHE "saves.ftp", 0);
1233-
http_download(remote, "checksum.sfv", APOLLO_LOCAL_CACHE "sfv.ftp", 0);
12341232

12351233
// Create zip file
12361234
snprintf(local, sizeof(local), APOLLO_LOCAL_CACHE "%s_%d-%02d-%02d-%02d%02d%02d.zip",
@@ -1272,17 +1270,17 @@ static int _upload_save_ftp(const save_entry_t* save)
12721270

12731271
// Update save index
12741272
LOG("Updating %s save index...", save->title_id);
1275-
fp = fopen(APOLLO_LOCAL_CACHE "saves.ftp", "a");
1273+
fp = fopen(APOLLO_LOCAL_CACHE "saves.ftp", "w");
12761274
if (fp)
12771275
{
1278-
fprintf(fp, "%s=[%s] %d-%02d-%02d %02d:%02d:%02d %s (CRC: %08X)\r\n", tmp, save->dir_name,
1279-
t.tm_year+1900, t.tm_mon+1, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec, save->name, crc);
1276+
fprintf(fp, "%s=[%s] %d-%02d-%02d %02d:%02d:%02d %s\r\n", tmp, save->dir_name,
1277+
t.tm_year+1900, t.tm_mon+1, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec, save->name);
12801278
fclose(fp);
12811279
}
12821280

12831281
// Update CRC index
12841282
LOG("Updating .sfv CRC32: %08X", crc);
1285-
fp = fopen(APOLLO_LOCAL_CACHE "sfv.ftp", "a");
1283+
fp = fopen(APOLLO_LOCAL_CACHE "sfv.ftp", "w");
12861284
if (fp)
12871285
{
12881286
fprintf(fp, "%s %08X\n", tmp, crc);
@@ -1291,9 +1289,6 @@ static int _upload_save_ftp(const save_entry_t* save)
12911289

12921290
// Upload zip and indexes
12931291
ret = ftp_upload(local, remote, tmp, 1);
1294-
ret &= ftp_upload(APOLLO_LOCAL_CACHE "saves.ftp", remote, "saves.txt", 1);
1295-
ret &= ftp_upload(APOLLO_LOCAL_CACHE "sfv.ftp", remote, "checksum.sfv", 1);
1296-
12971292
unlink_secure(local);
12981293

12991294
// Update games index if this title is not yet listed
@@ -1311,18 +1306,25 @@ static int _upload_save_ftp(const save_entry_t* save)
13111306
snprintf(local, sizeof(local), APOLLO_LOCAL_CACHE "%.9s.PNG", save->title_id);
13121307
ret &= ftp_upload(local, remote, "icon0.png", 1);
13131308

1314-
fp = fopen(APOLLO_LOCAL_CACHE "games.ftp", "a");
1309+
fp = fopen(APOLLO_LOCAL_CACHE "games.ftp", "w");
13151310
if (fp)
13161311
{
13171312
fprintf(fp, "%s=%s\r\n", save->title_id, tmp);
13181313
fclose(fp);
13191314
}
13201315

1321-
snprintf(remote, sizeof(remote), "%s%016" PRIX64 "/PS%d/", apollo_config.ftp_url, apollo_config.account_id, save->type);
1322-
ret &= ftp_upload(APOLLO_LOCAL_CACHE "games.ftp", remote, "games.txt", 1);
1316+
init_loading_screen(_("Sync with FTP Server..."));
1317+
snprintf(local, sizeof(local), "%s%016" PRIX64 "/PS%d/", apollo_config.ftp_url, apollo_config.account_id, save->type);
1318+
ret &= ftp_upload(APOLLO_LOCAL_CACHE "games.ftp", local, "games.txt", 0);
13231319
}
1320+
else init_loading_screen(_("Sync with FTP Server..."));
1321+
13241322
free(tmp);
13251323

1324+
ret &= ftp_upload(APOLLO_LOCAL_CACHE "saves.ftp", remote, "saves.txt", 0);
1325+
ret &= ftp_upload(APOLLO_LOCAL_CACHE "sfv.ftp", remote, "checksum.sfv", 0);
1326+
stop_loading_screen();
1327+
13261328
return ret;
13271329
}
13281330

@@ -1333,8 +1335,10 @@ static void uploadSaveFTP(const save_entry_t* save)
13331335
if (!show_dialog(DIALOG_TYPE_YESNO, _("Do you want to upload %s?"), save->dir_name))
13341336
return;
13351337

1338+
ftp_init();
13361339
ret = _upload_save_ftp(save);
13371340
clean_directory(APOLLO_LOCAL_CACHE, ".ftp");
1341+
ftp_end();
13381342

13391343
if (ret)
13401344
show_message("%s\n%s", _("Save successfully uploaded:"), save->dir_name);
@@ -1354,6 +1358,7 @@ static void uploadAllSavesFTP(const save_entry_t* save, int all)
13541358
if (!show_dialog(DIALOG_TYPE_YESNO, _("Do you want to upload the selected saves to FTP?")))
13551359
return;
13561360

1361+
ftp_init();
13571362
LOG("Uploading all saves to FTP server...");
13581363
for (node = list_head(list); (item = list_get(node)); node = list_next(node))
13591364
{
@@ -1378,6 +1383,7 @@ static void uploadAllSavesFTP(const save_entry_t* save, int all)
13781383
}
13791384

13801385
clean_directory(APOLLO_LOCAL_CACHE, ".ftp");
1386+
ftp_end();
13811387

13821388
show_message("%d/%d %s", done, done+err_count, _("Saves uploaded to FTP"));
13831389
}

source/http.c

Lines changed: 51 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
#define HTTP_FAILED 0
1515
#define HTTP_USER_AGENT "Mozilla/5.0 (PLAYSTATION 4; 1.00)"
1616

17+
static CURL *ftp_ctx = NULL;
18+
1719

1820
int http_init(void)
1921
{
@@ -87,14 +89,12 @@ static void set_curl_opts(CURL* curl)
8789
}
8890
}
8991

90-
int http_download(const char* url, const char* filename, const char* local_dst, int show_progress)
92+
static int _curl_download(CURL *curl, const char* url, const char* filename, const char* local_dst, int show_progress)
9193
{
9294
char full_url[1024];
93-
CURL *curl;
9495
CURLcode res;
9596
FILE* fd;
9697

97-
curl = curl_easy_init();
9898
if(!curl)
9999
{
100100
LOG("ERROR: CURL INIT");
@@ -104,15 +104,13 @@ int http_download(const char* url, const char* filename, const char* local_dst,
104104
fd = fopen(local_dst, "wb");
105105
if (!fd) {
106106
LOG("fopen Error: File path '%s'", local_dst);
107-
curl_easy_cleanup(curl);
108107
return HTTP_FAILED;
109108
}
110109

111110
if (!filename) filename = "";
112111
snprintf(full_url, sizeof(full_url), "%s%s", url, filename);
113112
LOG("URL: %s >> %s", full_url, local_dst);
114113

115-
set_curl_opts(curl);
116114
curl_easy_setopt(curl, CURLOPT_URL, full_url);
117115
// The function that will be used to write the data
118116
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite);
@@ -139,8 +137,6 @@ int http_download(const char* url, const char* filename, const char* local_dst,
139137

140138
// close file descriptor
141139
fclose(fd);
142-
// cleanup
143-
curl_easy_cleanup(curl);
144140

145141
if (show_progress)
146142
end_progress_bar();
@@ -155,6 +151,21 @@ int http_download(const char* url, const char* filename, const char* local_dst,
155151
return HTTP_SUCCESS;
156152
}
157153

154+
int http_download(const char* url, const char* filename, const char* local_dst, int show_progress)
155+
{
156+
int ret;
157+
CURL *curl = curl_easy_init();
158+
159+
if (!curl)
160+
return HTTP_FAILED;
161+
162+
set_curl_opts(curl);
163+
ret = _curl_download(curl, url, filename, local_dst, show_progress);
164+
curl_easy_cleanup(curl);
165+
166+
return ret;
167+
}
168+
158169
void http_end(void)
159170
{
160171
curl_global_cleanup();
@@ -164,6 +175,32 @@ void http_end(void)
164175
sceSysmoduleUnloadModuleInternal(ORBIS_SYSMODULE_INTERNAL_NET);
165176
}
166177

178+
void ftp_init(void)
179+
{
180+
if (!ftp_ctx && (ftp_ctx = curl_easy_init()))
181+
set_curl_opts(ftp_ctx);
182+
}
183+
184+
void ftp_end(void)
185+
{
186+
if (ftp_ctx)
187+
curl_easy_cleanup(ftp_ctx);
188+
189+
ftp_ctx = NULL;
190+
}
191+
192+
int ftp_download(const char* url, const char* filename, const char* local_dst, int show_progress)
193+
{
194+
if (!ftp_ctx)
195+
return HTTP_FAILED;
196+
197+
curl_easy_setopt(ftp_ctx, CURLOPT_UPLOAD, 0);
198+
curl_easy_setopt(ftp_ctx, CURLOPT_APPEND, 0);
199+
curl_easy_setopt(ftp_ctx, CURLOPT_NOPROGRESS, 1L);
200+
201+
return _curl_download(ftp_ctx, url, filename, local_dst, show_progress);
202+
}
203+
167204
int ftp_upload(const char* local_file, const char* url, const char* filename, int show_progress)
168205
{
169206
FILE *fd;
@@ -173,7 +210,7 @@ int ftp_upload(const char* local_file, const char* url, const char* filename, in
173210
unsigned long fsize;
174211

175212
/* get a curl handle */
176-
curl = curl_easy_init();
213+
curl = ftp_ctx;
177214
if(!curl)
178215
{
179216
LOG("ERROR: CURL INIT");
@@ -185,7 +222,6 @@ int ftp_upload(const char* local_file, const char* url, const char* filename, in
185222
if(!fd)
186223
{
187224
LOG("Couldn't open '%s'", local_file);
188-
curl_easy_cleanup(curl);
189225
return HTTP_FAILED;
190226
}
191227

@@ -199,25 +235,19 @@ int ftp_upload(const char* local_file, const char* url, const char* filename, in
199235
LOG("Local file size: %lu bytes.", fsize);
200236
LOG("Uploading (%s) -> (%s)", local_file, remote_url);
201237

202-
set_curl_opts(curl);
203238
/* enable uploading */
204239
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
205-
240+
curl_easy_setopt(curl, CURLOPT_APPEND, 0L);
206241
/* specify target */
207242
curl_easy_setopt(curl, CURLOPT_URL, remote_url);
208-
209243
// create missing dirs if needed
210244
curl_easy_setopt(curl, CURLOPT_FTP_CREATE_MISSING_DIRS, CURLFTP_CREATE_DIR);
211-
212245
/* please ignore the IP in the PASV response */
213246
curl_easy_setopt(curl, CURLOPT_FTP_SKIP_PASV_IP, 1L);
214-
215247
/* we want to use our own read function */
216248
curl_easy_setopt(curl, CURLOPT_READFUNCTION, fread);
217-
218249
/* now specify which file to upload */
219250
curl_easy_setopt(curl, CURLOPT_READDATA, fd);
220-
221251
/* Set the size of the file to upload (optional). */
222252
curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)fsize);
223253

@@ -229,22 +259,18 @@ int ftp_upload(const char* local_file, const char* url, const char* filename, in
229259
curl_easy_setopt(curl, CURLOPT_XFERINFODATA, (void*) filename);
230260
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0L);
231261
}
262+
else
263+
{
264+
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 1L);
265+
curl_easy_setopt(curl, CURLOPT_APPEND, 1L);
266+
}
232267

233268
/* Now run off and do what you have been told! */
234269
res = curl_easy_perform(curl);
235270

236-
if (res == CURLE_SSL_CONNECT_ERROR)
237-
{
238-
curl_easy_setopt(curl, CURLOPT_USE_SSL, CURLUSESSL_NONE);
239-
res = curl_easy_perform(curl);
240-
}
241-
242271
/* close the local file */
243272
fclose(fd);
244273

245-
/* always cleanup */
246-
curl_easy_cleanup(curl);
247-
248274
if (show_progress)
249275
end_progress_bar();
250276

source/settings.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,11 +156,10 @@ static void ftp_url_callback(int sel)
156156
strcat(apollo_config.ftp_url, "/");
157157

158158
// test the connection
159+
ftp_init();
159160
init_loading_screen(_("Testing connection..."));
160-
ret = http_download(apollo_config.ftp_url, "apollo.txt", APOLLO_LOCAL_CACHE "users.ftp", 0);
161-
data = ret ? readTextFile(APOLLO_LOCAL_CACHE "users.ftp") : NULL;
162-
if (!data)
163-
data = strdup("; Apollo Save Tool (" APOLLO_PLATFORM ") v" APOLLO_VERSION "\r\n");
161+
ret = ftp_download(apollo_config.ftp_url, "apollo.txt", APOLLO_LOCAL_CACHE "users.ftp", 0);
162+
data = ret ? readTextFile(APOLLO_LOCAL_CACHE "users.ftp") : strdup("");
164163

165164
snprintf(tmp, sizeof(tmp), "%016lX", apollo_config.account_id);
166165
if (strstr(data, tmp) == NULL)
@@ -169,14 +168,15 @@ static void ftp_url_callback(int sel)
169168
FILE* fp = fopen(APOLLO_LOCAL_CACHE "users.ftp", "w");
170169
if (fp)
171170
{
172-
fprintf(fp, "%s%s\r\n", data, tmp);
171+
fprintf(fp, "; Apollo Save Tool (" APOLLO_PLATFORM ") v" APOLLO_VERSION "\r\n%s\r\n", tmp);
173172
fclose(fp);
174173
}
175174

176175
ret = ftp_upload(APOLLO_LOCAL_CACHE "users.ftp", apollo_config.ftp_url, "apollo.txt", 0);
177176
}
178177
free(data);
179178
stop_loading_screen();
179+
ftp_end();
180180

181181
if (ret)
182182
{
@@ -405,6 +405,7 @@ int load_app_settings(app_config_t* config)
405405
memcpy(config, file_data, file_size);
406406

407407
LOG("Settings loaded: UserID (%08x) AccountID (%016lX)", config->user_id, config->account_id);
408+
LOG("PSID: %016lX%016lX", ES64(config->psid[0]), ES64(config->psid[1]));
408409
free(file_data);
409410
}
410411

0 commit comments

Comments
 (0)