Diff for /parser3/src/classes/file.C between versions 1.221 and 1.236

version 1.221, 2012/06/15 06:13:10 version 1.236, 2015/06/29 17:59:13
Line 33  volatile const char * IDENT_FILE_C="$Id$ Line 33  volatile const char * IDENT_FILE_C="$Id$
 #define CHARSET_EXEC_PARAM_NAME "charset"  #define CHARSET_EXEC_PARAM_NAME "charset"
   
 #define NAME_NAME "name"  #define NAME_NAME "name"
   #define KEEP_EMPTY_DIRS_NAME "keep-empty-dirs"
   #define SUPPRESS_EXCEPTION_NAME "exception"
   
 // externs  // externs
   
 extern String sql_limit_name;  extern String sql_limit_name;
 extern String sql_offset_name;  extern String sql_offset_name;
   
   // helpers
   
   class File_list_table_template_columns: public ArrayString {
   public:
           File_list_table_template_columns() {
                   *this+=new String("name");
                   *this+=new String("dir");
                   *this+=new String("size");
                   *this+=new String("cdate");
                   *this+=new String("mdate");
                   *this+=new String("adate");
           }
   };
   
   Table file_list_table_template(new File_list_table_template_columns);
   
 // class  // class
   
 class MFile: public Methoded {  class MFile: public Methoded {
Line 128  static void _save(Request& r, MethodPara Line 146  static void _save(Request& r, MethodPara
   
 static void _delete(Request& r, MethodParams& params) {  static void _delete(Request& r, MethodParams& params) {
         const String& file_name=params.as_string(0, FILE_NAME_MUST_NOT_BE_CODE);          const String& file_name=params.as_string(0, FILE_NAME_MUST_NOT_BE_CODE);
           bool keep_empty_dirs=false;
           bool fail_on_problem=true;
   
           if(params.count()>1)
                   if(HashStringValue* options=params.as_hash(1)){
                           int valid_options=0;
                           if(Value* vkeep_empty_dirs=options->get(KEEP_EMPTY_DIRS_NAME)){
                                   keep_empty_dirs=r.process_to_value(*vkeep_empty_dirs).as_bool();
                                   valid_options++;
                           }
                           if(Value* vsuppress_exception=options->get(SUPPRESS_EXCEPTION_NAME)){
                                   fail_on_problem=r.process_to_value(*vsuppress_exception).as_bool();
                                   valid_options++;
                           }
                           if(valid_options != options->count())
                                   throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
                   }
   
         // unlink          // unlink
         file_delete(r.absolute(file_name));          file_delete(r.absolute(file_name), fail_on_problem, keep_empty_dirs);
 }  }
   
 static void _move(Request& r, MethodParams& params) {  static void _move(Request& r, MethodParams& params) {
         Value& vfrom_file_name=params.as_no_junction(0, "from file name must not be code");          Value& vfrom_file_name=params.as_no_junction(0, "from file name must not be code");
         Value& vto_file_name=params.as_no_junction(1, "to file name must not be code");          Value& vto_file_name=params.as_no_junction(1, "to file name must not be code");
           bool keep_empty_dirs=false;
   
           if(params.count()>2)
                   if(HashStringValue* options=params.as_hash(2)){
                           int valid_options=0;
                           if(Value* vkeep_empty_dirs=options->get(KEEP_EMPTY_DIRS_NAME)){
                                   keep_empty_dirs=r.process_to_value(*vkeep_empty_dirs).as_bool();
                                   valid_options++;
                           }
                           if(valid_options != options->count())
                                   throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
                   }
   
         // move          // move
         file_move(          file_move(
                 r.absolute(vfrom_file_name.as_string()),                  r.absolute(vfrom_file_name.as_string()),
                 r.absolute(vto_file_name.as_string()));                  r.absolute(vto_file_name.as_string()),
                   keep_empty_dirs);
 }  }
   
 static void copy_process_source(  static void copy_process_source(
Line 166  static void copy_process_source( Line 214  static void copy_process_source(
 static void copy_open_target(int f, void *from_spec) {  static void copy_open_target(int f, void *from_spec) {
         String& file_spec=*static_cast<String *>(from_spec);          String& file_spec=*static_cast<String *>(from_spec);
         file_read_action_under_lock(file_spec, "copy", copy_process_source, &f);          file_read_action_under_lock(file_spec, "copy", copy_process_source, &f);
 };  }
   
 static void _copy(Request& r, MethodParams& params) {  static void _copy(Request& r, MethodParams& params) {
         Value& vfrom_file_name=params.as_no_junction(0, "from file name must not be code");          Value& vfrom_file_name=params.as_no_junction(0, "from file name must not be code");
Line 306  static void _create(Request& r, MethodPa Line 354  static void _create(Request& r, MethodPa
         VFile& self=GET_SELF(r, VFile);          VFile& self=GET_SELF(r, VFile);
   
         if(const String* content_str=vcontent.get_string()){          if(const String* content_str=vcontent.get_string()){
                 String::Body body=content_str->cstr_to_string_body_untaint(String::L_AS_IS); // explode content, honor tainting changes                  String::Body body=content_str->cstr_to_string_body_untaint(String::L_AS_IS, r.connection(false), &r.charsets); // explode content, honor tainting changes
                 if(asked_charset && is_text)                  if(asked_charset && is_text)
                         body=Charset::transcode(body, r.charsets.source(), *asked_charset);                          body=Charset::transcode(body, r.charsets.source(), *asked_charset);
                   self.set(true/*tainted*/, is_text, body.cstrm(), body.length(), file_name, vcontent_type, &r);
                 self.set(true/*tainted*/, is_text, body.cstrm(), body.length());  
         } else {          } else {
                 if(asked_charset)                  if(asked_charset)
                         throw Exception(PARSER_RUNTIME, 0, "charset option can not be used with file-content");                          throw Exception(PARSER_RUNTIME, 0, "charset option can not be used with file-content");
                 self.set(*vcontent.as_vfile(String::L_AS_IS));                  self.set(*vcontent.as_vfile(String::L_AS_IS), mode != 0, is_text, file_name, vcontent_type, &r);
                 self.set_mode(is_text);  
         }          }
   
         self.set_name(file_name);  
   
         self.set_content_type(vcontent_type, file_name, &r);  
 }  }
   
 static void _stat(Request& r, MethodParams& params) {  static void _stat(Request& r, MethodParams& params) {
Line 334  static void _stat(Request& r, MethodPara Line 377  static void _stat(Request& r, MethodPara
                   
         VFile& self=GET_SELF(r, VFile);          VFile& self=GET_SELF(r, VFile);
   
         self.set(true/*tainted*/, false/*binary*/, 0/*no bytes*/, size, &lfile_name, 0, &r);          self.set_binary(true/*tainted*/, 0/*no bytes*/, size, &lfile_name, 0, &r);
         HashStringValue& ff=self.fields();          HashStringValue& ff=self.fields();
         ff.put(adate_name, new VDate(atime));          ff.put(adate_name, new VDate(atime));
         ff.put(mdate_name, new VDate(mtime));          ff.put(mdate_name, new VDate(mtime));
Line 344  static void _stat(Request& r, MethodPara Line 387  static void _stat(Request& r, MethodPara
 static bool is_safe_env_key(const char* key) {  static bool is_safe_env_key(const char* key) {
         for(const char* validator=key; *validator; validator++) {          for(const char* validator=key; *validator; validator++) {
                 char c=*validator;                  char c=*validator;
                 if(!(c>='A' && c<='Z' || c>='0' && c<='9' || c=='_' || c=='-'))                  if(!( (c>='A' && c<='Z') || (c>='0' && c<='9') || (c=='_' || c=='-') ))
                         return false;                          return false;
         }          }
 #ifdef PA_SAFE_MODE  #ifdef PA_SAFE_MODE
Line 407  static void pass_cgi_header_attribute( Line 450  static void pass_cgi_header_attribute(
   
 static void append_to_argv(Request& r, ArrayString& argv, const String* str){  static void append_to_argv(Request& r, ArrayString& argv, const String* str){
         if(!str->is_empty())          if(!str->is_empty())
                 argv+=new String(str->cstr_to_string_body_untaint(String::L_AS_IS, 0, &r.charsets), String::L_AS_IS);                  argv+=new String(str->cstr_to_string_body_untaint(String::L_AS_IS, r.connection(false), &r.charsets), String::L_AS_IS);
 }  }
   
 /// @todo fix `` in perl - they produced flipping consoles and no output to perl  /// @todo fix `` in perl - they produced flipping consoles and no output to perl
Line 431  static void _exec_cgi(Request& r, Method Line 474  static void _exec_cgi(Request& r, Method
                         env.put( \                          env.put( \
                                 String::Body(#name), \                                  String::Body(#name), \
                                 String::Body(*value_cstr?value_cstr:0)); \                                  String::Body(*value_cstr?value_cstr:0)); \
         // passing SAPI::environment          // passing environment
         if(const char *const *pairs=SAPI::environment(r.sapi_info)) {          for(SAPI::Env::Iterator i(r.sapi_info); i; i.next() )
                 while(const char* pair=*pairs++)                  env.put(
                         if(const char* eq_at=strchr(pair, '='))                          i.key(),
                                 if(eq_at[1]) // has value                          i.value()
                                         env.put(                  );
                                                 pa_strdup(pair, eq_at-pair),  
                                                 pa_strdup(eq_at+1, 0));  
         }  
   
         // const          // const
         ECSTR(GATEWAY_INTERFACE, "CGI/1.1");          ECSTR(GATEWAY_INTERFACE, "CGI/1.1");
         ECSTR("PARSER_VERSION", PARSER_VERSION);          ECSTR(PARSER_VERSION, PARSER_VERSION);
         // from Request.info          // from Request.info
         ECSTR(DOCUMENT_ROOT, r.request_info.document_root);          ECSTR(DOCUMENT_ROOT, r.request_info.document_root);
         ECSTR(PATH_TRANSLATED, r.request_info.path_translated);          ECSTR(PATH_TRANSLATED, r.request_info.path_translated);
Line 478  static void _exec_cgi(Request& r, Method Line 518  static void _exec_cgi(Request& r, Method
                         if(info.vstdin) {                          if(info.vstdin) {
                                 if(const String* sstdin=info.vstdin->get_string()) {                                  if(const String* sstdin=info.vstdin->get_string()) {
                                         // untaint stdin                                          // untaint stdin
                                         in = new String(sstdin->cstr_to_string_body_untaint(String::L_AS_IS), String::L_AS_IS);                                          in = new String(sstdin->cstr_to_string_body_untaint(String::L_AS_IS, r.connection(false), &r.charsets), String::L_AS_IS);
                                 } else                                  } else
                                         if(VFile* vfile=static_cast<VFile *>(info.vstdin->as("file")))                                          if(VFile* vfile=static_cast<VFile *>(info.vstdin->as("file")))
                                                 in->append_know_length((const char* )vfile->value_ptr(), vfile->value_size(), String::L_TAINTED);                                                  in->append_know_length((const char* )vfile->value_ptr(), vfile->value_size(), String::L_TAINTED);
Line 640  static void _cgi(Request& r, MethodParam Line 680  static void _cgi(Request& r, MethodParam
 static void _list(Request& r, MethodParams& params) {  static void _list(Request& r, MethodParams& params) {
         Value& relative_path=params.as_no_junction(0, "path must not be code");          Value& relative_path=params.as_no_junction(0, "path must not be code");
   
           bool stat=false;
         VRegex* vregex=0;          VRegex* vregex=0;
         VRegexCleaner vrcleaner;          VRegexCleaner vrcleaner;
   
         if(params.count()>1){          if(params.count()>1){
                 Value& regexp=params.as_no_junction(1, "regexp must not be code");                  Value& voption=params.as_no_junction(1, "option must not be code");
                 if(regexp.is_defined()){                  if(voption.is_defined()) {
                         if(Value* value=regexp.as(VREGEX_TYPE)){                          Value* vfilter=0;
                                 vregex=static_cast<VRegex*>(value);                          if(HashStringValue* options=voption.get_hash()) {
                                   int valid_options=0;
                                   if(Value* vstat=options->get("stat")) {
                                           stat=r.process_to_value(*vstat).as_bool();
                                           valid_options++;
                                   }
                                   if(Value* value=options->get("filter")) {
                                           vfilter=value;
                                           valid_options++;
                                   }
                                   if(valid_options!=options->count())
                                           throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
                         } else {                          } else {
                                 vregex=new VRegex(r.charsets.source(), &regexp.as_string(), 0/*options*/);                                  vfilter=&voption;
                                 vregex->study();                          }
                                 vrcleaner.vregex=vregex;                          if(vfilter) {
                                   if(Value* value=vfilter->as(VREGEX_TYPE)) {
                                           vregex=static_cast<VRegex*>(value);
                                   } else if(vfilter->is_string()) {
                                           if(!vfilter->get_string()->trim().is_empty()) {
                                                   vregex=new VRegex(r.charsets.source(), &vfilter->as_string(), 0/*options*/);
                                                   vregex->study();
                                                   vrcleaner.vregex=vregex;
                                           }
                                   } else {
                                           throw Exception(PARSER_RUNTIME, 0, "filter must be regex or string");
                                   }
                         }                          }
                 }                  }
         }          }
   
         const char* absolute_path_cstr=r.absolute(relative_path.as_string()).taint_cstr(String::L_FILE_SPEC);          const char* absolute_path_cstr=r.absolute(relative_path.as_string()).taint_cstr(String::L_FILE_SPEC);
   
         Table::columns_type columns(new ArrayString);          Table::Action_options table_options;
         *columns+=new String("name");          Table& table=*new Table(file_list_table_template, table_options);
         Table& table=*new Table(columns);  
   
         const int ovector_size=(1/*match*/)*3;          const int ovector_size=(1/*match*/)*3;
         int ovector[ovector_size];          int ovector[ovector_size];
Line 671  static void _list(Request& r, MethodPara Line 734  static void _list(Request& r, MethodPara
                 if(!vregex || vregex->exec(ffblk.ff_name, file_name_size, ovector, ovector_size)>=0) {                  if(!vregex || vregex->exec(ffblk.ff_name, file_name_size, ovector, ovector_size)>=0) {
                         Table::element_type row(new ArrayString);                          Table::element_type row(new ArrayString);
                         *row+=new String(pa_strdup(file_name_cstr, file_name_size), String::L_TAINTED);                          *row+=new String(pa_strdup(file_name_cstr, file_name_size), String::L_TAINTED);
                           *row+=new String(String::Body::Format(ffblk.is_dir(stat) ? 1 : 0), String::L_CLEAN);
                           if(stat) {
                                   *row+=VDouble(ffblk.size()).get_string();
                                   *row+=new String(String::Body::Format((int)ffblk.c_timestamp()), String::L_CLEAN);
                                   *row+=new String(String::Body::Format((int)ffblk.m_timestamp()), String::L_CLEAN);
                                   *row+=new String(String::Body::Format((int)ffblk.a_timestamp()), String::L_CLEAN);
                           }
                         table+=row;                          table+=row;
                 }                  }
         );          );
Line 685  struct Lock_execute_body_info { Line 755  struct Lock_execute_body_info {
         Value* body_code;          Value* body_code;
 };  };
 #endif  #endif
   
 static void lock_execute_body(int , void *ainfo) {  static void lock_execute_body(int , void *ainfo) {
         Lock_execute_body_info& info=*static_cast<Lock_execute_body_info *>(ainfo);          Lock_execute_body_info& info=*static_cast<Lock_execute_body_info *>(ainfo);
         // execute body          // execute body
         info.r->write_assign_lang(info.r->process(*info.body_code));          info.r->write_assign_lang(info.r->process(*info.body_code));
 };  }
   
 static void _lock(Request& r, MethodParams& params) {  static void _lock(Request& r, MethodParams& params) {
         const String& file_spec=r.absolute(params.as_string(0, FILE_NAME_MUST_BE_STRING));          const String& file_spec=r.absolute(params.as_string(0, FILE_NAME_MUST_BE_STRING));
         Lock_execute_body_info info={          Lock_execute_body_info info={
Line 966  static void _sql(Request& r, MethodParam Line 1038  static void _sql(Request& r, MethodParam
   
         VFile& self=GET_SELF(r, VFile);          VFile& self=GET_SELF(r, VFile);
   
         self.set(true/*tainted*/, false/*binary*/, (char*)handlers.value.str, handlers.value.length, handlers.user_file_name          self.set_binary(true/*tainted*/, handlers.value.str, handlers.value.length, handlers.user_file_name
                                 , handlers.user_content_type ? new VString(*handlers.user_content_type) : 0                                  , handlers.user_content_type ? new VString(*handlers.user_content_type) : 0
                                 , &r);                                  , &r);
 }  }
Line 1124  MFile::MFile(): Methoded("file") { Line 1196  MFile::MFile(): Methoded("file") {
         add_native_method("save", Method::CT_DYNAMIC, _save, 2, 3);          add_native_method("save", Method::CT_DYNAMIC, _save, 2, 3);
   
         // ^file:delete[file-name]          // ^file:delete[file-name]
         add_native_method("delete", Method::CT_STATIC, _delete, 1, 1);          // ^file:delete[file-name;$.keep-empty-dir(true)$.exception(false)]
           add_native_method("delete", Method::CT_STATIC, _delete, 1, 2);
   
         // ^file:move[from-file-name;to-file-name]          // ^file:move[from-file-name;to-file-name]
         add_native_method("move", Method::CT_STATIC, _move, 2, 2);          // ^file:move[from-file-name;to-file-name;$.keep-empty-dir(true)]
           add_native_method("move", Method::CT_STATIC, _move, 2, 3);
   
         // ^file::load[mode;disk-name]          // ^file::load[mode;disk-name]
         // ^file::load[mode;disk-name;user-name]          // ^file::load[mode;disk-name;user-name]
Line 1150  MFile::MFile(): Methoded("file") { Line 1224  MFile::MFile(): Methoded("file") {
   
         // ^file:list[path]          // ^file:list[path]
         // ^file:list[path][regexp]          // ^file:list[path][regexp]
           // ^file:list[path][$.filter[regexp] $.stat(true)]
         add_native_method("list", Method::CT_STATIC, _list, 1, 2);          add_native_method("list", Method::CT_STATIC, _list, 1, 2);
   
         // ^file:lock[path]{code}          // ^file:lock[path]{code}

Removed from v.1.221  
changed lines
  Added in v.1.236


E-mail: