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

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.16    ! paf         6:        $Id: file.C,v 1.15 2001/04/03 05:23:36 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: 
                     24: static void _save(Request& r, const String& method_name, Array *params) {
1.4       paf        25:        Pool& pool=r.pool();
1.8       paf        26:        Value& vfile_name=*static_cast<Value *>(params->get(0));
1.1       paf        27:        // forcing
1.4       paf        28:        // ^save[this body type]
1.8       paf        29:        r.fail_if_junction_(true, vfile_name, 
1.4       paf        30:                method_name, "file name must not be junction");
                     31: 
                     32:        // forcing untaint language
                     33:        String lfile_name(pool);
1.8       paf        34:        lfile_name.append(vfile_name.as_string(),
1.5       paf        35:                String::UL_FILE_NAME, true);
1.7       paf        36: 
                     37:        // save
                     38:        static_cast<VFile *>(r.self)->save(r.absolute(lfile_name));
                     39: }
                     40: 
                     41: static void _delete(Request& r, const String& method_name, Array *params) {
                     42:        Pool& pool=r.pool();
1.8       paf        43:        Value& vfile_name=*static_cast<Value *>(params->get(0));
1.7       paf        44:        // forcing
                     45:        // ^delete[this body type]
1.8       paf        46:        r.fail_if_junction_(true, vfile_name, 
1.7       paf        47:                method_name, "file name must not be junction");
                     48: 
                     49:        // forcing untaint language
                     50:        String lfile_name(pool);
1.8       paf        51:        lfile_name.append(vfile_name.as_string(),
1.7       paf        52:                String::UL_FILE_NAME, true);
1.4       paf        53:                
1.7       paf        54:        // unlink
                     55:        file_delete(pool, r.absolute(lfile_name));
1.1       paf        56: }
                     57: 
1.8       paf        58: static void _find(Request& r, const String& method_name, Array *params) {
                     59:        Pool& pool=r.pool();
                     60:        Value& vfile_name=*static_cast<Value *>(params->get(0));
                     61:        // forcing
                     62:        // ^delete[this body type]
                     63:        r.fail_if_junction_(true, vfile_name, 
                     64:                method_name, "file name must not be junction");
                     65: 
                     66:        // forcing untaint language
                     67:        String lfile_name(pool);
                     68:        lfile_name.append(vfile_name.as_string(),
                     69:                String::UL_FILE_NAME, true);
                     70: 
                     71:        // passed file name simply exists in current dir
                     72:        if(file_readable(r.absolute(lfile_name))) {
                     73:                r.write_no_lang(*new(pool) VString(lfile_name));
                     74:                return;
                     75:        }
                     76: 
                     77:        // scan .. dirs for result
1.9       paf        78:        for(int i=0; i<FIND_MONKEY_MAX_HOPS; i++) {
1.8       paf        79:                String test_name(pool);
                     80:                for(int j=0; j<i; j++)
                     81:                        test_name.APPEND_CONST("../");
1.15      paf        82:                test_name.append(lfile_name, String::UL_CLEAN);
1.8       paf        83:                if(file_readable(r.absolute(test_name))) {
                     84:                        r.write_no_lang(*new(pool) VString(*new(pool) String(test_name)));
                     85:                        return;
                     86:                }
                     87:        }
                     88: 
                     89:        // not found
                     90:        if(params->size()==2) {
                     91:                // forcing ..{this body type}
                     92:                Value& not_found_code=*static_cast<Value *>(params->get(1));
                     93:                r.fail_if_junction_(false, not_found_code, 
                     94:                        method_name, "not-found param must be junction");
                     95:                r.write_pass_lang(r.process(not_found_code));
                     96:        }
                     97: }
                     98: 
1.9       paf        99: static void _load(Request& r, const String& method_name, Array *params) {
                    100:        Pool& pool=r.pool();
                    101:        Value& vfile_name=*static_cast<Value *>(params->get(0));
                    102: 
1.10      paf       103:        // forcing ^load[this body type]
1.9       paf       104:        r.fail_if_junction_(true, vfile_name, 
                    105:                method_name, "file name must not be junction");
                    106: 
                    107:        // forcing untaint language
                    108:        String lfile_name(pool);
                    109:        lfile_name.append(vfile_name.as_string(),
                    110:                String::UL_FILE_NAME, true);
                    111: 
1.12      paf       112:        void *data;  size_t size;
1.9       paf       113:        file_read(pool, r.absolute(lfile_name), data, size, false/*binary*/);
                    114: 
                    115:        char *user_file_name=params->size()==1?lfile_name.cstr()
                    116:                :static_cast<Value *>(params->get(1))->as_string().cstr();
1.10      paf       117:        
1.11      paf       118:        const String *mime_type=0;
                    119:        if(params->size()==3)
                    120:                mime_type=&static_cast<Value *>(params->get(2))->as_string();
                    121:        else {
                    122:                if(r.mime_types) {
                    123:                        if(char *cext=strrchr(user_file_name, '.')) {
                    124:                                cext++;
                    125:                                String sext(pool, cext);
                    126:                                if(r.mime_types->locate(0, sext))
                    127:                                        if(!(mime_type=r.mime_types->item(1)))
                    128:                                                PTHROW(0, 0,
1.12      paf       129:                                                r.mime_types->origin_string(),
                    130:                                                "MIME-TYPE table column elements must not be empty");
1.11      paf       131:                        }
                    132:                }
                    133:        }
1.12      paf       134: 
1.11      paf       135:        if(!mime_type)
                    136:                mime_type=new(pool) String(pool, "application/octet-stream");
                    137: 
                    138:        static_cast<VFile *>(r.self)->set(data, size, user_file_name, mime_type);
1.9       paf       139: }
                    140: 
1.1       paf       141: // initialize
                    142: 
1.3       paf       143: void initialize_file_class(Pool& pool, VStateless_class& vclass) {
1.1       paf       144:        // ^save[file-name]
1.14      paf       145:        vclass.add_native_method("save", Method::CT_DYNAMIC, _save, 1, 1);
1.7       paf       146: 
                    147:        // ^delete[file-name]
1.14      paf       148:        vclass.add_native_method("delete", Method::CT_STATIC, _delete, 1, 1);
1.8       paf       149: 
                    150:        // ^find[file-name]
                    151:        // ^find[file-name]{when-not-found}
1.14      paf       152:        vclass.add_native_method("find", Method::CT_STATIC, _find, 1, 2);
1.9       paf       153: 
                    154:        // ^load[disk-name]
                    155:        // ^load[disk-name;user-name]
1.11      paf       156:        // ^load[disk-name;user-name;mime-type]
1.14      paf       157:        vclass.add_native_method("load", Method::CT_DYNAMIC, _load, 1, 3);
1.1       paf       158: }

E-mail: