]> git.stg.codes - ssmd.git/blobdiff - src/switch.cpp
Fix build on osx.
[ssmd.git] / src / switch.cpp
index 444324b946edeb84179eb67a9a6f02fc06e75ec7..71917ed798df5e6e0a519bfe576de63ad9a5000e 100644 (file)
@@ -130,21 +130,32 @@ void Switch::sync()
     }
 
     {
-        std::string fileName(_ip + ".sh");
+        std::ostream * stream = NULL;
+        std::string fileName(_settings.scriptBase() + "/" + _ip + ".sh");
         std::string newFileName(fileName + ".new");
-        std::ofstream script(newFileName.c_str());
-        script << "#!/bin/sh\n";
-        if (!dropACLs(target, script)) {
+
+        if (_settings.dumpScripts()) {
+            stream = new std::ofstream(newFileName.c_str());
+            *stream << "#!/bin/sh\n";
+        } else {
+            stream = new std::stringstream;
+        }
+
+        if (!dropACLs(target, *stream)) {
             logger << "Switch::sync() - failed to drop ACLs for the switch '" << _ip << "'" << std::endl;
+            delete stream;
             return;
         }
 
-        if (!createACLs(target, script)) {
+        if (!createACLs(target, *stream)) {
             logger << "Switch::sync() - failed to create ACLs for the switch '" << _ip << "'" << std::endl;
+            delete stream;
             return;
         }
-        script.close();
-        rename(newFileName.c_str(), fileName.c_str());
+
+        delete stream;
+        if (_settings.dumpScripts())
+            rename(newFileName.c_str(), fileName.c_str());
     }
 
     if (_settings.isDebug()) {
@@ -217,7 +228,8 @@ bool Switch::dropACLsByTable(const CTarget & target, unsigned profileId, const S
     size_t chunks = aclsList.size() / _settings.maxACLPerPDU() + 1;
     for (size_t i = 0; i < chunks && it != aclsList.end(); ++i) {
         Pdu pdu;
-        stream << "snmpset -v2c -cgts_community_w " << _ip;
+        if (_settings.dumpScripts())
+            stream << "snmpset -v2c -c" << _writeCommunity << " " << _ip;
         for (size_t j = 0; j < _settings.maxACLPerPDU() && it != aclsList.end(); ++j, ++it) {
             int id;
             if (int c = it->get_value(id) != SNMP_CLASS_SUCCESS) {
@@ -230,9 +242,11 @@ bool Switch::dropACLsByTable(const CTarget & target, unsigned profileId, const S
             Vb vb(Oid(dropACLOid.c_str()));
             vb.set_value(int(6));
             pdu += vb;
-            stream << " " << dropACLOid << " i 6";
+            if (_settings.dumpScripts())
+                stream << " " << dropACLOid << " i 6";
         }
-        stream << "\n";
+        if (_settings.dumpScripts())
+            stream << "\n";
         if (int c = _snmp.set(pdu, target) != SNMP_CLASS_SUCCESS) {
             if (c != SNMP_ERROR_TOO_BIG) {
                 logger << "Switch::dropACLsByTable() - failed to invoke Snmp::set for the switch '" << _ip << "'. Error message: '" << Snmp::error_msg(c) << "'" << std::endl;
@@ -260,7 +274,8 @@ bool Switch::createACLs(const CTarget & target, std::ostream & stream)
         pdu.clear();
         _aclsCreated = true;
         ++pos;
-        stream << "snmpset -v2c -cgts_community_w " << _ip << " " << *it << "\n";
+        if (_settings.dumpScripts())
+            stream << "snmpset -v2c -c" << _writeCommunity << " " << _ip << " " << *it << "\n";
     }
     return true;
 }