Annotation of parser3/src/classes/file.C, revision 1.13

1.1       paf         1: /*
                      2:        Parser
                      3:        Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
                      4:        Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
                      5: 
1.13    ! paf         6:        $Id: file.C,v 1.12 2001/03/28 14:41:27 paf Exp $
1.1       paf         7: */
                      8: 
                      9: #include "pa_request.h"
                     10: #include "_file.h"
                     11: #include "pa_vfile.h"
1.11      paf        12: #include "pa_table.h"
1.1       paf        13: 
1.9       paf        14: // consts
                     15: 
                     16: const int FIND_MONKEY_MAX_HOPS=10;
                     17: 
1.1       paf        18: // global var
                     19: 
1.3       paf        20: VStateless_class *file_class;
1.1       paf        21: 
                     22: // methods
                     23: 
1.7       paf        24: /// @test mkdirs
1.1       paf        25: static void _save(Request& r, const String& method_name, Array *params) {
1.13    ! paf        26:        if(r.self==file_class)
        !            27:                RTHROW(0, 0,
        !            28:                        &method_name,
        !            29:                        "method of 'file' is not static");
        !            30: 
1.4       paf        31:        Pool& pool=r.pool();
1.8       paf        32:        Value& vfile_name=*static_cast<Value *>(params->get(0));
1.1       paf        33:        // forcing
1.4       paf        34:        // ^save[this body type]
1.8       paf        35:        r.fail_if_junction_(true, vfile_name, 
1.4       paf        36:                method_name, "file name must not be junction");
                     37: 
                     38:        // forcing untaint language
                     39:        String lfile_name(pool);
1.8       paf        40:        lfile_name.append(vfile_name.as_string(),
1.5       paf        41:                String::UL_FILE_NAME, true);
1.7       paf        42: 
                     43:        // save
                     44:        static_cast<VFile *>(r.self)->save(r.absolute(lfile_name));
                     45: }
                     46: 
                     47: static void _delete(Request& r, const String& method_name, Array *params) {
                     48:        Pool& pool=r.pool();
1.8       paf        49:        Value& vfile_name=*static_cast<Value *>(params->get(0));
1.7       paf        50:        // forcing
                     51:        // ^delete[this body type]
1.8       paf        52:        r.fail_if_junction_(true, vfile_name, 
1.7       paf        53:                method_name, "file name must not be junction");
                     54: 
                     55:        // forcing untaint language
                     56:        String lfile_name(pool);
1.8       paf        57:        lfile_name.append(vfile_name.as_string(),
1.7       paf        58:                String::UL_FILE_NAME, true);
1.4       paf        59:                
1.7       paf        60:        // unlink
                     61:        file_delete(pool, r.absolute(lfile_name));
1.1       paf        62: }
                     63: 
1.8       paf        64: static void _find(Request& r, const String& method_name, Array *params) {
                     65:        Pool& pool=r.pool();
                     66:        Value& vfile_name=*static_cast<Value *>(params->get(0));
                     67:        // forcing
                     68:        // ^delete[this body type]
                     69:        r.fail_if_junction_(true, vfile_name, 
                     70:                method_name, "file name must not be junction");
                     71: 
                     72:        // forcing untaint language
                     73:        String lfile_name(pool);
                     74:        lfile_name.append(vfile_name.as_string(),
                     75:                String::UL_FILE_NAME, true);
                     76: 
                     77:        // passed file name simply exists in current dir
                     78:        if(file_readable(r.absolute(lfile_name))) {
                     79:                r.write_no_lang(*new(pool) VString(lfile_name));
                     80:                return;
                     81:        }
                     82: 
                     83:        // scan .. dirs for result
1.9       paf        84:        for(int i=0; i<FIND_MONKEY_MAX_HOPS; i++) {
1.8       paf        85:                String test_name(pool);
                     86:                for(int j=0; j<i; j++)
                     87:                        test_name.APPEND_CONST("../");
                     88:                test_name.append(lfile_name, String::UL_NO);
                     89:                if(file_readable(r.absolute(test_name))) {
                     90:                        r.write_no_lang(*new(pool) VString(*new(pool) String(test_name)));
                     91:                        return;
                     92:                }
                     93:        }
                     94: 
                     95:        // not found
                     96:        if(params->size()==2) {
                     97:                // forcing ..{this body type}
                     98:                Value& not_found_code=*static_cast<Value *>(params->get(1));
                     99:                r.fail_if_junction_(false, not_found_code, 
                    100:                        method_name, "not-found param must be junction");
                    101:                r.write_pass_lang(r.process(not_found_code));
                    102:        }
                    103: }
                    104: 
1.9       paf       105: static void _load(Request& r, const String& method_name, Array *params) {
1.13    ! paf       106:        if(r.self==file_class)
        !           107:                RTHROW(0, 0,
        !           108:                        &method_name,
        !           109:                        "method of 'file' is not static");
        !           110: 
1.9       paf       111:        Pool& pool=r.pool();
                    112:        Value& vfile_name=*static_cast<Value *>(params->get(0));
                    113: 
1.10      paf       114:        // forcing ^load[this body type]
1.9       paf       115:        r.fail_if_junction_(true, vfile_name, 
                    116:                method_name, "file name must not be junction");
                    117: 
                    118:        // forcing untaint language
                    119:        String lfile_name(pool);
                    120:        lfile_name.append(vfile_name.as_string(),
                    121:                String::UL_FILE_NAME, true);
                    122: 
1.12      paf       123:        void *data;  size_t size;
1.9       paf       124:        file_read(pool, r.absolute(lfile_name), data, size, false/*binary*/);
                    125: 
                    126:        char *user_file_name=params->size()==1?lfile_name.cstr()
                    127:                :static_cast<Value *>(params->get(1))->as_string().cstr();
1.10      paf       128:        
1.11      paf       129:        const String *mime_type=0;
                    130:        if(params->size()==3)
                    131:                mime_type=&static_cast<Value *>(params->get(2))->as_string();
                    132:        else {
                    133:                if(r.mime_types) {
                    134:                        if(char *cext=strrchr(user_file_name, '.')) {
                    135:                                cext++;
                    136:                                String sext(pool, cext);
                    137:                                if(r.mime_types->locate(0, sext))
                    138:                                        if(!(mime_type=r.mime_types->item(1)))
                    139:                                                PTHROW(0, 0,
1.12      paf       140:                                                r.mime_types->origin_string(),
                    141:                                                "MIME-TYPE table column elements must not be empty");
1.11      paf       142:                        }
                    143:                }
                    144:        }
1.12      paf       145: 
1.11      paf       146:        if(!mime_type)
                    147:                mime_type=new(pool) String(pool, "application/octet-stream");
                    148: 
                    149:        static_cast<VFile *>(r.self)->set(data, size, user_file_name, mime_type);
1.9       paf       150: }
                    151: 
1.1       paf       152: // initialize
                    153: 
1.3       paf       154: void initialize_file_class(Pool& pool, VStateless_class& vclass) {
1.1       paf       155:        // ^save[file-name]
                    156:        vclass.add_native_method("save", _save, 1, 1);
1.7       paf       157: 
                    158:        // ^delete[file-name]
                    159:        vclass.add_native_method("delete", _delete, 1, 1);
1.8       paf       160: 
                    161:        // ^find[file-name]
                    162:        // ^find[file-name]{when-not-found}
                    163:        vclass.add_native_method("find", _find, 1, 2);
1.9       paf       164: 
                    165:        // ^load[disk-name]
                    166:        // ^load[disk-name;user-name]
1.11      paf       167:        // ^load[disk-name;user-name;mime-type]
                    168:        vclass.add_native_method("load", _load, 1, 3);
1.1       paf       169: }

E-mail: