Annotation of 43BSDTahoe/new/xns/examples/filing_common/VPsupport.c, revision 1.1.1.1

1.1       root        1: #ifndef lint
                      2: static char *rcsid = "$Header: VPsupport.c,v 1.3 87/04/16 15:24:53 ed Exp $";
                      3: #endif lint
                      4: 
                      5: /*
                      6:  * Copyright (c) 1986, 1987 Xerox Corporation.
                      7:  */
                      8: 
                      9: /* $Log:       VPsupport.c,v $
                     10:  * Revision 1.3  87/04/16  15:24:53  ed
                     11:  * Some machines prefer large arrays to be static.
                     12:  * 
                     13:  * Revision 1.2  87/04/01  10:08:39  ed
                     14:  * Added GetSizeAttribute.
                     15:  * 
                     16:  * Revision 1.1  87/03/18  08:42:28  ed
                     17:  * Initial revision
                     18:  * 
                     19:  */
                     20: 
                     21: /*
                     22:  * support routines for dealing with Viewpoint related files on Unix
                     23:  * files are formatted as
                     24:  *     header ("viewpoint-files/xerox")
                     25:  *     int attribute_length (byte length of attribute sequence to follow)
                     26:  *     Sequence of attributes
                     27:  *     file content
                     28:  *             will be serialized data if isDirectory value is TRUE
                     29:  */
                     30: 
                     31: #include <stdio.h>
                     32: #include <sys/types.h>
                     33: #include <sys/stat.h>
                     34: #include <netns/ns.h>
                     35: #include <netns/sp.h>
                     36: #ifdef FILING4
                     37: #include "filingV4.h"
                     38: #endif FILING4
                     39: #ifdef FILING5
                     40: #include "filingV5.h"
                     41: #endif FILING5
                     42: #ifdef FILING6
                     43: #include "filingV6.h"
                     44: #endif FILING6
                     45: #ifdef FILINGSUBSET1
                     46: #include "filingsubsetV1.h"
                     47: #endif FILINGSUBSET1
                     48: #include <xnscourier/filetypes.h>
                     49: 
                     50: Boolean isValidAttr(t)
                     51: FILING_AttributeType t;
                     52: {
                     53:        /* 
                     54:         * Following are service specific and therefore irrelevant
                     55:         *      (what are 24 and 25 ??)
                     56:         */
                     57:        if ( (t == FILING_fileID) || (t == FILING_numberOfChildren) ||
                     58:                        (t == FILING_parentID) || (t == FILING_storedSize) ||
                     59:                        (t == FILING_subtreeSize) || (t == 24) || (t == 25) ) 
                     60:                        return(FALSE);
                     61: 
                     62:        /*
                     63:         * Following are retained locally and may be in conflict
                     64:         * (i.e., name/pathname/position/version) so they are in effect replaced
                     65:         */
                     66:        if ( (t == FILING_createdOn) || (t == FILING_dataSize) ||
                     67:                        (t == FILING_isDirectory) ||
                     68:                        (t == FILING_modifiedOn) || (t == FILING_name) ||
                     69:                        (t == FILING_pathname) || (t == FILING_position) ||
                     70:                        (t == FILING_type) || (t == FILING_version) )
                     71:                return(FALSE);
                     72: 
                     73:        return(TRUE);
                     74: }
                     75: 
                     76: SaveExtendedAttributes(file, attr)
                     77: FILE *file;
                     78: FILING_AttributeSequence attr;
                     79: 
                     80: {
                     81:        int i, count, pos;
                     82:        int attr_size;
                     83:        Cardinal sequence_count;
                     84:                FILING_AttributeType t;
                     85:        static Unspecified buffer[MAX_SEQUENCE_LENGTH];
                     86: 
                     87:        attr_size= sequence_count= 0;
                     88: 
                     89:        fprintf(file, VPHDR);
                     90:        attr_size= FILING_sizeof_AttributeSequence(&attr) * sizeof(Unspecified);
                     91:        fwrite(&attr_size, sizeof(attr_size), 1, file);
                     92:        fwrite(&attr.length, sizeof(Cardinal), 1, file);
                     93: 
                     94:        for ( i= 0; i < attr.length; i++ ) {
                     95:                t= attr.sequence[i].type;
                     96: 
                     97:                count= FILING_externalize_Attribute(&attr.sequence[i], buffer);
                     98:                fwrite(buffer, sizeof(Unspecified), count, file);
                     99:        }
                    100: 
                    101:        return(attr_size);
                    102: }
                    103: 
                    104: GetExtendedAttributes(file,attrptr)
                    105: FILE *file;
                    106: char **attrptr;
                    107: {
                    108:        int size, count;
                    109:        char *buffer, *malloc();
                    110:        char hdr[50];
                    111: 
                    112:        fread(hdr, sizeof(char), strlen(VPHDR), file);
                    113:        fread(&size, sizeof(size), 1, file);
                    114: 
                    115:        if ( (buffer= malloc(size)) == 0 ) {
                    116:                return(0);
                    117:        }
                    118: 
                    119:        if ( (count= fread(buffer, sizeof(char), size, file)) == 0 ) {
                    120:                return(0);
                    121:        }
                    122: 
                    123:        if ( count != size ) {
                    124:                return(0);
                    125:        }
                    126:        *attrptr= buffer;
                    127:        return(count);
                    128: }
                    129: 
                    130: FreeExtendedAttributes(attrptr)
                    131: char *attrptr;
                    132: {
                    133:        if ( attrptr != 0 )
                    134:                free(attrptr);
                    135: }
                    136: 
                    137: AddAllExtendedAttributes(file, attrseq)
                    138: FILE *file;
                    139: FILING_AttributeSequence *attrseq;
                    140: {
                    141:        int attr_size, size, index, i;
                    142:        Unspecified *buffer, *unptr;
                    143:        Cardinal sequence_count= 0;
                    144:        FILING_AttributeType t;
                    145: 
                    146:        if ( ftell(file) != 0 )
                    147:                rewind(file);                   /* to be sure */
                    148:        
                    149:        if ( (attr_size= GetExtendedAttributes(file, &buffer)) == 0 ) {
                    150:                return(0);
                    151:        }
                    152: 
                    153:        unptr= (Unspecified *) buffer;
                    154:        sequence_count= *unptr;
                    155:        unptr ++;
                    156: 
                    157:        index= attrseq->length;
                    158: 
                    159:        for ( i= 0; i < sequence_count ; i++ ) {
                    160:                size= FILING_internalize_Attribute(&(attrseq->sequence[index]), unptr);
                    161:                t= attrseq->sequence[index].type;
                    162:                if ( isValidAttr(t) )
                    163:                        index++;
                    164: 
                    165:                unptr += size;
                    166:        }
                    167: 
                    168:        attrseq->length= index;
                    169: 
                    170:        FreeExtendedAttributes(buffer);
                    171:        return(-1);
                    172: }
                    173: 
                    174: AddExtendedStoreAttributes(file, attrseq)
                    175: FILE *file;
                    176: FILING_AttributeSequence *attrseq;
                    177: 
                    178: {
                    179:         int attr_size, size, index, i;
                    180:         Unspecified *buffer, *unptr;
                    181:        Cardinal sequence_count= 0;
                    182:        FILING_AttributeType t;
                    183: 
                    184:        if ( ftell(file) != 0 )
                    185:                rewind(file);                           /* to be sure */
                    186: 
                    187:         if ( (attr_size= GetExtendedAttributes(file,&buffer)) == 0 )
                    188:                 return(0);
                    189: 
                    190:         unptr= (Unspecified *) buffer;
                    191:        sequence_count= *unptr;
                    192:        unptr++;
                    193: 
                    194:         index= attrseq->length;
                    195: 
                    196:        for ( i= 0; i < sequence_count; i++ ) {
                    197:                 size= FILING_internalize_Attribute(&(attrseq->sequence[index]), unptr);
                    198:                t= attrseq->sequence[index].type;
                    199: 
                    200:                if ( (isValidAttr(t)) && (t != FILING_modifiedBy) &&
                    201:                                (t != FILING_readBy) && (t != FILING_readOn) &&
                    202:                                (t != FILING_type) ) {
                    203:                        index++;
                    204:                 }
                    205:                 unptr+= size;
                    206:         }
                    207: 
                    208:         attrseq->length= index;
                    209: 
                    210:         FreeExtendedAttributes(buffer);
                    211:         return(-1);
                    212: }
                    213: 
                    214: AddExtendedDeserializeAttributes(file, attrseq)
                    215: FILE *file;
                    216: FILING_AttributeSequence *attrseq;
                    217: 
                    218: {
                    219:         int attr_size, size, index, i;
                    220:         Unspecified *buffer, *unptr;
                    221:        Cardinal sequence_count= 0;
                    222:        FILING_AttributeType t;
                    223: 
                    224:        if ( ftell(file) != 0 )
                    225:                rewind(file);                           /* to be sure */
                    226: 
                    227:         if ( (attr_size= GetExtendedAttributes(file,&buffer)) == 0 )
                    228:                 return(0);
                    229: 
                    230:         unptr= buffer;
                    231:        sequence_count= *unptr;
                    232:        unptr++;
                    233: 
                    234:         index= attrseq->length;
                    235: 
                    236:        for ( i= 0; i < sequence_count; i++ ) {
                    237:                 size= FILING_internalize_Attribute(&(attrseq->sequence[index]), unptr);
                    238:                t= attrseq->sequence[index].type;
                    239:         /*
                    240:          * omit attributes which are illegal on Deserialize
                    241:          */
                    242: 
                    243:                 if ( (isValidAttr(t)) && (t != FILING_checksum) &&
                    244:                                 (t != FILING_childrenUniquelyNamed) &&
                    245:                                 (t != FILING_createdBy) &&
                    246:                                 (t != FILING_modifiedBy) &&
                    247:                                 (t != FILING_ordering) &&
                    248:                                (t != FILING_readBy) &&
                    249:                                 (t != FILING_readOn) ) {
                    250:                         index++;
                    251:                 }
                    252:                 unptr+= size;
                    253:         }
                    254: 
                    255:         attrseq->length= index;
                    256: 
                    257:         FreeExtendedAttributes(buffer);
                    258:         return(-1);
                    259: }
                    260: 
                    261: Boolean GetDirectoryAttribute(file)
                    262: FILE *file;
                    263: 
                    264: {
                    265:        int i;
                    266:        Unspecified *buffer, *unptr;
                    267:        int attr_size;
                    268:        Boolean dirval;
                    269:        Cardinal sequence_count= 0;
                    270:        FILING_Attribute attribute;
                    271: 
                    272:        dirval= FALSE;
                    273: 
                    274:        if ( ftell(file) != 0 )
                    275:                rewind(file);                   /* back to beginning */
                    276: 
                    277:        if ( (attr_size= GetExtendedAttributes(file,&buffer)) == 0 ) {
                    278:                return(FALSE);
                    279:        }
                    280: 
                    281:        unptr= buffer;
                    282:        sequence_count= *unptr;
                    283:        unptr++;
                    284: 
                    285:        for ( i= 0; i < sequence_count; i++ ){
                    286:                unptr+= FILING_internalize_Attribute(&attribute, unptr);
                    287:                if ( attribute.type == FILING_isDirectory ) {
                    288:                        dirval= AttrToBoolean(&attribute);
                    289:                        break;
                    290:                }
                    291:        }
                    292: 
                    293:        FreeExtendedAttributes(buffer);
                    294:        return(dirval);
                    295: }
                    296: 
                    297: LongCardinal GetTypeAttribute(file)
                    298: FILE *file;
                    299: 
                    300: {
                    301:        int i;
                    302:        Unspecified *buffer, *unptr;
                    303:        LongCardinal filetype, attr_size;
                    304:        FILING_Attribute attribute;
                    305:        Cardinal sequence_count= 0;
                    306: 
                    307:        filetype= TYPE_I;
                    308: 
                    309:        if ( ftell(file) != 0 )
                    310:                rewind(file);                   /* back to beginning */
                    311: 
                    312:        if ( (attr_size= GetExtendedAttributes(file, &buffer)) == 0 ) {
                    313:                return(TYPE_I);
                    314:        }
                    315: 
                    316:        unptr= buffer;
                    317:        sequence_count= *unptr;
                    318:        unptr++;
                    319: 
                    320:        for ( i= 0; i < sequence_count; i++ ) {
                    321:                unptr+= FILING_internalize_Attribute(&attribute, unptr);
                    322:                if ( attribute.type == FILING_type ) {
                    323:                        filetype= AttrToLongCardinal(&attribute);
                    324:                        break;
                    325:                }
                    326:        }
                    327: 
                    328:        FreeExtendedAttributes(buffer);
                    329:        return(filetype);
                    330: }
                    331: 
                    332: LongCardinal GetSizeAttribute(file)
                    333: FILE *file;
                    334: 
                    335: {
                    336:        int i;
                    337:        Unspecified *buffer, *unptr;
                    338:        LongCardinal filesize, attr_size;
                    339:        FILING_Attribute attribute;
                    340:        Cardinal sequence_count= 0;
                    341: 
                    342:        filesize= 0;
                    343: 
                    344:        if ( ftell(file) != 0 )
                    345:                rewind(file);                   /* back to beginning */
                    346: 
                    347:        if ( (attr_size= GetExtendedAttributes(file, &buffer)) == 0 ) {
                    348:                return(filesize);
                    349:        }
                    350: 
                    351:        unptr= buffer;
                    352:        sequence_count= *unptr;
                    353:        unptr++;
                    354: 
                    355:        for ( i= 0; i < sequence_count; i++ ) {
                    356:                unptr+= FILING_internalize_Attribute(&attribute, unptr);
                    357:                if ( attribute.type == FILING_dataSize ) {
                    358:                        filesize= AttrToLongCardinal(&attribute);
                    359:                        break;
                    360:                }
                    361:        }
                    362: 
                    363:        FreeExtendedAttributes(buffer);
                    364:        return(filesize);
                    365: }
                    366: 
                    367: PositionAfterExtendedAttributes(file)
                    368: FILE *file;
                    369: {
                    370:        int size;
                    371: 
                    372:        fseek(file, strlen(VPHDR), 0);
                    373:        fread(&size, sizeof(size), 1, file);
                    374:        return (fseek(file, (long) size, 1));
                    375: 
                    376: }

unix.superglobalmegacorp.com

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