char* Ntop::getValidPath(char *__path) { char _path[MAX_PATH]; struct stat buf; #ifdef WIN32 const char *install_dir = (const char *)get_install_dir(); #endif const char* dirs[] = { startup_dir, #ifndef WIN32 "/usr/local/share/ntopng", CONST_DEFAULT_INSTALL_DIR, #else install_dir, #endif NULL }; if(strncmp(__path, "./", 2) == 0) { snprintf(_path, MAX_PATH, "%s/%s", startup_dir, &__path[2]); fixPath(_path); if(stat(_path, &buf) == 0) { free(__path); return(strdup(_path)); } } if((__path[0] == '/') || (__path[0] == '\\')) { /* Absolute paths */ if(stat(__path, &buf) == 0) { return(__path); } } else snprintf(_path, MAX_PATH, "%s", __path); /* relative paths */ for(int i=0; dirs[i] != NULL; i++) { char path[MAX_PATH]; snprintf(path, sizeof(path), "%s/%s", dirs[i], _path); fixPath(path); if(stat(path, &buf) == 0) { free(__path); return(strdup(path)); } } free(__path); return(strdup("")); }
/* The configure-generated path macros might have '@' at the beginning, indicating the runtime directory. */ void maybe_prepend_install_dir(const char *orig, char *buf, int buflen, void (*errfn)(const char *, ...)) { if (*orig == '@') { int len = get_install_dir(buf, buflen, errfn); if (len + (int)strlen(orig) > buflen) { errfn("Pathname too long: %s", orig); } strcat(buf, orig+1); } else { if ((int)strlen(orig) >= buflen-1) { errfn("Pathname too long: %s", orig); } strcpy(buf, orig); } }
/* On windows and darwin, this returns the runtime's prefix directory that can be replaced with '@'. Note that it calls errfn on other platforms. Eventually we want to cover more platforms, for getting directory of the running binary is sometimes useful in general. */ void Scm_GetRuntimeDirectory(char *buf, int buflen, void (*errfn)(const char *, ...)) { get_install_dir(buf, buflen, errfn); }
gboolean synce_app_man_install(IRAPISession *session, const gchar *filepath, SynceAppManBusyFunc busy_func, gpointer busy_data, GError **error) { gchar *install_path = NULL; SYSTEM_INFO system; gboolean result; WCHAR* wide_program = NULL; PROCESS_INFORMATION info; BOOL rapi_res; HRESULT hr; DWORD last_error; gchar *tmpdir = NULL; GList *cab_list = NULL; g_return_val_if_fail (error == NULL || *error == NULL, FALSE); if (busy_func) (*busy_func)(busy_data); memset(&system, 0, sizeof(system)); IRAPISession_CeGetSystemInfo(session, &system); #if GLIB_CHECK_VERSION(2,30,0) tmpdir = g_dir_make_tmp("sti_XXXXXX", error); if (!tmpdir) { g_prefix_error(error, _("Failed to create temp directory '%s': "), tmpdir); result = FALSE; goto exit; } g_chmod(tmpdir, 0700); #else tmpdir = tempnam(g_get_tmp_dir(), "sti"); if (g_mkdir(tmpdir, 0700) != 0) { g_set_error(error, G_FILE_ERROR, g_file_error_from_errno(errno), _("Failed to create temp directory '%s': %s"), tmpdir, g_strerror(errno)); result = FALSE; goto exit; } #endif cab_list = extract_with_orange(filepath, tmpdir, system.dwProcessorType); if (!cab_list) { g_set_error(error, SYNCE_APP_MAN_ERROR, SYNCE_APP_MAN_ERROR_INVALID_INSTALL_FILE, _("No CAB files found")); return FALSE; } /* Do some install things */ install_path = get_install_dir(session, error); if (!install_path) { result = FALSE; goto exit; } GList *tmplist = g_list_first(cab_list); while(tmplist) { gchar *cabname = tmplist->data; g_debug("%s: copying file %s to device", G_STRFUNC, cabname); if (!copy_to_device(session, cabname, install_path, busy_func, busy_data, error)) { result = FALSE; goto exit; } tmplist = g_list_next(tmplist); } wide_program = wstr_from_utf8("wceload.exe"); memset(&info, 0, sizeof(info)); rapi_res = IRAPISession_CeCreateProcess(session, wide_program, NULL, NULL, NULL, false, 0, NULL, NULL, NULL, &info); wstr_free_string(wide_program); IRAPISession_CeCloseHandle(session, info.hProcess); IRAPISession_CeCloseHandle(session, info.hThread); if (!rapi_res) { if (FAILED(hr = IRAPISession_CeRapiGetError(session))) { g_set_error(error, SYNCE_APP_MAN_ERROR, SYNCE_APP_MAN_ERROR_RAPI_TERM, _("Failed to execute installer: %s"), synce_strerror(hr)); result = FALSE; goto exit; } last_error = IRAPISession_CeGetLastError(session); g_set_error(error, SYNCE_APP_MAN_ERROR, SYNCE_APP_MAN_ERROR_RAPI, _("Failed to execute installer: %s"), synce_strerror(last_error)); result = FALSE; goto exit; } g_debug("%s: successfully installed %s on device", G_STRFUNC, filepath); result = TRUE; exit: tmplist = g_list_first(cab_list); while(tmplist) { g_unlink((gchar *)(tmplist->data)); tmplist = g_list_next(tmplist); } if (tmpdir) { g_rmdir(tmpdir); g_free(tmpdir); } return result; }
char *file_find(const char *filename) { size_t i, max_len; char *full_path = (char*) calloc(PATH_MAX_LEN + 1, 1); const char *env_var = getenv("BLADERF_SEARCH_DIR"); /* Check directory specified by environment variable */ if (env_var != NULL) { strncat(full_path, env_var, PATH_MAX_LEN - 1); full_path[strlen(full_path)] = DIR_DELIMETER; max_len = PATH_MAX_LEN - strlen(full_path); if (max_len >= strlen(filename)) { strncat(full_path, filename, max_len); if (access(full_path, ACCESS_FILE_EXISTS) != -1) { return full_path; } } } /* Check the directory containing the currently running binary */ memset(full_path, 0, PATH_MAX_LEN); max_len = PATH_MAX_LEN - 1; if (get_binary_dir(full_path, max_len) != 0) { max_len -= strlen(full_path); if (max_len >= strlen(filename)) { strncat(full_path, filename, max_len); if (access(full_path, ACCESS_FILE_EXISTS) != -1) { return full_path; } } else { log_debug("Skipping search for %s in %s. " "Path would be truncated.\n", filename, full_path); } } /* Search our list of pre-determined paths */ for (i = 0; i < ARRAY_SIZE(search_paths); i++) { memset(full_path, 0, PATH_MAX_LEN); max_len = PATH_MAX_LEN; if (search_paths[i].prepend_home) { const size_t len = get_home_dir(full_path, max_len); if (len != 0) { max_len -= len; } else { continue; } } strncat(full_path, search_paths[i].path, max_len); max_len = PATH_MAX_LEN - strlen(full_path); if (max_len >= strlen(filename)) { strncat(full_path, filename, max_len); if (access(full_path, ACCESS_FILE_EXISTS) != -1) { return full_path; } } else { log_debug("Skipping search for %s in %s. " "Path would be truncated.\n", filename, full_path); } } /* Search the installation directory, if applicable */ if (get_install_dir(full_path, PATH_MAX_LEN)) { max_len = PATH_MAX_LEN - strlen(full_path); if (max_len >= strlen(filename)) { strncat(full_path, filename, max_len); if (access(full_path, ACCESS_FILE_EXISTS) != -1) { return full_path; } } else { log_debug("Skipping search for %s in %s. " "Path would be truncated.\n", filename, full_path); } } free(full_path); return NULL; }