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__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 <= 0) { return virgo_error_createf(-1, "readdir returned %u", rc); } 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__bundle_is_valid(virgo_t *v) { int fd; /* Ensure we can read the zip file */ fd = open(virgo_get_load_path(v), O_RDONLY); if (fd < 0) { return virgo_error_createf(-1, "Error: zip can't be opened %s", virgo_get_load_path(v)); } close(fd); return VIRGO_SUCCESS; }
virgo_error_t* virgo__log_rotate(virgo_t *v) { FILE *old = v->log_fp; FILE *nxt = stderr; if (v->log_path != NULL) { nxt = fopen(v->log_path, "ab"); if (nxt == NULL) { char buf[256]; int err = errno; #ifdef _WIN32 strncpy(&buf[0], strerror(err), sizeof(buf)); #else strerror_r(err, &buf[0], sizeof(buf)); #endif logCrit(v, "Failed to open log file: %s (errno=%d,%s)", v->log_path, err, &buf[0]); return virgo_error_createf(VIRGO_EIO, "Failed to open log file: %s (errno=%d,%s)", v->log_path, err, &buf[0]); } } v->log_fp = nxt; if (old != NULL && old != stderr) { fclose(old); } if (v->log_path) { virgo_log_infof(v, "Log file started (path=%s)", v->log_path); } return VIRGO_SUCCESS; }