bool NodeConfigManager::saveUserConfig(UserId userId)
{
   // FIXME: do we need locking for load/save/get/remove?
   bool rval;
   string path;
   rval = getUserConfigPath(this, userId, path);
   if(rval)
   {
      MO_CAT_DEBUG(BM_NODE_CAT,
         "NodeConfigManager: save user config: id=%" PRIu64 ", path=%s",
         userId, path.c_str());
      // FIXME: how to check if config is "empty" of useful data?
      Config c = getUserConfig(userId, true);
      File file(path.c_str());
      rval = file->mkdirs();
      if(rval)
      {
         FileOutputStream fos(file);
         JsonWriter writer;
         writer.setCompact(false);
         rval = writer.write(c, &fos);
         fos.close();
      }
   }
   return true;
}
bool Id3v2TagFrameIO::getContractData(
   ByteBuffer* b, uint32_t* uncompressed, uint32_t* compressed)
{
   bool rval;

   // create output stream
   OutputStream* os = (b == NULL) ?
      static_cast<OutputStream*>(new NullOutputStream()) :
      static_cast<OutputStream*>(new ByteArrayOutputStream(b, false));

   // zlib-compress data
   Deflater def;
   def.startDeflating(-1, false);
   MutatorOutputStream mos(os, true, &def, false);

   // produce JSON-formatted contract data
   JsonWriter writer;
   writer.setCompact(true);
   rval = writer.write(mContract, &mos);
   mos.close();

   // store uncompressed bytes
   if(uncompressed != NULL)
   {
      *uncompressed = def.getTotalInputBytes();
   }

   // store compressed bytes
   if(compressed != NULL)
   {
      *compressed = def.getTotalOutputBytes();
   }

   return rval;
}
// FIXME: This code has an issue if the config file has a changed id or
// FIXME: changed contents since it was first loaded. Just assuming this code
// FIXME: is the only code that manages the file for now.
bool NodeConfigManager::saveSystemUserConfig()
{
   bool rval;
   Config cfg = getNodeConfig();
   const char* suPath;
   string path;

   rval = !cfg.isNull();

   if(rval)
   {
      // get system user config and expand any home path
      suPath = cfg["systemUserConfig"]->getString();
      rval = expandBitmunkHomePath(suPath, path);
   }

   if(rval)
   {
      // FIXME: do we need locking for load/save/get/remove?
      MO_CAT_DEBUG(BM_NODE_CAT,
         "NodeConfigManager: save system user config: path=%s",
         path.c_str());
      // read
      File file(path.c_str());
      // new config
      Config c;
      if(file->exists())
      {
         FileInputStream fis(file);
         JsonReader reader;
         reader.start(c);
         rval = reader.read(&fis) && reader.finish();
         fis.close();
      }
      else
      {
         c[ConfigManager::VERSION] = MO_DEFAULT_CONFIG_VERSION;
         c[ConfigManager::ID] = "custom system user";
         c[ConfigManager::GROUP] = "system user";
      }
      // update
      if(rval &&
         getConfigManager()->hasConfig(c[ConfigManager::ID]->getString()))
      {
         // get old raw config
         Config active = getConfigManager()->getConfig(
            c[ConfigManager::ID]->getString(), true);
         c.merge(active, false);
      }
      // backup old file
      if(rval && file->exists())
      {
         string backupPath = path + ".bak";
         File backupFile(backupPath.c_str());
         rval = file->rename(backupFile);
      }
      // make sure dirs exist
      rval = rval && file->mkdirs();
      // write
      if(rval)
      {
         FileOutputStream fos(file);
         JsonWriter writer;
         writer.setCompact(false);
         rval = writer.write(c, &fos);
         fos.close();
      }
   }

   if(!rval)
   {
      ExceptionRef e = new Exception(
         "Could not save system user config.",
         "bitmunk.node.NodeConfigManager.ConfigError");
      e->getDetails()["path"] = path.c_str();
      Exception::push(e);
   }

   return rval;
}