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

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.9     ! paf         6:        $Id: file.C,v 1.8 2001/03/28 09:01:20 paf Exp $
1.1       paf         7: */
                      8: 
                      9: #include "pa_request.h"
                     10: #include "_file.h"
                     11: #include "pa_vfile.h"
                     12: 
1.9     ! paf        13: // consts
        !            14: 
        !            15: const int FIND_MONKEY_MAX_HOPS=10;
        !            16: 
1.1       paf        17: // global var
                     18: 
1.3       paf        19: VStateless_class *file_class;
1.1       paf        20: 
                     21: // methods
                     22: 
1.7       paf        23: /// @test mkdirs
1.1       paf        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("../");
                     82:                test_name.append(lfile_name, String::UL_NO);
                     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: 
        !           103:        // forcing
        !           104:        // ^load[this body type]
        !           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: 
        !           113:        void *data;
        !           114:        size_t size;
        !           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();
        !           119:        r.write_no_lang(*new(pool) VFile(pool, data, size, user_file_name));
        !           120: }
        !           121: 
1.1       paf       122: // initialize
                    123: 
1.3       paf       124: void initialize_file_class(Pool& pool, VStateless_class& vclass) {
1.1       paf       125:        // ^save[file-name]
                    126:        vclass.add_native_method("save", _save, 1, 1);
1.7       paf       127: 
                    128:        // ^delete[file-name]
                    129:        vclass.add_native_method("delete", _delete, 1, 1);
1.8       paf       130: 
                    131:        // ^find[file-name]
                    132:        // ^find[file-name]{when-not-found}
                    133:        vclass.add_native_method("find", _find, 1, 2);
1.9     ! paf       134: 
        !           135:        // ^load[disk-name]
        !           136:        // ^load[disk-name;user-name]
        !           137:        // TODO:^load[disk-name;user-name;content-type]
        !           138:        vclass.add_native_method("load", _load, 1, 2);
1.1       paf       139: }

E-mail: