virgo_error_t* virgo__versions_latest_file(virgo_t *v, const char *path, is_file_cmp file_compare, char *buffer, size_t buffer_len) { char *latest = NULL; char *ptr; int rc, i; uv_fs_t readdir_req; virgo_error_t *err; rc = uv_fs_readdir(uv_default_loop(), &readdir_req, path, 0, NULL); if (!rc) { return virgo_error_create(-1, "readdir returned 0"); } ptr = readdir_req.ptr; for (i=0; i < rc; i++) { int comparison; /* Verify this is a bundle filename */ if (!file_compare(ptr)) { goto next; } /* Initial pass */ if (!latest) { latest = ptr; goto next; } /* Perform the comparison */ err = compare_files(ptr, latest, &comparison); if (err) { virgo_error_clear(err); goto next; } /* If comparison returns 1, then the versions are greater */ if (comparison == 1) { latest = ptr; } next: ptr = ptr + strlen(ptr) + 1; } if (!latest) { uv_fs_req_cleanup(&readdir_req); return virgo_error_create(VIRGO_ENOFILE, "zero files"); } /* Save off the path */ snprintf(buffer, buffer_len, "%s%s%s", path, SEP, latest); uv_fs_req_cleanup(&readdir_req); return VIRGO_SUCCESS; }
virgo_error_t* virgo__lua_run(virgo_t *v) { int rv; const char* lua_err; lua_State* L = v->L; virgo__set_virgo_key(L, "loaded_zip_path", v->lua_load_path); lua_getglobal(L, "require"); lua_pushliteral(L, "virgo_init"); lua_call(L, 1, 1); /* push on the error handler */ lua_pushcfunction(L, virgo__lua_handle_crash); lua_getglobal(L, "virgo_entry"); if (!lua_isfunction(L, -1)) { return virgo_error_create(VIRGO_EINVAL, "virgo_init.lua was not properly installed"); } lua_pushstring(L, v->lua_default_module); /* pcall virgo.run(default) with error handler handle_crash */ rv = lua_pcall(L, 1, 0, -3); if (rv != 0) { lua_err = lua_tostring(v->L, -1); return virgo_error_createf(VIRGO_EINVAL, "\nLua Runtime Error: %s", lua_err); } return VIRGO_SUCCESS; }
virgo_error_t* virgo_init(virgo_t *v) { virgo_error_t* err; if (virgo__argv_has_flag(v, "-h", "--help") == 1) { return virgo_error_create(VIRGO_EHELPREQ, "--help was passed");; } if (virgo__argv_has_flag(v, "-v", "--version") == 1) { return virgo_error_create(VIRGO_EVERSIONREQ, "--version was passed");; } #ifdef _WIN32 if (virgo__argv_has_flag(v, NULL, "--service-install") == 1) { return virgo__service_install(v); } if (virgo__argv_has_flag(v, NULL, "--service-delete") == 1) { return virgo__service_delete(v); } #endif err = virgo__lua_init(v); if (err ) { return err; } err = virgo__agent_conf_init(v); if (err) { return err; } return VIRGO_SUCCESS; }
virgo_error_t* virgo__temp_dir_get(char **temp_dir) { const char *try_dirs[] = { "/tmp", "/usr/tmp", "/var/tmp" }; const char *try_envs[] = { "TMPDIR", "TMP", "TEMP"}; const char *dir; size_t i; /* Our goal is to find a temporary directory suitable for writing into. Here's the order in which we'll try various paths: $TMPDIR $TMP $TEMP "C:\TEMP" (windows only) "SYS:\TMP" (netware only) "/tmp" "/var/tmp" "/usr/tmp" P_tmpdir (POSIX define) `pwd` NOTE: This algorithm is basically the same one used by Python 2.2's tempfile.py module. */ /* Try the environment first. */ for (i = 0; i < (sizeof(try_envs) / sizeof(const char *)); i++) { char *value; value = getenv(try_envs[i]); if (value) { size_t len = strlen(value); if (len && (len < VIRGO_PATH_MAX) && !test_tempdir(value)) { dir = value; goto end; } } } #ifdef WIN32 /* Next, on Win32, try the C:\TEMP directory. */ if (!test_tempdir("C:\\TEMP")) { dir = "C:\\TEMP"; goto end; } #endif #ifdef NETWARE /* Next, on NetWare, try the SYS:/TMP directory. */ if (!test_tempdir("SYS:/TMP")) { dir = "SYS:/TMP"; goto end; } #endif /* Next, try a set of hard-coded paths. */ for (i = 0; i < (sizeof(try_dirs) / sizeof(const char *)); i++) { if (!test_tempdir(try_dirs[i])) { dir = try_dirs[i]; goto end; } } #ifdef P_tmpdir /* * If we have it, use the POSIX definition of where * the tmpdir should be */ if (!test_tempdir(P_tmpdir)) { dir = P_tmpdir; goto end; } #endif return virgo_error_create(VIRGO_EINVAL, "Unable to detect temporary directory."); end: *temp_dir = strdup(dir); return VIRGO_SUCCESS; }