00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 static int convert_old_files(char *path, char *newfiledb)
00039 {
00040 FILE *f, *fdb;
00041 char *s, *fn, *nick, *tm, *s1;
00042 filedb_entry *fdbe = NULL;
00043 int in_file = 0, i;
00044 struct stat st;
00045
00046 s = nmalloc(strlen(path) + 8);
00047 sprintf(s, "%s/.files", path);
00048 f = fopen(s, "r");
00049 my_free(s);
00050 if (f == NULL)
00051 return 0;
00052
00053 fdb = fopen(newfiledb, "w+b");
00054 if (!fdb) {
00055 putlog(LOG_MISC, "(!) Can't create filedb in %s", newfiledb);
00056 fclose(f);
00057 return 0;
00058 }
00059 lockfile(fdb);
00060 lockfile(f);
00061 filedb_initdb(fdb);
00062
00063 putlog(LOG_FILES, "*", FILES_CONVERT, path);
00064
00065 while (!feof(f)) {
00066 s = nmalloc(121);
00067 s1 = s;
00068 fgets(s, 120, f);
00069 if (s[strlen(s) - 1] == '\n')
00070 s[strlen(s) - 1] = 0;
00071 if (!feof(f)) {
00072 fn = newsplit(&s1);
00073 rmspace(fn);
00074 if ((fn[0]) && (fn[0] != ';') && (fn[0] != '#')) {
00075
00076 if (fn[0] == '-') {
00077
00078 if (in_file && fdbe) {
00079 rmspace(s);
00080 if (fdbe->desc) {
00081 fdbe->desc = nrealloc(fdbe->desc,
00082 strlen(fdbe->desc) + strlen(s) + 2);
00083 strcat(fdbe->desc, "\n");
00084 } else
00085 fdbe->desc = nmalloc(strlen(s) + 2);
00086 strcat(fdbe->desc, s);
00087 }
00088 } else {
00089 if (fdbe) {
00090
00091 filedb_addfile(fdb, fdbe);
00092 free_fdbe(&fdbe);
00093 }
00094 fdbe = malloc_fdbe();
00095 in_file = 1;
00096 nick = newsplit(&s1);
00097 rmspace(nick);
00098 tm = newsplit(&s1);
00099 rmspace(tm);
00100 rmspace(s1);
00101 i = strlen(fn) - 1;
00102 if (fn[i] == '/')
00103 fn[i] = 0;
00104 malloc_strcpy(fdbe->filename, fn);
00105 malloc_strcpy(fdbe->uploader, nick);
00106 fdbe->gots = atoi(s1);
00107 fdbe->uploaded = atoi(tm);
00108 sprintf(s, "%s/%s", path, fn);
00109 if (stat(s, &st) == 0) {
00110
00111 if (S_ISDIR(st.st_mode)) {
00112 fdbe->stat |= FILE_DIR;
00113 if (nick[0] == '+') {
00114 char x[100];
00115
00116
00117 struct flag_record fr = { FR_GLOBAL, 0, 0, 0, 0, 0 };
00118
00119 break_down_flags(nick + 1, &fr, NULL);
00120 build_flags(x, &fr, NULL);
00121
00122 malloc_strcpy_nocheck(fdbe->flags_req, x);
00123 }
00124 }
00125 fdbe->size = st.st_size;
00126 } else
00127 in_file = 0;
00128 }
00129 }
00130 }
00131 my_free(s);
00132 }
00133 if (fdbe) {
00134
00135 filedb_addfile(fdb, fdbe);
00136 free_fdbe(&fdbe);
00137 }
00138 fseek(fdb, 0L, SEEK_END);
00139 unlockfile(f);
00140 unlockfile(fdb);
00141 fclose(fdb);
00142 fclose(f);
00143 return 1;
00144 }
00145
00146
00147
00148
00149 static void convert_version1(FILE *fdb_s, FILE *fdb_t)
00150 {
00151 filedb1 fdb1;
00152
00153 fseek(fdb_s, 0L, SEEK_SET);
00154 while (!feof(fdb_s)) {
00155 fread(&fdb1, sizeof(filedb1), 1, fdb_s);
00156 if (!feof(fdb_s)) {
00157 if (!(fdb1.stat & FILE_UNUSED)) {
00158 filedb_entry *fdbe = malloc_fdbe();
00159
00160 fdbe->stat = fdb1.stat;
00161 if (fdb1.filename[0])
00162 malloc_strcpy(fdbe->filename, fdb1.filename);
00163 if (fdb1.desc[0])
00164 malloc_strcpy(fdbe->desc, fdb1.desc);
00165 if (fdb1.uploader[0])
00166 malloc_strcpy(fdbe->uploader, fdb1.uploader);
00167 if (fdb1.flags_req[0])
00168 malloc_strcpy(fdbe->flags_req, (char *) fdb1.flags_req);
00169 fdbe->uploaded = fdb1.uploaded;
00170 fdbe->size = fdb1.size;
00171 fdbe->gots = fdb1.gots;
00172 if (fdb1.sharelink[0])
00173 malloc_strcpy(fdbe->sharelink, fdb1.sharelink);
00174 filedb_addfile(fdb_s, fdbe);
00175 free_fdbe(&fdbe);
00176 }
00177 }
00178 }
00179 }
00180
00181
00182
00183
00184 static void convert_version2(FILE *fdb_s, FILE *fdb_t)
00185 {
00186 filedb2 fdb2;
00187
00188 fseek(fdb_s, 0L, SEEK_SET);
00189 while (!feof(fdb_s)) {
00190 fread(&fdb2, sizeof(filedb2), 1, fdb_s);
00191 if (!feof(fdb_s)) {
00192 if (!(fdb2.stat & FILE_UNUSED)) {
00193 filedb_entry *fdbe = malloc_fdbe();
00194
00195 fdbe->stat = fdb2.stat;
00196 if (fdb2.filename[0])
00197 malloc_strcpy(fdbe->filename, fdb2.filename);
00198 if (fdb2.desc[0])
00199 malloc_strcpy(fdbe->desc, fdb2.desc);
00200 if (fdb2.chname[0])
00201 malloc_strcpy(fdbe->chan, fdb2.chname);
00202 if (fdb2.uploader[0])
00203 malloc_strcpy(fdbe->uploader, fdb2.uploader);
00204 if (fdb2.flags_req[0])
00205 malloc_strcpy(fdbe->flags_req, fdb2.flags_req);
00206 fdbe->uploaded = fdb2.uploaded;
00207 fdbe->size = fdb2.size;
00208 fdbe->gots = fdb2.gots;
00209 if (fdb2.sharelink[0])
00210 malloc_strcpy(fdbe->sharelink, fdb2.sharelink);
00211 filedb_addfile(fdb_t, fdbe);
00212 free_fdbe(&fdbe);
00213 }
00214 }
00215 }
00216 }
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230 static int convert_old_db(FILE ** fdb_s, char *filedb)
00231 {
00232 filedb_top fdbt;
00233 FILE *fdb_t;
00234 int ret = 0;
00235
00236 filedb_readtop(*fdb_s, &fdbt);
00237
00238 if (fdbt.version > 0 && fdbt.version < FILEDB_VERSION3) {
00239 char *tempdb;
00240
00241 putlog(LOG_MISC, "*", "Converting old filedb %s to newest format.",
00242 filedb);
00243
00244 tempdb = nmalloc(strlen(filedb) + 5);
00245 simple_sprintf(tempdb, "%s-tmp", filedb);
00246
00247 fdb_t = fopen(tempdb, "w+b");
00248 if (fdb_t) {
00249 filedb_initdb(fdb_t);
00250
00251
00252
00253
00254 if (fdbt.version == FILEDB_VERSION1)
00255 convert_version1(*fdb_s, fdb_t);
00256 else
00257 convert_version2(*fdb_s, fdb_t);
00258
00259 unlockfile(*fdb_s);
00260 fclose(fdb_t);
00261 fclose(*fdb_s);
00262
00263
00264 if (movefile(tempdb, filedb))
00265 putlog(LOG_MISC, "*", "(!) Moving file db from %s to %s failed.",
00266 tempdb, filedb);
00267
00268 *fdb_s = fopen(filedb, "r+b");
00269 if (*fdb_s) {
00270 lockfile(*fdb_s);
00271
00272
00273
00274
00275 ret = 1;
00276 } else
00277 putlog(LOG_MISC, "*", "(!) Reopening db %s failed.", filedb);
00278 }
00279 my_free(tempdb);
00280
00281 } else if (fdbt.version == FILEDB_VERSION3)
00282 ret = 1;
00283 else
00284 putlog(LOG_MISC, "*", "(!) Unknown db version: %d", fdbt.version);
00285 if (!ret)
00286 putlog(LOG_MISC, "*", "Conversion of filedb %s failed.", filedb);
00287 return ret;
00288 }