Debian Codegraph @

emscripten/importexport.c

raw
1 /**
2 * Import/export ~/.dink/ as .zip
3
4 * Copyright (C) 2018 Sylvain Beucler
5
6 * This file is part of GNU FreeDink
7
8 * GNU FreeDink is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 3 of the
11 * License, or (at your option) any later version.
12
13 * GNU FreeDink is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see
20 * <http://www.gnu.org/licenses/>.
21 */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include <zip.h>
28
29 #include <sys/types.h>
30 #include <dirent.h>
31 #include <sys/stat.h>
32 #include <errno.h>
33 #include <libgen.h>
34
35 extern int mkdir_rec(char* path);
36
37 int IsDir(char* path) {
38 struct stat st;
39 return (stat(path, &st) == 0 && S_ISDIR(st.st_mode));
40 }
41
42 int AddFileToZip(zip_t* archive, char* path, char* prefix) {
43 zip_source_t *s;
44 s = zip_source_file(archive, path, 0, -1);
45 if (s == NULL) {
46 fprintf(stderr, "error opening %s\n", path);
47 return 0;
48 }
49 char* zippath = path + strlen(prefix);
50 if (zip_file_add(archive, zippath, s, ZIP_FL_ENC_UTF_8) < 0) {
51 zip_source_free(s);
52 fprintf(stderr, "error adding %s: %s\n", path, zip_strerror(archive));
53 return 0;
54 }
55 return 1;
56 }
57
58 int AddPathToZip(zip_t* archive, char* path, char* prefix) {
59 //printf("processing %s\n", path);
60 struct stat st;
61 if (IsDir(path)) {
62 DIR* dir = opendir(path);
63 if (dir == NULL) {
64 perror("opendir");
65 return 0;
66 }
67 struct dirent *ent;
68 while ((ent = readdir(dir)) != NULL) {
69 //printf("dir: %s\n", ent->d_name);
70 if (strcmp(ent->d_name, ".") == 0) continue;
71 if (strcmp(ent->d_name, "..") == 0) continue;
72 char* subpath = malloc(strlen(path) + 1 + strlen(ent->d_name) + 1);
73 sprintf(subpath, "%s/%s", path, ent->d_name);
74 AddPathToZip(archive, subpath, prefix);
75 free(subpath);
76 }
77 closedir(dir);
78 } else {
79 AddFileToZip(archive, path, prefix);
80 }
81 return 1;
82 }
83
84 int emSavegamesExport(void) {
85 int errorp;
86 zip_t* archive = zip_open("savegames.zip", ZIP_CREATE|ZIP_TRUNCATE|ZIP_CHECKCONS, &errorp);
87 if (archive == NULL) {
88 zip_error_t error;
89 zip_error_init_with_code(&error, errorp);
90 fprintf(stderr, "error creating savegames.zip: %s\n", zip_error_strerror(&error));
91 zip_error_fini(&error);
92 return 0;
93 }
94
95 char* path = malloc(strlen(getenv("HOME"))+1+strlen(".dink")+1);
96 sprintf(path, "%s/%s", getenv("HOME"), ".dink");
97 char* prefix = malloc(strlen(getenv("HOME"))+1+strlen(".dink")+1+1);
98 sprintf(prefix, "%s/%s/", getenv("HOME"), ".dink");
99 AddPathToZip(archive, path, prefix);
100 free(path);
101
102 if (zip_get_num_entries(archive, 0) == 0) {
103 printf("No savegames found!\n");
104 return 0;
105 } else if (zip_close(archive) < 0) {
106 fprintf(stderr, "cannot write savegames.zip: %s\n", zip_strerror(archive));
107 return 0;
108 }
109
110 chmod("savegames.zip", 00666);
111 return 1;
112 }
113
114 void emSavegamesImport(void) {
115 int errorp;
116 zip_t* archive = zip_open("/savegames.zip", ZIP_RDONLY, &errorp);
117 if (archive == NULL) {
118 zip_error_t error;
119 zip_error_init_with_code(&error, errorp);
120 fprintf(stderr, "error opening savegames.zip: %s\n", zip_error_strerror(&error));
121 zip_error_fini(&error);
122 return;
123 }
124
125 char* prefix = malloc(strlen(getenv("HOME"))+1+strlen(".dink")+1+1);
126 sprintf(prefix, "%s/%s/", getenv("HOME"), ".dink");
127 for (int i = 0; i < zip_get_num_entries(archive, 0); i++) {
128 zip_stat_t sb;
129 zip_stat_index(archive, i, 0, &sb);
130 if (!(sb.valid & ZIP_STAT_NAME)
131 || !(sb.valid & ZIP_STAT_SIZE)) {
132 continue;
133 }
134
135 if (sb.name[strlen(sb.name)-1] == '/') {
136 // will recursively create directories later
137 continue;
138 }
139
140 char* destFullPath = malloc(strlen(prefix)+strlen(sb.name)+1);
141 sprintf(destFullPath, "%s%s", prefix, sb.name);
142 char* dir = strdup(destFullPath);
143 dirname(dir);
144 int ret = mkdir_rec(dir);
145 if (ret < 0) printf("Error creating directory %s\n", dir);
146 free(dir);
147 if (ret < 0)
148 continue;
149
150 zip_file_t* file;
151 if ((file = zip_fopen_index(archive, i, 0)) == NULL) {
152 fprintf(stderr, "error opening file %d: %s\n", i, zip_strerror(archive));
153 continue;
154 }
155 FILE* out;
156 if ((out = fopen(destFullPath, "wb")) == NULL) {
157 perror("fopen");
158 continue;
159 }
160
161 unsigned char buf[4096]; // size from emscripten implementation
162 zip_uint64_t total = 0;
163 while (total < sb.size) {
164 zip_int64_t nb_read;
165 if ((nb_read = zip_fread(file, buf, sizeof(buf))) < 0) {
166 fprintf(stderr, "error reading %s: %s\n", sb.name, zip_file_strerror(file));
167 continue;
168 }
169 fwrite(buf, nb_read, 1, out);
170 total += nb_read;
171 }
172 fclose(out);
173 zip_fclose(file);
174 free(destFullPath);
175 }
176 }
177
178 #ifdef TEST
179 int main(void) {
180 return !emSavegamesExport();
181 }
182 #endif