Allow specifying full file name (including the suffix)
Showing
1 changed file
with
72 additions
and
36 deletions
... | @@ -76,6 +76,41 @@ mu_check_perm (const char *name, int mode) | ... | @@ -76,6 +76,41 @@ mu_check_perm (const char *name, int mode) |
76 | return 0; | 76 | return 0; |
77 | } | 77 | } |
78 | 78 | ||
79 | static char * | ||
80 | strip_suffix (const char *name, const char *suffix) | ||
81 | { | ||
82 | int nlen = strlen (name); | ||
83 | int slen = strlen (suffix); | ||
84 | char *p; | ||
85 | |||
86 | if (nlen > slen && strcmp (name + nlen - slen, suffix) == 0) | ||
87 | { | ||
88 | p = xmalloc (nlen - slen + 1); | ||
89 | memcpy (p, name, nlen - slen); | ||
90 | p[nlen - slen] = 0; | ||
91 | } | ||
92 | else | ||
93 | p = xstrdup (name); | ||
94 | return p; | ||
95 | } | ||
96 | |||
97 | static char * | ||
98 | make_db_name (const char *name, const char *suffix) | ||
99 | { | ||
100 | int nlen = strlen (name); | ||
101 | int slen = strlen (suffix); | ||
102 | char *p; | ||
103 | |||
104 | if (nlen > slen && strcmp (name + nlen - slen, suffix) == 0) | ||
105 | p = xstrdup (name); | ||
106 | else | ||
107 | { | ||
108 | p = xmalloc (strlen (name) + slen + 1); | ||
109 | strcat (strcpy (p, name), suffix); | ||
110 | } | ||
111 | return p; | ||
112 | } | ||
113 | |||
79 | #if defined(WITH_GDBM) | 114 | #if defined(WITH_GDBM) |
80 | 115 | ||
81 | #define DB_SUFFIX ".db" | 116 | #define DB_SUFFIX ".db" |
... | @@ -84,8 +119,7 @@ int | ... | @@ -84,8 +119,7 @@ int |
84 | mu_dbm_stat (char *name, struct stat *sb) | 119 | mu_dbm_stat (char *name, struct stat *sb) |
85 | { | 120 | { |
86 | int rc; | 121 | int rc; |
87 | char *pfname = xmalloc (strlen (name) + sizeof DB_SUFFIX); | 122 | char *pfname = make_db_name (name, DB_SUFFIX); |
88 | strcat (strcpy (pfname, name), DB_SUFFIX); | ||
89 | rc = stat (pfname, sb); | 123 | rc = stat (pfname, sb); |
90 | free (pfname); | 124 | free (pfname); |
91 | return rc; | 125 | return rc; |
... | @@ -95,10 +129,8 @@ int | ... | @@ -95,10 +129,8 @@ int |
95 | mu_dbm_open (char *name, DBM_FILE *db, int flags, int mode) | 129 | mu_dbm_open (char *name, DBM_FILE *db, int flags, int mode) |
96 | { | 130 | { |
97 | int f; | 131 | int f; |
98 | char *pfname; | 132 | char *pfname = make_db_name (name, DB_SUFFIX); |
99 | 133 | ||
100 | pfname = xmalloc (strlen (name) + sizeof DB_SUFFIX); | ||
101 | strcat (strcpy (pfname, name), DB_SUFFIX); | ||
102 | if (mu_check_perm (pfname, mode)) | 134 | if (mu_check_perm (pfname, mode)) |
103 | { | 135 | { |
104 | free (pfname); | 136 | free (pfname); |
... | @@ -173,8 +205,7 @@ int | ... | @@ -173,8 +205,7 @@ int |
173 | mu_dbm_stat (char *name, struct stat *sb) | 205 | mu_dbm_stat (char *name, struct stat *sb) |
174 | { | 206 | { |
175 | int rc; | 207 | int rc; |
176 | char *pfname = xmalloc (strlen (name) + sizeof DB_SUFFIX); | 208 | char *pfname = make_db_name (name, DB_SUFFIX); |
177 | strcat (strcpy (pfname, name), DB_SUFFIX); | ||
178 | rc = stat (pfname, sb); | 209 | rc = stat (pfname, sb); |
179 | free (pfname); | 210 | free (pfname); |
180 | return rc; | 211 | return rc; |
... | @@ -185,10 +216,8 @@ mu_dbm_open (char *name, DBM_FILE *dbm, int flags, int mode) | ... | @@ -185,10 +216,8 @@ mu_dbm_open (char *name, DBM_FILE *dbm, int flags, int mode) |
185 | { | 216 | { |
186 | int f, rc; | 217 | int f, rc; |
187 | DB *db; | 218 | DB *db; |
188 | char *pfname; | 219 | char *pfname = make_db_name (name, DB_SUFFIX); |
189 | 220 | ||
190 | pfname = xmalloc (strlen (name) + sizeof DB_SUFFIX); | ||
191 | strcat (strcpy (pfname, name), DB_SUFFIX); | ||
192 | if (mu_check_perm (pfname, mode)) | 221 | if (mu_check_perm (pfname, mode)) |
193 | { | 222 | { |
194 | free (pfname); | 223 | free (pfname); |
... | @@ -315,8 +344,7 @@ int | ... | @@ -315,8 +344,7 @@ int |
315 | mu_dbm_stat (char *name, struct stat *sb) | 344 | mu_dbm_stat (char *name, struct stat *sb) |
316 | { | 345 | { |
317 | int rc; | 346 | int rc; |
318 | char *pfname = xmalloc (strlen (name) + sizeof DB_SUFFIX); | 347 | char *pfname = make_db_name (name, DB_SUFFIX); |
319 | strcat (strcpy (pfname, name), DB_SUFFIX); | ||
320 | rc = stat (pfname, sb); | 348 | rc = stat (pfname, sb); |
321 | free (pfname); | 349 | free (pfname); |
322 | return rc; | 350 | return rc; |
... | @@ -326,6 +354,7 @@ int | ... | @@ -326,6 +354,7 @@ int |
326 | mu_dbm_open (char *name, DBM_FILE *db, int flags, int mode) | 354 | mu_dbm_open (char *name, DBM_FILE *db, int flags, int mode) |
327 | { | 355 | { |
328 | int f; | 356 | int f; |
357 | char *pfname; | ||
329 | 358 | ||
330 | switch (flags) | 359 | switch (flags) |
331 | { | 360 | { |
... | @@ -342,7 +371,9 @@ mu_dbm_open (char *name, DBM_FILE *db, int flags, int mode) | ... | @@ -342,7 +371,9 @@ mu_dbm_open (char *name, DBM_FILE *db, int flags, int mode) |
342 | errno = EINVAL; | 371 | errno = EINVAL; |
343 | return -1; | 372 | return -1; |
344 | } | 373 | } |
345 | *db = dbm_open (name, f, mode); | 374 | pfname = strip_suffix (name, DB_SUFFIX); |
375 | *db = dbm_open (pfname, f, mode); | ||
376 | free (pfname); | ||
346 | if (!*db) | 377 | if (!*db) |
347 | return -1; | 378 | return -1; |
348 | 379 | ||
... | @@ -359,14 +390,14 @@ mu_dbm_open (char *name, DBM_FILE *db, int flags, int mode) | ... | @@ -359,14 +390,14 @@ mu_dbm_open (char *name, DBM_FILE *db, int flags, int mode) |
359 | int | 390 | int |
360 | mu_dbm_close (DBM_FILE db) | 391 | mu_dbm_close (DBM_FILE db) |
361 | { | 392 | { |
362 | dbm_close(db); | 393 | dbm_close (db); |
363 | return 0; | 394 | return 0; |
364 | } | 395 | } |
365 | 396 | ||
366 | int | 397 | int |
367 | mu_dbm_fetch (DBM_FILE db, DBM_DATUM key, DBM_DATUM *ret) | 398 | mu_dbm_fetch (DBM_FILE db, DBM_DATUM key, DBM_DATUM *ret) |
368 | { | 399 | { |
369 | *ret = dbm_fetch(db, key); | 400 | *ret = dbm_fetch (db, key); |
370 | return ret->dptr == NULL; | 401 | return ret->dptr == NULL; |
371 | } | 402 | } |
372 | 403 | ||
... | @@ -379,7 +410,7 @@ mu_dbm_delete (DBM_FILE db, DBM_DATUM key) | ... | @@ -379,7 +410,7 @@ mu_dbm_delete (DBM_FILE db, DBM_DATUM key) |
379 | int | 410 | int |
380 | mu_dbm_insert (DBM_FILE db, DBM_DATUM key, DBM_DATUM contents, int replace) | 411 | mu_dbm_insert (DBM_FILE db, DBM_DATUM key, DBM_DATUM contents, int replace) |
381 | { | 412 | { |
382 | return dbm_store(db, key, contents, replace ? DBM_REPLACE : DBM_INSERT); | 413 | return dbm_store (db, key, contents, replace ? DBM_REPLACE : DBM_INSERT); |
383 | } | 414 | } |
384 | 415 | ||
385 | DBM_DATUM | 416 | DBM_DATUM |
... | @@ -402,8 +433,7 @@ int | ... | @@ -402,8 +433,7 @@ int |
402 | mu_dbm_stat (char *name, struct stat *sb) | 433 | mu_dbm_stat (char *name, struct stat *sb) |
403 | { | 434 | { |
404 | int rc; | 435 | int rc; |
405 | char *pfname = xmalloc (strlen (name) + sizeof DB_SUFFIX); | 436 | char *pfname = make_db_name (name, DB_SUFFIX); |
406 | strcat (strcpy (pfname, name), DB_SUFFIX); | ||
407 | rc = stat (pfname, sb); | 437 | rc = stat (pfname, sb); |
408 | free (pfname); | 438 | free (pfname); |
409 | return rc; | 439 | return rc; |
... | @@ -414,7 +444,8 @@ int | ... | @@ -414,7 +444,8 @@ int |
414 | mu_dbm_open (char *name, DBM_FILE *db, int flags, int mode) | 444 | mu_dbm_open (char *name, DBM_FILE *db, int flags, int mode) |
415 | { | 445 | { |
416 | int f; | 446 | int f; |
417 | 447 | char *pfname; | |
448 | |||
418 | switch (flags) | 449 | switch (flags) |
419 | { | 450 | { |
420 | case MU_STREAM_CREAT: | 451 | case MU_STREAM_CREAT: |
... | @@ -429,37 +460,42 @@ mu_dbm_open (char *name, DBM_FILE *db, int flags, int mode) | ... | @@ -429,37 +460,42 @@ mu_dbm_open (char *name, DBM_FILE *db, int flags, int mode) |
429 | default: | 460 | default: |
430 | return -1; | 461 | return -1; |
431 | } | 462 | } |
432 | 463 | pfname = strip_suffix (name, DB_SUFFIX); | |
433 | if (f & O_CREAT) | 464 | if (f & O_CREAT) |
434 | { | 465 | { |
435 | char *p; | 466 | char *p; |
436 | int fd; | 467 | int fd; |
437 | 468 | ||
438 | p = xmalloc(strlen(name)+5); | 469 | p = make_db_name (pfname, ".pag"); |
439 | strcat(strcpy(p, name), ".pag"); | 470 | fd = open (p, f, mode); |
440 | fd = open(p, f, mode); | 471 | free (p); |
441 | free(p); | ||
442 | if (fd < 0) | 472 | if (fd < 0) |
443 | return -1; | 473 | { |
474 | free (pfname); | ||
475 | return -1; | ||
476 | } | ||
444 | close(fd); | 477 | close(fd); |
445 | 478 | ||
446 | p = xmalloc(strlen(name)+5); | 479 | p = make_db_name (pfname, ".dir"); |
447 | strcat(strcpy(p, name), ".dir"); | 480 | fd = open (p, f, mode); |
448 | fd = open(p, f, mode); | 481 | free (p); |
449 | free(p); | ||
450 | if (fd < 0) | 482 | if (fd < 0) |
451 | return -1; | 483 | { |
452 | close(fd); | 484 | free (pfname); |
485 | return -1; | ||
486 | } | ||
487 | close (fd); | ||
453 | } | 488 | } |
454 | 489 | rc = dbminit (pfname); | |
455 | return dbminit(name); | 490 | free (pfname); |
491 | return rc; | ||
456 | } | 492 | } |
457 | 493 | ||
458 | /*ARGSUSED*/ | 494 | /*ARGSUSED*/ |
459 | int | 495 | int |
460 | mu_dbm_close (DBM_FILE db) | 496 | mu_dbm_close (DBM_FILE db) |
461 | { | 497 | { |
462 | dbmclose(); | 498 | dbmclose (); |
463 | return 0; | 499 | return 0; |
464 | } | 500 | } |
465 | 501 | ||
... | @@ -467,7 +503,7 @@ mu_dbm_close (DBM_FILE db) | ... | @@ -467,7 +503,7 @@ mu_dbm_close (DBM_FILE db) |
467 | int | 503 | int |
468 | mu_dbm_fetch (DBM_FILE db, DBM_DATUM key, DBM_DATUM *ret) | 504 | mu_dbm_fetch (DBM_FILE db, DBM_DATUM key, DBM_DATUM *ret) |
469 | { | 505 | { |
470 | *ret = fetch(key); | 506 | *ret = fetch (key); |
471 | return ret->dptr == NULL; | 507 | return ret->dptr == NULL; |
472 | } | 508 | } |
473 | 509 | ||
... | @@ -480,7 +516,7 @@ mu_dbm_delete (DBM_FILE db, DBM_DATUM key) | ... | @@ -480,7 +516,7 @@ mu_dbm_delete (DBM_FILE db, DBM_DATUM key) |
480 | int | 516 | int |
481 | mu_dbm_insert (DBM_FILE db, DBM_DATUM key, DBM_DATUM contents, int replace) | 517 | mu_dbm_insert (DBM_FILE db, DBM_DATUM key, DBM_DATUM contents, int replace) |
482 | { | 518 | { |
483 | return store(key, contents); | 519 | return store (key, contents); |
484 | } | 520 | } |
485 | 521 | ||
486 | DBM_DATUM | 522 | DBM_DATUM | ... | ... |
-
Please register or sign in to post a comment