Annotation of uae/src/fsdb.c, revision 1.1.1.3

1.1       root        1:  /*
                      2:   * UAE - The Un*x Amiga Emulator
                      3:   *
                      4:   * Library of functions to make emulated filesystem as independent as
                      5:   * possible of the host filesystem's capabilities.
                      6:   *
                      7:   * Copyright 1999 Bernd Schmidt
                      8:   */
                      9: 
                     10: #include "sysconfig.h"
                     11: #include "sysdeps.h"
                     12: 
                     13: #include "config.h"
                     14: #include "threaddep/penguin.h"
                     15: #include "options.h"
                     16: #include "uae.h"
                     17: #include "memory.h"
                     18: #include "custom.h"
                     19: #include "newcpu.h"
                     20: #include "filesys.h"
                     21: #include "autoconf.h"
                     22: #include "compiler.h"
                     23: #include "fsusage.h"
                     24: #include "native2amiga.h"
                     25: #include "scsidev.h"
                     26: #include "fsdb.h"
                     27: 
                     28: /* The on-disk format is as follows:
                     29:  * Offset 0, 1 byte, valid
                     30:  * Offset 1, 4 bytes, mode
                     31:  * Offset 5, 257 bytes, aname
                     32:  * Offset 263, 257 bytes, nname
                     33:  * Offset 518, 81 bytes, comment
                     34:  */
                     35: 
                     36: char *nname_begin (char *nname)
                     37: {
                     38:     char *p = strrchr (nname, FSDB_DIR_SEPARATOR);
                     39:     if (p)
                     40:        return p + 1;
                     41:     return nname;
                     42: }
                     43: 
                     44: /* Find the name REL in directory DIRNAME.  If we find a file that
                     45:  * has exactly the same name, return REL.  If we find a file that
                     46:  * has the same name when compared case-insensitively, return a
                     47:  * malloced string that contains the name we found.  If no file
                     48:  * exists that compares equal to REL, return 0.  */
                     49: char *fsdb_search_dir (const char *dirname, char *rel)
                     50: {
                     51:     char *p = 0;
                     52:     struct dirent *de;
                     53:     DIR *dir = opendir (dirname);
                     54: 
                     55:     /* This really shouldn't happen...  */
                     56:     if (! dir)
                     57:        return 0;
                     58:     
                     59:     while (p == 0 && (de = readdir (dir)) != 0) {
                     60:        if (strcmp (de->d_name, rel) == 0)
                     61:            p = rel;
                     62:        else if (strcasecmp (de->d_name, rel) == 0)
                     63:            p = my_strdup (de->d_name);
                     64:     }
                     65:     closedir (dir);
                     66:     return p;
                     67: }
                     68: 
                     69: static FILE *get_fsdb (a_inode *dir, const char *mode)
                     70: {
                     71:     char *n = build_nname (dir->nname, FSDB_FILE);
                     72:     FILE *f = fopen (n, mode);
                     73:     free (n);
                     74:     return f;
                     75: }
                     76: 
                     77: /* Prune the db file the first time this directory is opened in a session.  */
                     78: void fsdb_clean_dir (a_inode *dir)
                     79: {
                     80:     char buf[1 + 4 + 257 + 257 + 81];
                     81:     char *n = build_nname (dir->nname, FSDB_FILE);
                     82:     FILE *f = fopen (n, "r+b");
                     83:     off_t pos1 = 0, pos2 = 0;
                     84: 
                     85:     if (f == 0)
                     86:        return;
                     87:     for (;; pos2 += sizeof buf) {
                     88:        if (fread (buf, 1, sizeof buf, f) < sizeof buf)
                     89:            break;
                     90:        if (buf[0] == 0)
                     91:            continue;
                     92:        if (pos1 != pos2) {
                     93:            fseek (f, pos1, SEEK_SET);
                     94:            fwrite (buf, 1, sizeof buf, f);
                     95:            fseek (f, pos2 + sizeof buf, SEEK_SET);
                     96:        }
                     97:        pos1 += sizeof buf;
                     98:     }
                     99:     fclose (f);
                    100:     truncate (n, pos1);
                    101:     free (n);
                    102: }
                    103: 
                    104: static a_inode *aino_from_buf (a_inode *base, char *buf)
                    105: {
                    106:     uae_u32 mode;
                    107:     a_inode *aino = (a_inode *) xmalloc (sizeof (a_inode));
                    108: 
                    109:     mode = do_get_mem_long ((uae_u32 *)(buf + 1));
                    110:     buf += 5;
                    111:     aino->aname = my_strdup (buf);
                    112:     buf += 257;
                    113:     aino->nname = build_nname (base->nname, buf);
                    114:     buf += 257;
                    115:     aino->comment = *buf != '\0' ? my_strdup (buf) : 0;
                    116:     fsdb_fill_file_attrs (aino);
                    117:     aino->amigaos_mode = mode;
                    118:     aino->has_dbentry = 1;
                    119:     aino->dirty = 0;
                    120:     return aino;
                    121: }
                    122: 
                    123: a_inode *fsdb_lookup_aino_aname (a_inode *base, const char *aname)
                    124: {
                    125:     FILE *f = get_fsdb (base, "rb");
                    126:     if (f == 0)
                    127:        return 0;
                    128:     for (;;) {
                    129:        char buf[1 + 4 + 257 + 257 + 81];
                    130:        if (fread (buf, 1, sizeof buf, f) < sizeof buf)
1.1.1.2   root      131:            break;
1.1       root      132:        if (buf[0] == 0)
                    133:            continue;
                    134:        if (same_aname (buf + 5, aname)) {
                    135:            fclose (f);
                    136:            return aino_from_buf (base, buf);
                    137:        }
                    138:     }
                    139:     fclose (f);
                    140:     return 0;
                    141: }
                    142: 
                    143: a_inode *fsdb_lookup_aino_nname (a_inode *base, const char *nname)
                    144: {
                    145:     char buf[1 + 4 + 257 + 257 + 81];
                    146:     FILE *f = get_fsdb (base, "rb");
                    147:     if (f == 0)
                    148:        return 0;
                    149:     for (;;) {
                    150:        if (fread (buf, 1, sizeof buf, f) < sizeof buf)
1.1.1.2   root      151:            break;
1.1       root      152:        if (buf[0] == 0)
                    153:            continue;
                    154:        if (strcmp (buf + 5 + 257, nname) == 0) {
                    155:            fclose (f);
                    156:            return aino_from_buf (base, buf);
                    157:        }
                    158:     }
                    159:     fclose (f);
                    160:     return 0;
                    161: }
                    162: 
                    163: int fsdb_used_as_nname (a_inode *base, const char *nname)
                    164: {
                    165:     char buf[1 + 4 + 257 + 257 + 81];
                    166:     FILE *f = get_fsdb (base, "rb");
                    167:     if (f == 0)
                    168:        return 0;
                    169:     for (;;) {
                    170:        if (fread (buf, 1, sizeof buf, f) < sizeof buf)
1.1.1.2   root      171:            break;
1.1       root      172:        if (buf[0] == 0)
                    173:            continue;
                    174:        if (strcmp (buf + 5 + 257, nname) == 0) {
                    175:            fclose (f);
                    176:            return 1;
                    177:        }
                    178:     }
                    179:     fclose (f);
                    180:     return 0;
                    181: }
                    182: 
                    183: static int needs_dbentry (a_inode *aino)
                    184: {
                    185:     const char *an_begin, *nn_begin;
                    186: 
                    187:     if (aino->deleted)
                    188:        return 0;
                    189:     
                    190:     if (! fsdb_mode_representable_p (aino) || aino->comment != 0)
                    191:        return 1;
                    192: 
                    193:     nn_begin = nname_begin (aino->nname);
                    194:     return strcmp (nn_begin, aino->aname) != 0;
                    195: }
                    196: 
                    197: static void write_aino (FILE *f, a_inode *aino)
                    198: {
                    199:     char buf[1 + 4 + 257 + 257 + 81];
                    200:     buf[0] = aino->needs_dbentry;
                    201:     do_put_mem_long ((uae_u32 *)(buf + 1), aino->amigaos_mode);
                    202:     strncpy (buf + 5, aino->aname, 256);
                    203:     buf[5 + 256] = '\0';
                    204:     strncpy (buf + 5 + 257, nname_begin (aino->nname), 256);
                    205:     buf[5 + 257 + 256] = '\0';
                    206:     strncpy (buf + 5 + 2*257, aino->comment ? aino->comment : "", 80);
                    207:     buf[5 + 2 * 257 + 80] = '\0';
                    208:     fwrite (buf, 1, sizeof buf, f);
                    209:     aino->has_dbentry = aino->needs_dbentry;
                    210: }
                    211: 
                    212: void fsdb_dir_writeback (a_inode *dir)
                    213: {
                    214:     FILE *f;
                    215:     int changes_needed = 0;
                    216:     a_inode *aino;
                    217: 
                    218:     /* First pass: clear dirty bits where unnecessary, and see if any work
                    219:      * needs to be done.  */
                    220:     for (aino = dir->child; aino; aino = aino->sibling) {
                    221:        if (! aino->dirty)
                    222:            continue;
                    223:        aino->needs_dbentry = needs_dbentry (aino);
1.1.1.2   root      224:        if (! aino->needs_dbentry)
1.1       root      225:            aino->dirty = 0;
1.1.1.2   root      226:        else
                    227:            changes_needed = 1;
1.1       root      228:     }
                    229:     if (! changes_needed)
                    230:        return;
                    231: 
                    232:     f = get_fsdb (dir, "r+b");
                    233:     if (f == 0) {
                    234:        f = get_fsdb (dir, "w+b");
                    235:        if (f == 0)
                    236:            /* This shouldn't happen... */
                    237:            return;
                    238:     }
                    239: 
                    240:     for (aino = dir->child; aino; aino = aino->sibling) {
                    241:        off_t pos;
                    242: 
                    243:        if (! aino->dirty)
                    244:            continue;
                    245:        aino->dirty = 0;
                    246:        if (! aino->has_dbentry) {
                    247:            fseek (f, 0, SEEK_END);
                    248:            write_aino (f, aino);
                    249:            continue;
                    250:        }
                    251: 
                    252:        fseek (f, 0, SEEK_SET);
                    253:        pos = 0;
                    254:        for (;;) {
                    255:            char buf[1 + 4 + 257 + 257 + 81];
                    256:            if (fread (buf, 1, sizeof buf, f) < sizeof buf)
                    257:                /* Can't happen... */
                    258:                break;
                    259:            if (strcmp (buf + 5, aino->aname) == 0) {
                    260:                fseek (f, pos, SEEK_SET);
                    261:                write_aino (f, aino);
                    262:                break;
                    263:            }
                    264:            pos += sizeof buf;
                    265:        }
                    266:     }
                    267:     fclose (f);
                    268: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.