Diff for /parser3/src/classes/file.C between versions 1.301 and 1.310

version 1.301, 2026/09/30 16:44:19 version 1.310, 2026/11/27 21:35:11
Line 5 Line 5
         Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru>          Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru>
 */  */
   
 #include "pa_config_includes.h"  
   
 #include "classes.h"  #include "classes.h"
 #include "pa_vmethod_frame.h"  #include "pa_vmethod_frame.h"
 #include "pa_base64.h"  #include "pa_base64.h"
Line 51  public: Line 49  public:
         File_list_table_template_columns() {          File_list_table_template_columns() {
                 *this+=new String("name");                  *this+=new String("name");
                 *this+=new String("dir");                  *this+=new String("dir");
                   *this+=new String("symlink");
                 *this+=new String("size");                  *this+=new String("size");
                 *this+=new String("cdate");                  *this+=new String("cdate");
                 *this+=new String("mdate");                  *this+=new String("mdate");
Line 143  static void _save(Request& r, MethodPara Line 142  static void _save(Request& r, MethodPara
                 if(HashStringValue* options=params.as_hash(2)){                  if(HashStringValue* options=params.as_hash(2)){
                         int valid_options=0;                          int valid_options=0;
                         if(Value* vcharset_name=options->get(PA_CHARSET_NAME)){                          if(Value* vcharset_name=options->get(PA_CHARSET_NAME)){
                                 asked_charset=&pa_charsets.get(vcharset_name->as_string());                                  asked_charset=&pa_charsets.get_file_charset(vcharset_name->as_string());
                                 valid_options++;                                  valid_options++;
                         }                          }
                         if(Value* vappend=options->get(MODE_APPEND)){                          if(Value* vappend=options->get(MODE_APPEND)){
Line 184  static void _delete(Request& r, MethodPa Line 183  static void _delete(Request& r, MethodPa
                 file_delete(*path, fail_on_problem, keep_empty_dirs);                  file_delete(*path, fail_on_problem, keep_empty_dirs);
 }  }
   
   static void _rmdir(Request& r, MethodParams& params) {
           const String& dir_name=params.as_file_name(0);
           bool fail_on_problem=true;
   
           if(params.count()>1)
                   if(HashStringValue* options=params.as_hash(1)){
                           int valid_options=0;
                           if(Value* vsuppress_exception=options->get(SUPPRESS_EXCEPTION_NAME)){
                                   fail_on_problem=r.process(*vsuppress_exception).as_bool();
                                   valid_options++;
                           }
                           if(valid_options != options->count())
                                   throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
                   }
   
           const String* path=fail_on_problem ? &r.full_disk_path(dir_name) : r.real_disk_path(dir_name);
           if(path)
                   dir_delete(*path, fail_on_problem);
   }
   
 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");
Line 503  static void _exec_cgi(Request& r, Method Line 522  static void _exec_cgi(Request& r, Method
   
         // const          // const
         ECSTR(GATEWAY_INTERFACE, "CGI/1.1");          ECSTR(GATEWAY_INTERFACE, "CGI/1.1");
         ECSTR(PARSER_VERSION, PARSER_VERSION);          ECSTR(PARSER_VERSION, PARSER_VERSION_WITH_FEATURES);
         // 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 707  static void _list(Request& r, MethodPara Line 726  static void _list(Request& r, MethodPara
         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;          bool stat=false;
           bool fail_on_problem=false;
         VRegex* vregex=0;          VRegex* vregex=0;
         VRegexCleaner vrcleaner;          VRegexCleaner vrcleaner;
   
Line 724  static void _list(Request& r, MethodPara Line 744  static void _list(Request& r, MethodPara
                                         vfilter=value;                                          vfilter=value;
                                         valid_options++;                                          valid_options++;
                                 }                                  }
                                   if(Value* vexception=options->get(SUPPRESS_EXCEPTION_NAME)) {
                                           fail_on_problem=r.process(*vexception).as_bool();
                                           valid_options++;
                                   }
                                 if(valid_options!=options->count())                                  if(valid_options!=options->count())
                                         throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);                                          throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION);
                         } else {                          } else {
Line 745  static void _list(Request& r, MethodPara Line 769  static void _list(Request& r, MethodPara
                 }                  }
         }          }
   
         const char* absolute_path_cstr=r.full_disk_path(relative_path.as_string()).taint_cstr(String::L_FILE_SPEC);  
   
         Table::Action_options table_options;          Table::Action_options table_options;
         Table& table=*new Table(file_list_table_template(), table_options);          Table& table=*new Table(file_list_table_template(), table_options);
   
           // no exception for a uri or an invalid or tainted file://
           const String* path=fail_on_problem ? &r.full_disk_path(relative_path.as_string()) : r.real_disk_path(relative_path.as_string());
           if(!path) {
                   r.write(*new VTable(&table));
                   return;
           }
           const char* absolute_path_cstr=path->taint_cstr(String::L_FILE_SPEC);
   
         const int ovector_size=(1/*match*/)*3;          const int ovector_size=(1/*match*/)*3;
         int ovector[ovector_size];          int ovector[ovector_size];
   
Line 761  static void _list(Request& r, MethodPara Line 791  static void _list(Request& r, MethodPara
                         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(ffblk.is_dir(stat) ? "1" : "0", String::L_CLEAN);                          *row+=new String(ffblk.is_dir(stat) ? "1" : "0", String::L_CLEAN);
                           *row+=new String(ffblk.is_symlink() ? "1" : "0", String::L_CLEAN);
                         if(stat) {                          if(stat) {
                                 *row+=VDouble(ffblk.size()).get_string();                                  *row+=VDouble(ffblk.size()).get_string();
                                 *row+=new String(pa_uitoa(ffblk.c_timestamp()), String::L_CLEAN);                                  *row+=new String(pa_uitoa(ffblk.c_timestamp()), String::L_CLEAN);
Line 769  static void _list(Request& r, MethodPara Line 800  static void _list(Request& r, MethodPara
                         }                          }
                         table+=row;                          table+=row;
                 }                  }
           ,
                   if(fail_on_problem)
                           throw Exception(errno==EACCES ? "file.access" : (errno==ENOENT || errno==ENOTDIR) ? "file.missing" : 0,
                                   path, "list failed: %s (%d)%s", strerror(errno), errno, actual_filename(*path, absolute_path_cstr));
         );          );
   
         // write out result          // write out result
Line 829  static const String* web_path(Request& r Line 864  static const String* web_path(Request& r
   
         // checking the result is inside document root;          // checking the result is inside document root;
         // first checking the segment boundary: /root must not match /root-but-longer          // first checking the segment boundary: /root must not match /root-but-longer
         return result.first_char()=='/' && !strncmp(disk_path->cstr(), document_root, document_root_length) ? &result : 0;          return result.first_char()=='/' && !path_cmp(disk_path->cstr(), document_root, document_root_length) ? &result : 0;
 }  }
   
 static void _find(Request& r, MethodParams& params) {  static void _find(Request& r, MethodParams& params) {
Line 966  static void _fullpath(Request& r, Method Line 1001  static void _fullpath(Request& r, Method
                 throw Exception(PARSER_RUNTIME, &file_spec, "is outside of $request:document-root [%s]", r.request_info.document_root);                  throw Exception(PARSER_RUNTIME, &file_spec, "is outside of $request:document-root [%s]", r.request_info.document_root);
 }  }
   
   static void _diskpath(Request& r, MethodParams& params) {
           r.write(r.full_disk_path(params.as_file_spec(0)));
   }
   
 static void _sql_string(Request& r, MethodParams&) {  static void _sql_string(Request& r, MethodParams&) {
         VFile& self=GET_SELF(r, VFile);          VFile& self=GET_SELF(r, VFile);
   
Line 1240  MFile::MFile(): Methoded("file") { Line 1279  MFile::MFile(): Methoded("file") {
         add_native_method("create", Method::CT_DYNAMIC, _create, 1, 4);          add_native_method("create", Method::CT_DYNAMIC, _create, 1, 4);
   
         // ^file.save[mode;file-name]          // ^file.save[mode;file-name]
         // ^file.save[mode;file-name;$.charset[...]$.append(false)]          // ^file.save[mode;file-name; $.charset[...] $.append(false) ]
         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]
         // ^file:delete[file-name;$.keep-empty-dir(true)$.exception(false)]          // ^file:delete[file-name; $.keep-empty-dir(true) $.exception(false) ]
         add_native_method("delete", Method::CT_STATIC, _delete, 1, 2);          add_native_method("delete", Method::CT_STATIC, _delete, 1, 2);
   
           // ^file:rmdir[dir-name]
           // ^file:rmdir[dir-name; $.exception(false) ]
           add_native_method("rmdir", Method::CT_STATIC, _rmdir, 1, 2);
   
         // ^file:move[from-file-name;to-file-name]          // ^file:move[from-file-name;to-file-name]
         // ^file:move[from-file-name;to-file-name;$.keep-empty-dir(true)]          // ^file:move[from-file-name;to-file-name; $.keep-empty-dir(true) ]
         add_native_method("move", Method::CT_STATIC, _move, 2, 3);          add_native_method("move", Method::CT_STATIC, _move, 2, 3);
   
         // ^file::load[mode;disk-name]          // ^file::load[mode;disk-name]
Line 1293  MFile::MFile(): Methoded("file") { Line 1336  MFile::MFile(): Methoded("file") {
         add_native_method("justext", Method::CT_STATIC, _justext, 1, 1);          add_native_method("justext", Method::CT_STATIC, _justext, 1, 1);
         // /some/page.html: ^file:fullpath[a.gif] => /some/a.gif          // /some/page.html: ^file:fullpath[a.gif] => /some/a.gif
         add_native_method("fullpath", Method::CT_STATIC, _fullpath, 1, 1);          add_native_method("fullpath", Method::CT_STATIC, _fullpath, 1, 1);
           // /some/page.html: ^file:diskpath[a.gif] => /document/root/some/a.gif, file:// and C:\ resolved as well
           add_native_method("diskpath", Method::CT_STATIC, _diskpath, 1, 1);
   
         // ^file.sql-string[]          // ^file.sql-string[]
         add_native_method("sql-string", Method::CT_DYNAMIC, _sql_string, 0, 0);          add_native_method("sql-string", Method::CT_DYNAMIC, _sql_string, 0, 0);
Line 1307  MFile::MFile(): Methoded("file") { Line 1352  MFile::MFile(): Methoded("file") {
         // decode:          // decode:
         //   ^file::base64[encoded] // backward          //   ^file::base64[encoded] // backward
         //   ^file::base64[mode;user-file-name;encoded]          //   ^file::base64[mode;user-file-name;encoded]
         //   ^file::base64[mode;user-file-name;encoded;$.content-type[...]]          //   ^file::base64[mode;user-file-name;encoded; $.content-type[...] ]
         add_native_method("base64", Method::CT_ANY, _base64, 0, 4);          add_native_method("base64", Method::CT_ANY, _base64, 0, 4);
   
         // ^file.crc32[]          // ^file.crc32[]
Line 1319  MFile::MFile(): Methoded("file") { Line 1364  MFile::MFile(): Methoded("file") {
         add_native_method("md5", Method::CT_ANY, _md5, 0, 1);          add_native_method("md5", Method::CT_ANY, _md5, 0, 1);
   
         // ^file:copy[from-file-name;to-file-name]          // ^file:copy[from-file-name;to-file-name]
         // ^file:copy[from-file-name;to-file-name;$.append(false)]          // ^file:copy[from-file-name;to-file-name; $.append(false) ]
         add_native_method("copy", Method::CT_STATIC, _copy, 2, 3);          add_native_method("copy", Method::CT_STATIC, _copy, 2, 3);
 }  }

Removed from v.1.301  
changed lines
  Added in v.1.310


E-mail: