yast2-storage

AppUtil.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) [2004-2010] Novell, Inc.
00003  *
00004  * All Rights Reserved.
00005  *
00006  * This program is free software; you can redistribute it and/or modify it
00007  * under the terms of version 2 of the GNU General Public License as published
00008  * by the Free Software Foundation.
00009  *
00010  * This program is distributed in the hope that it will be useful, but WITHOUT
00011  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
00013  * more details.
00014  *
00015  * You should have received a copy of the GNU General Public License along
00016  * with this program; if not, contact Novell, Inc.
00017  *
00018  * To contact Novell about this file by physical or electronic mail, you may
00019  * find current contact information at www.novell.com.
00020  */
00021 
00022 
00023 #ifndef APP_UTIL_H
00024 #define APP_UTIL_H
00025 
00026 #include <libintl.h>
00027 #include <sys/time.h>
00028 #include <sys/types.h>
00029 #include <sstream>
00030 #include <locale>
00031 #include <string>
00032 #include <list>
00033 #include <map>
00034 
00035 
00036 namespace storage
00037 {
00038     using std::string;
00039     using std::list;
00040     using std::map;
00041 
00042 
00043 void createPath(const string& Path_Cv);
00044 bool checkNormalFile(const string& Path_Cv);
00045 bool checkDir(const string& Path_Cv);
00046 bool getStatMode(const string& Path_Cv, mode_t& val );
00047 bool setStatMode(const string& Path_Cv, mode_t val );
00048 
00049     list<string> glob(const string& path, int flags);
00050 
00051     struct StatVfs
00052     {
00053         unsigned long long sizeK;
00054         unsigned long long freeK;
00055     };
00056 
00057     bool getStatVfs(const string& path, StatVfs&);
00058 
00059     bool getMajorMinor(const string& device, unsigned long& major, unsigned long& minor, bool may_fail=false);
00060 
00061 
00062 string extractNthWord(int Num_iv, const string& Line_Cv, bool GetRest_bi = false);
00063 std::list<string> splitString( const string& s, const string& delChars=" \t\n",
00064                           bool multipleDelim=true, bool skipEmpty=true,
00065                           const string& quotes="" );
00066 std::map<string,string> makeMap( const std::list<string>& l,
00067                                  const string& delim = "=",
00068                                  const string& removeSur = " \t\n" );
00069 
00070     string udevAppendPart(const string&, unsigned num);
00071     string afterLast(const string& s, const string& pat );
00072 
00073     string udevEncode(const string&);
00074     string udevDecode(const string&);
00075 
00076     map<string, string> getDirLinks(const string& dir);
00077 
00078     bool mkdtemp(string& path);
00079 
00080     bool readlink(const string& path, string& buf);
00081 
00082 
00083     class UdevMap
00084     {
00085     public:
00086 
00087         UdevMap(const string& path);
00088 
00089         typedef map<string, list<string>>::const_iterator const_iterator;
00090 
00091         const_iterator begin() const { return data.begin(); }
00092         const_iterator end() const { return data.end(); }
00093 
00094         const_iterator find(const string& nm) const { return data.find(nm); }
00095 
00096     private:
00097 
00098         map<string, list<string>> data;
00099 
00100     };
00101 
00102 
00103     string normalizeDevice(const string& dev);
00104     list<string> normalizeDevices(const list<string>& devs);
00105     string undevDevice(const string& dev);
00106 
00107 bool isNfsDev( const string& dev );
00108 
00109 unsigned getMajorDevices(const char* driver);
00110 
00111 
00112 template<class StreamType>
00113 void classic(StreamType& stream)
00114 {
00115     stream.imbue(std::locale::classic());
00116 }
00117 
00118 
00119 enum LogLevel { DEBUG, MILESTONE, WARNING, ERROR };
00120 
00121 void createLogger(const string& name, const string& logpath, const string& logfile);
00122 
00123 bool testLogLevel(LogLevel level);
00124 
00125 void prepareLogStream(std::ostringstream& stream);
00126 
00127 std::ostringstream* logStreamOpen();
00128 
00129 void logStreamClose(LogLevel level, const char* file, unsigned line,
00130                     const char* func, std::ostringstream*);
00131 
00132 #define y2deb(op) y2log_op(storage::DEBUG, __FILE__, __LINE__, __FUNCTION__, op)
00133 #define y2mil(op) y2log_op(storage::MILESTONE, __FILE__, __LINE__, __FUNCTION__, op)
00134 #define y2war(op) y2log_op(storage::WARNING, __FILE__, __LINE__, __FUNCTION__, op)
00135 #define y2err(op) y2log_op(storage::ERROR, __FILE__, __LINE__, __FUNCTION__, op)
00136 
00137 #define y2log_op(level, file, line, func, op)                           \
00138     do {                                                                \
00139         if (storage::testLogLevel(level))                               \
00140         {                                                               \
00141             std::ostringstream* __buf = storage::logStreamOpen();       \
00142             *__buf << op;                                               \
00143             storage::logStreamClose(level, file, line, func, __buf);    \
00144         }                                                               \
00145     } while (0)
00146 
00147 
00148     string hostname();
00149     string datetime();
00150 
00151 
00152     class StopWatch
00153     {
00154     public:
00155 
00156         StopWatch();
00157 
00158         friend std::ostream& operator<<(std::ostream& s, const StopWatch& sw);
00159 
00160     protected:
00161 
00162         struct timeval start_tv;
00163 
00164     };
00165 
00166 
00167     struct Text
00168     {
00169         Text() : native(), text() {}
00170         Text(const string& native, const string& text) : native(native), text(text) {}
00171 
00172         void clear();
00173 
00174         const Text& operator+=(const Text& a);
00175 
00176         string native;
00177         string text;
00178     };
00179 
00180 
00181     Text sformat(const Text& format, ...);
00182 
00183 
00184     Text _(const char* msgid);
00185     Text _(const char* msgid, const char* msgid_plural, unsigned long int n);
00186 
00187 
00188 extern const string app_ws;
00189 
00190 }
00191 
00192 #endif