author | Michael Krelin <hacker@klever.net> | 2005-04-25 16:27:12 (UTC) |
---|---|---|
committer | Michael Krelin <hacker@klever.net> | 2005-04-25 16:27:12 (UTC) |
commit | a74c677c09774c02d3bc347525fb071f25d25697 (patch) (side-by-side diff) | |
tree | 8f28192b802047486998460b0894e90fde480fd3 | |
parent | a83335d1c67d37100a018e8ee4fd6c0ff77ac380 (diff) | |
download | konforka-a74c677c09774c02d3bc347525fb071f25d25697.zip konforka-a74c677c09774c02d3bc347525fb071f25d25697.tar.gz konforka-a74c677c09774c02d3bc347525fb071f25d25697.tar.bz2 |
1. bumped version to 0.0.1
2. added utility functions extracted from sitecing
-rw-r--r-- | configure.ac | 2 | ||||
-rw-r--r-- | include/Makefile.am | 1 | ||||
-rw-r--r-- | include/konforka/util.h | 103 | ||||
-rw-r--r-- | lib/Makefile.am | 1 | ||||
-rw-r--r-- | lib/util.cc | 127 |
5 files changed, 233 insertions, 1 deletions
diff --git a/configure.ac b/configure.ac index 96d9325..33d1a59 100644 --- a/configure.ac +++ b/configure.ac @@ -1,7 +1,7 @@ -AC_INIT([konforka], [0.0], [konforka-bugs@klever.net]) +AC_INIT([konforka], [0.0.1], [konforka-bugs@klever.net]) AC_CONFIG_SRCDIR([include/konforka/exception.h]) AC_CONFIG_HEADER([config.h]) AM_INIT_AUTOMAKE([dist-bzip2]) AC_PROG_INSTALL AC_PROG_AWK diff --git a/include/Makefile.am b/include/Makefile.am index 3e043d3..5fbf85e 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -3,10 +3,11 @@ EXTRA_HEADERS= if HAVE_PQXX EXTRA_HEADERS += konforka/pqxx_pile.h endif nobase_include_HEADERS = \ konforka/exception.h \ + konforka/util.h \ konforka/basic_wrapper.h konforka/responsible_wrapper.h \ konforka/resource_pile.h \ konforka/pointer_map.h \ ${EXTRA_HEADERS} diff --git a/include/konforka/util.h b/include/konforka/util.h new file mode 100644 index 0000000..c06edd9 --- a/dev/null +++ b/include/konforka/util.h @@ -0,0 +1,103 @@ +#ifndef __KONFORKA_UTIL_H +#define __KONFORKA_UTIL_H + +#include <sys/types.h> +#include <string> +#include <konforka/exception.h> + +/** + * @file + * @brief miscellaneous utility stuff. + */ + +/** + * @brief The main konforka namespace. + */ +namespace konforka { + using std::string; + + class restricted_sequence_error : public konforka::exception { + public: + restricted_sequence_error(const string& fi,const string& fu,int l,const string& w) + : konforka::exception(fi,fu,l,w) { } + }; + + /** + * normalize_path options enumeration. + * @see normalize_path() + */ + enum normalize_path_options { + /** + * Restrict the /../ sequence. + */ + restrict_dotdot = 1, + /** + * Strip out the leading slash. + */ + strip_leading_slash = 2, + /** + * Strip out the trailing slash. + */ + strip_trailing_slash = 4 + }; + + /** + * Normalize pathname by stripping duplicate slashes, etc. + * @param p the pathname. + * @param o options. + * @return the normalized path. + * @see normalize_path_options + * @todo TODO: document exceptions. + */ + string normalize_path(const string& p,int o=(restrict_dotdot|strip_trailing_slash)); + + /** + * Extract the directory part of the filename. + * @param p the pathname. + * @return the directory part. + */ + string dir_name(const string& p); + + class beyond_root_error : public konforka::exception { + public: + beyond_root_error(const string& fi,const string& fu,int l,const string& w) + : konforka::exception(fi,fu,l,w) { } + }; + + /** + * combine_path options enumeration. + * @see combine_path() + */ + enum combine_path_options { + /** + * The origin is file. Otherwise it is directory. + */ + origin_is_file = 1, + /** + * Fail if we've gone up beyond root. + */ + fail_beyond_root = 2 + }; + + /** + * Combine path with the relative path. + * @param orig the origin. + * @param rel relative path to combine with. + * @param o options. + * @return the paths combined. + * @see combine_path_options + * @todo TODO: document exceptions. + */ + string combine_path(const string& orig,const string& rel,int o=origin_is_file); + + /** + * Create directory and parent directories if needed. + * @param p the pathname. + * @param m mode value for the newly created directories. + */ + void make_path(const string& p,mode_t m); + +} + + /* vim:set ft=cpp: */ +#endif /* __KONFORKA_UTIL_H */ diff --git a/lib/Makefile.am b/lib/Makefile.am index a904569..7d44856 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -9,8 +9,9 @@ EXTRA_SOURCES= if HAVE_PQXX EXTRA_SOURCES += pqxx_pile.cc endif libkonforka_la_SOURCES = \ exception.cc \ + util.cc \ pointer_map.cc \ ${EXTRA_SOURCES} diff --git a/lib/util.cc b/lib/util.cc new file mode 100644 index 0000000..74039c6 --- a/dev/null +++ b/lib/util.cc @@ -0,0 +1,127 @@ +#include <sys/types.h> +#include <sys/stat.h> +#include <konforka/util.h> + +namespace konforka { + + /* + * XXX: this code is borrowed from sitecing as is, although it should be optimized. + */ + + string normalize_path(const string& p,int o) { + const char *s = p.c_str(); + if( s[0]=='.' && s[1]=='/' ) + s += 2; // skip leading './' + if(o&strip_leading_slash) + for(;(*s)=='/';s++); + string rv; + string::size_type notslash = 0; + for(;*s;s++) { + if(s[0]=='/') { + if(s[1]=='/') + continue; // skip duplicate slash + if(s[1]=='.' && s[2]=='/') { + // '/./' sequence encountered + s += 2; + continue; + } + } + if( + (o&restrict_dotdot) && ( + ( rv.empty() && s[0]=='.' && s[1]=='.' && s[2]=='/' ) // '^../' + || + ( s[0]=='/' && s[1]=='.' && s[2]=='.' && (s[3]=='/' || s[3]==0) ) // '/../' or '/..$' + ) + ) + throw restricted_sequence_error(CODEPOINT,"restricted updir (..) sequence encountered"); + rv += *s; + if( (*s) !='/' ) + notslash = rv.length(); + } + if(!(o&strip_trailing_slash)) + notslash++; + if(notslash<rv.length()) + rv.erase(notslash); // XXX: does this operation have enough sense to be performed? + return rv; + } + + string dir_name(const string& p) { + string::size_type sl = p.find_last_of('/'); + if(sl==string::npos) + return ""; // no slashes -- no dir. + string::size_type nosl = p.find_last_not_of('/',sl); + if(nosl==string::npos) + return ""; // only slashes -- no dir. + return p.substr(0,nosl+1); + } + + string combine_path(const string& orig,const string& rel,int o) { + string r = normalize_path(rel,0); + if(r.empty()) { + // XXX: this behaviour is questionable. + return normalize_path( (o&origin_is_file)?dir_name(orig):orig, strip_leading_slash|restrict_dotdot|strip_trailing_slash); + } + string rv; + if(r[0]=='/') { + r.erase(0,1); + }else{ + rv = normalize_path( (o&origin_is_file)?dir_name(orig):orig, restrict_dotdot|strip_trailing_slash); + } + string::size_type lsl = rv.rfind('/'); + // iterate through slashes in relative path + for(string::size_type sl=r.find('/');sl!=string::npos;sl=r.find('/')) { + assert(sl!=0); // sure we don't start with '/' at this point + if(sl==1 && r[0]=='.') { // './' + r.erase(0,2); + }else if(sl==2 && r[0]=='.' && r[1]=='.') { // '../' + if(lsl==string::npos) { + if(rv.empty() && (o&fail_beyond_root)) + throw beyond_root_error(CODEPOINT,"went beyond root while combining path"); + rv.clear(); + }else{ + rv.erase(lsl); + lsl = rv.rfind('/'); + } + r.erase(0,3); + }else{ // 'something/' + lsl = rv.length(); + rv += '/'; + rv += r.substr(0,sl); + r.erase(0,sl+1); + } + } + if(r.empty()) + return rv+'/'; + if(r.length()==2 && r[0]=='.' && r[1]=='.') { + if(lsl==string::npos) { + if(rv.empty() && (o&fail_beyond_root)) + throw beyond_root_error(CODEPOINT,"went beyond root while combining path"); + return "/"; + }else{ + rv.erase(lsl+1); + return rv; + } + } + rv += '/'; + rv += r; + return rv; + } + + void make_path(const string& p,mode_t m) { + struct stat st; + for(string::size_type sl=0;sl!=string::npos;sl=p.find('/',sl+1)) { + if(!sl) + continue; + string pp = p.substr(0,sl); + if(stat(pp.c_str(),&st) || !S_ISDIR(st.st_mode)) { + if(mkdir(pp.c_str(),m)) + throw konforka::system_error(CODEPOINT,"failed to mkdir()"); + } + } + if(stat(p.c_str(),&st) || !S_ISDIR(st.st_mode)) { + if(mkdir(p.c_str(),m)) + throw konforka::system_error(CODEPOINT,"failed to mkdir()"); + } + } + +} |