Back to index

lshw  02.16
dump.cc
Go to the documentation of this file.
00001 #include "dump.h"
00002 #include "version.h"
00003 #include "osutils.h"
00004 
00005 #include <time.h>
00006 #include <stdlib.h>
00007 #include <unistd.h>
00008 #include <sys/types.h>
00009 
00010 #ifdef SQLITE
00011 
00012 using namespace std;
00013 using namespace sqlite;
00014 
00015 static bool createtables(database & db)
00016 {
00017   try {
00018     db.execute("CREATE TABLE IF NOT EXISTS META(key TEXT PRIMARY KEY COLLATE NOCASE, value BLOB)");
00019     statement stm(db, "INSERT OR IGNORE INTO META (key,value) VALUES(?,?)");
00020 
00021     stm.bind(1, "schema");
00022     stm.bind(2, 1.0);
00023     stm.execute();
00024 
00025     stm.reset();
00026     stm.bind(1, "application");
00027     stm.bind(2, "org.ezix.lshw");
00028     stm.execute();
00029 
00030     stm.reset();
00031     stm.bind(1, "creator");
00032     stm.bind(2, "lshw/" + string(getpackageversion()));
00033     stm.execute();
00034 
00035     stm.reset();
00036     stm.bind(1, "OS");
00037     stm.bind(2, operating_system());
00038     stm.execute();
00039 
00040     stm.reset();
00041     stm.bind(1, "platform");
00042     stm.bind(2, platform());
00043     stm.execute();
00044 
00045     db.execute("CREATE TABLE IF NOT EXISTS nodes(path TEXT PRIMARY KEY, id TEXT NOT NULL COLLATE NOCASE, parent TEXT COLLATE NOCASE, class TEXT NOT NULL COLLATE NOCASE, enabled BOOL, claimed BOOL, description TEXT, vendor TEXT, product TEXT, version TEXT, serial TEXT, businfo TEXT, physid TEXT, slot TEXT, size INTEGER, capacity INTEGER, clock INTEGER, width INTEGER, dev TEXT)");
00046     db.execute("CREATE TABLE IF NOT EXISTS logicalnames(logicalname TEXT NOT NULL, node TEXT NOT NULL COLLATE NOCASE)");
00047     db.execute("CREATE TABLE IF NOT EXISTS capabilities(capability TEXT NOT NULL COLLATE NOCASE, node TEXT NOT NULL COLLATE NOCASE, description TEXT, UNIQUE (capability,node))");
00048     db.execute("CREATE TABLE IF NOT EXISTS configuration(config TEXT NOT NULL COLLATE NOCASE, node TEXT NOT NULL COLLATE NOCASE, value TEXT, UNIQUE (config,node))");
00049     db.execute("CREATE TABLE IF NOT EXISTS hints(hint TEXT NOT NULL COLLATE NOCASE, node TEXT NOT NULL COLLATE NOCASE, value TEXT, UNIQUE (hint,node))");
00050     db.execute("CREATE TABLE IF NOT EXISTS resources(node TEXT NOT NULL COLLATE NOCASE, type TEXT NOT NULL COLLATE NOCASE, resource TEXT NOT NULL, UNIQUE(node,type,resource))");
00051     db.execute("CREATE VIEW IF NOT EXISTS unclaimed AS SELECT * FROM nodes WHERE NOT claimed");
00052     db.execute("CREATE VIEW IF NOT EXISTS disabled AS SELECT * FROM nodes WHERE NOT enabled");
00053   }
00054   catch(exception & e)
00055   {
00056     return false;
00057   }
00058 
00059   return true;
00060 }
00061 
00062 bool dump(hwNode & n, database & db, const string & path, bool recurse)
00063 {
00064   if(!createtables(db))
00065     return false;
00066 
00067   try {
00068     unsigned i = 0;
00069     statement stm(db, "INSERT OR REPLACE INTO nodes (id,class,product,vendor,description,size,capacity,width,version,serial,enabled,claimed,slot,clock,businfo,physid,path,parent,dev) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
00070     string mypath = path+(path=="/"?"":"/")+n.getPhysId();
00071 
00072     stm.bind(1, n.getId());
00073     stm.bind(2, n.getClassName());
00074     if(n.getProduct() != "") stm.bind(3, n.getProduct());
00075     if(n.getVendor() != "") stm.bind(4, n.getVendor());
00076     if(n.getDescription() != "") stm.bind(5, n.getDescription());
00077     if(n.getSize()) stm.bind(6, (long long int)n.getSize());
00078     if(n.getCapacity()) stm.bind(7, (long long int)n.getCapacity());
00079     if(n.getWidth()) stm.bind(8, (long long int)n.getWidth());
00080     if(n.getVersion() != "") stm.bind(9, n.getVersion());
00081     if(n.getSerial() != "") stm.bind(10, n.getSerial());
00082     stm.bind(11, (long long int)n.enabled());
00083     stm.bind(12, (long long int)n.claimed());
00084     if(n.getSlot() != "") stm.bind(13, n.getSlot());
00085     if(n.getClock()) stm.bind(14, (long long int)n.getClock());
00086     if(n.getBusInfo() != "") stm.bind(15, n.getBusInfo());
00087     if(n.getPhysId() != "") stm.bind(16, n.getPhysId());
00088     stm.bind(17, mypath);
00089     if(path != "") stm.bind(18, path);
00090     if(n.getDev() != "") stm.bind(19, n.getDev());
00091     stm.execute();
00092 
00093     stm.prepare("INSERT OR REPLACE INTO logicalnames (node,logicalname) VALUES(?,?)");
00094     vector<string> keys = n.getLogicalNames();
00095     for(i=0; i<keys.size(); i++)
00096     {
00097       stm.reset();
00098       stm.bind(1, mypath);
00099       stm.bind(2, keys[i]);
00100       stm.execute();
00101     }
00102 
00103     stm.prepare("INSERT OR REPLACE INTO capabilities (capability,node,description) VALUES(?,?,?)");
00104     keys = n.getCapabilitiesList();
00105     for(i=0; i<keys.size(); i++)
00106     {
00107       stm.reset();
00108       stm.bind(1, keys[i]);
00109       stm.bind(2, mypath);
00110       stm.bind(3, n.getCapabilityDescription(keys[i]));
00111       stm.execute();
00112     }
00113     
00114     stm.prepare("INSERT OR REPLACE INTO configuration (config,node,value) VALUES(?,?,?)");
00115     keys = n.getConfigKeys();
00116     for(i=0; i<keys.size(); i++)
00117     {
00118       stm.reset();
00119       stm.bind(1, keys[i]);
00120       stm.bind(2, mypath);
00121       stm.bind(3, n.getConfig(keys[i]));
00122       stm.execute();
00123     }
00124 
00125     stm.prepare("INSERT OR IGNORE INTO resources (type,node,resource) VALUES(?,?,?)");
00126     keys = n.getResources(":");
00127     for(i=0; i<keys.size(); i++)
00128     {
00129       string type = keys[i].substr(0, keys[i].find_first_of(':'));
00130       string resource = keys[i].substr(keys[i].find_first_of(':')+1);
00131       stm.reset();
00132       stm.bind(1, type);
00133       stm.bind(2, mypath);
00134       stm.bind(3, resource);
00135       stm.execute();
00136     }
00137 
00138     stm.prepare("INSERT OR REPLACE INTO hints (hint,node,value) VALUES(?,?,?)");
00139     keys = n.getHints();
00140     for(i=0; i<keys.size(); i++)
00141     {
00142       stm.reset();
00143       stm.bind(1, keys[i]);
00144       stm.bind(2, mypath);
00145       stm.bind(3, n.getHint(keys[i]).asString());
00146       stm.execute();
00147     }
00148 
00149     stm.reset();
00150     stm.bind(1,"run.root");
00151     stm.bind(2,"");
00152     stm.bind(3,(long long int)(geteuid() == 0));
00153     stm.execute();
00154     stm.reset();
00155     stm.bind(1,"run.time");
00156     stm.bind(2,"");
00157     stm.bind(3,(long long int)time(NULL));
00158     stm.execute();
00159     stm.reset();
00160     stm.bind(1,"run.language");
00161     stm.bind(2,"");
00162     stm.bind(3,getenv("LANG"));
00163     stm.execute();
00164 
00165     if(recurse)
00166       for(i=0; i<n.countChildren(); i++)
00167         dump(*(n.getChild(i)), db, mypath, recurse);
00168   }
00169   catch(exception & e)
00170   {
00171     return false;
00172   }
00173 
00174   return true;
00175 }
00176 
00177 #endif