// Copyright 2004-present Facebook. All Rights Reserved.

#include "ApkManager.h"

#include <boost/filesystem.hpp>
#include <iostream>
#include <sstream>

namespace {

bool check_directory(std::string& apkdir) {
  if (!boost::filesystem::is_directory(apkdir.c_str())) {
    std::cerr << "error: apkdir is not a writable directory: " << apkdir
              << std::endl;
    exit(EXIT_FAILURE);
  }
}

}

FILE* ApkManager::new_asset_file(const char* filename) {
  check_directory(m_apk_dir);
  std::ostringstream path;
  path << m_apk_dir << "/assets/";
  if (!boost::filesystem::is_directory(path.str().c_str())) {
    std::cerr << "error: assets dir is not a writable directory: " << path.str()
              << std::endl;
    exit(EXIT_FAILURE);
  }
  path << filename;

  FILE* fd = fopen(path.str().c_str(), "w");
  if (fd != nullptr) {
    m_files.emplace_back(fd);
  } else {
    perror("Error creating new asset file");
  }
  return fd;
}

// Copyright 2004-present Facebook. All Rights Reserved.

#include "ApkManager.h"

#include <boost/filesystem.hpp>
#include <iostream>
#include <sstream>

namespace {

void check_directory(std::string& dir) {
  if (!boost::filesystem::is_directory(dir.c_str())) {
    std::cerr << "error: not a writable directory: " << dir
              << std::endl;
    exit(EXIT_FAILURE);
  }
}

}

FILE* ApkManager::new_asset_file(const char* filename) {
  check_directory(m_apk_dir);
  std::ostringstream path;
  path << m_apk_dir << "/assets/";
  std::string assets_dir = path.str();
  check_directory(assets_dir);
  path << filename;

  FILE* fd = fopen(path.str().c_str(), "w");
  if (fd != nullptr) {
    m_files.emplace_back(fd);
  } else {
    perror("Error creating new asset file");
  }
  return fd;
}

Fix compilation warning when checking for writable directory
Fix compilation warning when checking for writable directory

Summary:
As title. Fixes code introduced in previous diff.

Fixes https://github.com/facebook/redex/issues/312

Reviewed By: thezhangwei

Differential Revision: D7487716

fbshipit-source-id: 12d1fa38b4cc9b10b31991a9875731b19988c96d
