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

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

E-mail: