Ejemplo n.º 1
0
    bool KeywordGenerator::updateFile(const std::stringstream& newContent , const std::string& filename) {
        ensurePath(filename);
        std::ofstream outputStream(filename);
        outputStream << newContent.str();

        return true;
    }
Ejemplo n.º 2
0
void CConfigGenEngine::createFakePlugins(StringBuffer& destFilePath) const
{
    String destFilePathStr(destFilePath);
    String* tmpstr = destFilePathStr.toLowerCase();
    if (!tmpstr->endsWith("plugins.xml"))
    {
        int index = tmpstr->indexOf("plugins.xml");
        destFilePath.remove(index + 11, destFilePath.length() - (index + 11));
    }

    delete tmpstr;

    StringBuffer tmpoutbuf("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Plugins/>");

    if (m_instances.ordinality() > 1 && strcmp(m_process.queryName(), XML_TAG_ESPPROCESS))
        destFilePath.replaceString("@temp"PATHSEPSTR, m_cachePath);
    else
    {
        char tempPath[_MAX_PATH];
        getTempPath(tempPath, sizeof(tempPath), m_name);
        ensurePath(tempPath);
        destFilePath.replaceString("@temp"PATHSEPSTR, tempPath);
    }

    Owned<IFile> pTargetFile = createIFile(destFilePath.str());
    if (pTargetFile->exists() && pTargetFile->isReadOnly())
        pTargetFile->setReadOnly(false);
    Owned<IFileIO> pTargetFileIO = pTargetFile->open(IFOcreate);
    pTargetFileIO->write( 0, tmpoutbuf.length(), tmpoutbuf.str());
    m_envDepEngine.addTempFile(destFilePath.str());
}
Ejemplo n.º 3
0
bool Os::ensurePath(const std::string& dirPath)
{
  struct stat st;
  if ( stat(dirPath.c_str(),&st)==0 )
    return true;
  
  size_t sep = dirPath.rfind(Os::pathSep);
  if ( sep != std::string::npos && sep > 0 ) {
      if (!ensurePath(dirPath.substr(0,sep)))
	  return false;
  }

  return mkdir( dirPath.c_str(), 0755) == 0;
}
Ejemplo n.º 4
0
    bool KeywordGenerator::updateFile(const std::stringstream& newContent , const std::string& filename) {
        bool update = true;
        {
            // Check if file already contains the newContent.
            std::ifstream inputStream(filename);
            if (inputStream) {
                std::stringstream oldContent;
                oldContent << inputStream.rdbuf();
                if (oldContent.str() == newContent.str()) {
                    update = false;
                }
            }
        }

        if (update) {
            ensurePath(filename);
            std::ofstream outputStream(filename);
            outputStream << newContent.str();
        }

        return update;
    }
Ejemplo n.º 5
0
//---------------------------------------------------------------------------
//  beforeDeploy
//---------------------------------------------------------------------------
void CConfigGenEngine::beforeDeploy()
{
    m_installFiles.clear();

    char tempPath[_MAX_PATH];
    getTempPath(tempPath, sizeof(tempPath), m_name);
    m_envDepEngine.addTempDirectory( tempPath );

    if (m_instances.ordinality() > 1)
    {
        strcat(tempPath, "Cache");
        char* pszEnd = tempPath + strlen(tempPath);

        Owned<IFile> pFile = createIFile(tempPath);
        int i = 1;

        while (pFile->exists()) { //dir/file exists
            itoa(++i, pszEnd, 10);
            pFile.setown( createIFile(tempPath) );
        }

        strcat(tempPath, PATHSEPSTR);
        m_cachePath.set( tempPath );
    }
    else
        m_cachePath.set( tempPath );

    ensurePath(tempPath);
    determineInstallFiles(m_process, m_installFiles);
    getCallback().installFileListChanged();

    if (m_instances.ordinality() > 1)
    {
        EnvMachineOS os = m_envDepEngine.lookupMachineOS( m_instances.item(0) );
        m_curInstance = "Cache";
        copyInstallFiles("Cache", -1, tempPath, os);
    }
}
Ejemplo n.º 6
0
bool Os::ensurePath(const std::string& path)
{
    if (!exists(path)) {
        size_t sep = path.rfind(Os::pathSep);
        if ( sep != std::string::npos && sep > 0 ) {
            ensurePath(path.substr(0,sep));
        }
#ifdef __WIN32__
	if (mkdir(path.c_str()) != 0 ) {
#else
        if (mkdir(path.c_str(), 0755) != 0) {
#endif
            LOG_WARNING("Failed to create dir: %s", path.c_str());
            return false;
        } else {
            LOG_DEBUG("Created dir %s", path.c_str());
            return true;
        }
    }
    return true;
}


bool Os::exists(const std::string& file)
{
    struct stat st;
    return stat(file.c_str(),&st) == 0;
}

static Os *
g_os = nullptr;

const char
#ifdef __WIN32__
Os::pathSep = '\\';
#else
Os::pathSep = '/';
#endif

Os::Os()
{
    if (g_os != nullptr) {
        LOG_FATAL("OS instance already exists!");
    }

    g_os = this;
}

Os *
Os::get()
{
    return g_os;
}

static std::string
g_appDir;

static std::string
g_appName;

static long
log_get_ticks()
{
    return OS->ticks();
}

void
Os::init(int argc, char **argv)
{
    PetalsLog::init(log_get_ticks, thp::format);

    char buf[PATH_MAX];

    const char *progname = argv[0];
    const char *slash = strrchr(progname, Os::pathSep);

    if (!slash) {
#ifdef __WIN32__
	if (GetModuleFileName(NULL, buf, sizeof(buf)) != -1) {
#else
        if (readlink("/proc/self/exe", buf, sizeof(buf)) != -1) {
#endif
            progname = buf;
            slash = strrchr(progname, Os::pathSep);
        }
    }

    if (!slash) {
        throw "Could not determine application path";
    }

    g_appName = slash + 1;
    g_appDir = std::string(progname, slash-progname);
}

std::string
Os::appName()
{
    return g_appName;
}

std::string
Os::globalDataDir()
{
    std::string sourceData = thp::format("%s/data", g_appDir.c_str());

    // Prefer './data' in the source checkout if available
    if (exists(sourceData)) {
        return sourceData;
    }

    // System-wide installation
    return thp::format("%s/../share/%s/data", g_appDir.c_str(), appName().c_str());
}
Ejemplo n.º 7
0
int main(int argc, char **argv)
{
  nonportFail = testingFail;

  char s[4];
  s[0] = '-';
  s[1] = 'l';
  s[2] = 's';
  s[3] = 0;
  if (0!=strcmp(s, "-ls")) {
    printf("strcmp failed!\n");
    return 4;
  }

  // process arguments
  bool interactive = false;
  for (int i=1; i<argc; i++) {
    if (0==strcmp("-ls", argv[i])) {
      // do an ls, and bail
      applyToCwdContents(printIt);
      return 0;
    }
    else if (0==strcmp("-noninteractive", argv[i])) {
      // don't do the interactive stuff
      interactive = false;
    }
    else {
      printf("unknown option: %s\n", argv[i]);
      return 2;
    }
  }

  // trying to figure out why backspace sometimes gives ^? crap
  // (turns out Konsole sometimes sends ^? in response to BS,
  // even when the option looks unchecked)
  //char buf[80];
  //printf("type stuff and try backspace: ");
  //gets(buf);
  //printf("%s (%d chars)\n", buf, strlen(buf));
  //return 0;

  long startTime = getMilliseconds();

  if (interactive) {
    printf("Type some characters; you should see each\n"
	   "character echoed once as you type it (q to stop):\n");
    setRawMode(true);
    char ch;
    do {
      ch = getConsoleChar();
      printf("%c", ch);
    } while (ch != 'q');

    setRawMode(false);

    printf("\n\nYou typed for %ld milliseconds\n",
	   getMilliseconds() - startTime);
  }

  limitFileAccess("chmod.test");

  printf("if the current dir contains a file called "
         "chmod.test, I just attempted to limit\n"
         "its access to just the owner\n");

  createDirectory("test.dir");

  // test chdir, which also implicitly tests mkdir
  bool didFirst=false;
  if (!changeDirectory("test.dir") || (didFirst=true, false) ||
      !changeDirectory("..")) {
    printf("failed while trying to chdir to %s\n",
           (didFirst? ".." : "test.dir"));
  }

  // more straightforward
  if (!fileOrDirectoryExists("test.dir")) {
    printf("test.dir didn't get created?\n");
  }

  printf("what's more, I just tried to mkdir & chdir test.dir\n");

  // test ensurePath
  if (!ensurePath("test.dir/a/b/c/d", false /*isDirectory*/)) {
    printf("ensurePath test.dir/a/b/c/d failed\n");
  }

  // try to list partial directory contents
  printf("listing of first 10 files in this directory:\n");
  {
    int count = 0;
    applyToCwdContents(printFirst10, &count);
  }

  // test date function
  {
    int m, d, y;
    getCurrentDate(m, d, y);

    printf("I think the date is (m/d/yyyy): %d/%d/%d\n",
           m, d, y);
  }

  // test sleep (mostly just to make sure it doesn't segfault)
  printf("sleeping for 1 second...\n");
  portableSleep(1);

  // test user name
  char buf[80];
  getCurrentUsername(buf, 80);
  printf("current user name is: %s\n", buf);

  if (interactive) {
    // test nonecho reading
    printf("Type something and press Enter; it won't be echoed (yet):\n");
    readNonechoString(buf, 80, "  > ");
    printf("You typed: %s\n", buf);
  }

  // test random stuff
  printf("hasSystemCryptoRandom: ");
  if (!hasSystemCryptoRandom()) {
    printf("no\n");
  }
  else {
    printf("yes\n");

    printf("three random numbers: %u %u %u\n",
           getSystemCryptoRandom(),
           getSystemCryptoRandom(),
           getSystemCryptoRandom());
  }

  printf("testing nprintf...\n");
  nprintfVector("simple");
  nprintfVector("a %s more", "little");
  nprintfVector("some %4d more %s complicated %c stuff",
                33, "yikes", 'f');
  nprintfVector("%f", 3.4);

  printf("nonport works\n");
  return 0;
}