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

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

E-mail: