# include "config.h"
#endif
+/*
+ * Required with GNU libc to get dladdr().
+ * We define it here because <dlfcn.h> apparently gets included by
+ * one of the headers we include below.
+ */
+#define _GNU_SOURCE
+
#ifdef HAVE_DIRENT_H
#include <dirent.h>
#endif
#include <unistd.h>
#endif
+#ifdef HAVE_FCNTL_H
+#include <fcntl.h>
+#endif
+
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#include <windows.h>
#include <tchar.h>
#include <shlobj.h>
-#include "epan/unicode-utils.h"
-#else
+#include <wsutil/unicode-utils.h>
+#else /* _WIN32 */
+#ifdef DLADDR_FINDS_EXECUTABLE_PATH
+#include <dlfcn.h>
+#endif /* DLADDR_FINDS_EXECUTABLE_PATH */
#include <pwd.h>
-#endif
+#endif /* _WIN32 */
#include "filesystem.h"
-#include "privileges.h"
-#include <wiretap/file_util.h>
+#include "report_err.h"
+#include <wsutil/privileges.h>
+#include <wsutil/file_util.h>
+
+#include <wiretap/wtap.h> /* for WTAP_ERR_SHORT_WRITE */
#define PROFILES_DIR "profiles"
+#define PLUGINS_DIR_NAME "plugins"
+
#define U3_MY_CAPTURES "\\My Captures"
char *persconffile_dir = NULL;
char *persdatafile_dir = NULL;
char *persconfprofile = NULL;
+static gboolean do_store_persconffiles = FALSE;
+static GHashTable *profile_files = NULL;
+
/*
* Given a pathname, return a pointer to the last pathname separator
* character in the pathname, or NULL if the pathname contains no
* separators.
*/
-static char *
+char *
find_last_pathname_separator(const char *path)
{
char *separator;
{
struct stat statb;
- if (eth_stat(path, &statb) < 0)
+ if (ws_stat(path, &statb) < 0)
return errno;
if (S_ISDIR(statb.st_mode))
{
struct stat statb;
- if (eth_stat(path, &statb) < 0)
+ if (ws_stat(path, &statb) < 0)
return errno;
if (S_ISFIFO(statb.st_mode))
static char *progfile_dir;
/*
- * TRUE if we're running from the build directory.
+ * TRUE if we're running from the build directory and we aren't running
+ * with special privileges.
*/
static gboolean running_in_build_directory_flag = FALSE;
#ifdef _WIN32
_U_
#endif
+, int (*main_addr)(int, char **)
+#if defined(_WIN32) || !defined(DLADDR_FINDS_EXECUTABLE_PATH)
+ _U_
+#endif
)
{
char *dir_end;
#ifdef _WIN32
TCHAR prog_pathname_w[_MAX_PATH+2];
size_t progfile_dir_len;
- char *prog_pathname;
- DWORD error;
- TCHAR *msg_w;
- guchar *msg;
- size_t msglen;
+ char *prog_pathname;
+ DWORD error;
+ TCHAR *msg_w;
+ guchar *msg;
+ size_t msglen;
/*
* Attempt to get the full pathname of the currently running
* program.
*/
- if (GetModuleFileName(NULL, prog_pathname_w, sizeof prog_pathname_w) != 0) {
- /*
- * XXX - Should we use g_utf16_to_utf8(), as in
- * getenv_utf8()?
- */
- prog_pathname = utf_16to8(prog_pathname_w);
+ if (GetModuleFileName(NULL, prog_pathname_w, G_N_ELEMENTS(prog_pathname_w)) != 0 && GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
+ /*
+ * XXX - Should we use g_utf16_to_utf8(), as in
+ * getenv_utf8()?
+ */
+ prog_pathname = utf_16to8(prog_pathname_w);
/*
* We got it; strip off the last component, which would be
* the file name of the executable, giving us the pathname
msg, error);
}
#else
+#ifdef DLADDR_FINDS_EXECUTABLE_PATH
+ Dl_info info;
+#endif
char *prog_pathname;
char *curdir;
long path_max;
&& !started_with_special_privs())
running_in_build_directory_flag = TRUE;
+#ifdef DLADDR_FINDS_EXECUTABLE_PATH
+ /*
+ * Try to use dladdr() to find the pathname of the executable.
+ * dladdr() is not guaranteed to give you anything better than
+ * argv[0] (i.e., it might not contain a / at all, much less
+ * being an absolute path), and doesn't appear to do so on
+ * Linux, but on other platforms it could give you an absolute
+ * path and obviate the need for us to determine the absolute
+ * path.
+ */
+ if (dladdr((void *)main_addr, &info))
+ arg0 = info.dli_fname;
+#endif
/*
* Try to figure out the directory in which the currently running
- * program resides, given the argv[0] it was started with. That
- * might be the absolute path of the program, or a path relative
+ * program resides, given something purporting to be the executable
+ * name (from dladdr() or from the argv[0] it was started with.
+ * That might be the absolute path of the program, or a path relative
* to the current directory of the process that started it, or
* just a name for the program if it was started from the command
* line and was searched for in $PATH. It's not guaranteed to be
return g_strdup_printf("pathconf failed: %s\n",
strerror(errno));
}
- curdir = g_malloc(path_max);
+ curdir = (char *)g_malloc(path_max);
if (getcwd(curdir, path_max) == NULL) {
/*
* It failed - give up, and just stick
if (path_end == NULL)
path_end = path_start + strlen(path_start);
path_component_len = path_end - path_start;
- path = g_malloc(path_component_len + 1
+ path = (char *)g_malloc(path_component_len + 1
+ strlen(arg0) + 1);
memcpy(path, path_start, path_component_len);
path[path_component_len] = '\0';
#ifdef _WIN32
char *u3deviceexecpath;
#endif
- static char *datafile_dir = NULL;
+ static const char *datafile_dir = NULL;
if (datafile_dir != NULL)
return datafile_dir;
datafile_dir = progfile_dir;
} else {
/*
- * Return the directory specified when the build
- * was configured.
+ * Return the directory specified when the build was
+ * configured, prepending the run path prefix if it exists.
*/
- datafile_dir = DATAFILE_DIR;
+ if (getenv("WIRESHARK_DATA_DIR") && !started_with_special_privs()) {
+ /*
+ * The user specified a different directory for data files
+ * and we aren't running with special privileges.
+ * XXX - We might be able to dispense with the priv check
+ */
+ datafile_dir = g_strdup(getenv("WIRESHARK_DATA_DIR"));
+ } else {
+ datafile_dir = DATAFILE_DIR;
+ }
}
#endif
return datafile_dir;
}
-#ifdef HAVE_PLUGINS
+#ifdef HAVE_PYTHON
+/*
+ * Find the directory where the python dissectors are stored.
+ *
+ * On Windows, we use the "py_dissector" subdirectory of the datafile directory.
+ *
+ * On UN*X, we use the PYTHON_DIR value supplied by the configure
+ * script, unless we think we're being run from the build directory,
+ * in which case we use the "py_dissector" subdirectory of the datafile directory.
+ *
+ * In both cases, we then use the subdirectory of that directory whose
+ * name is the version number.
+ *
+ * XXX - if we think we're being run from the build directory, perhaps we
+ * should have the plugin code not look in the version subdirectory
+ * of the plugin directory, but look in all of the subdirectories
+ * of the plugin directory, so it can just fetch the plugins built
+ * as part of the build process.
+ */
+static const char *wspython_dir = NULL;
+
+static void
+init_wspython_dir(void)
+{
+#ifdef _WIN32
+ /*
+ * On Windows, the data file directory is the installation
+ * directory; the python dissectors are stored under it.
+ *
+ * Assume we're running the installed version of Wireshark;
+ * on Windows, the data file directory is the directory
+ * in which the Wireshark binary resides.
+ */
+ wspython_dir = g_strdup_printf("%s\\python\\%s", get_datafile_dir(),
+ VERSION);
+
+ /*
+ * Make sure that pathname refers to a directory.
+ */
+ if (test_for_directory(wspython_dir) != EISDIR) {
+ /*
+ * Either it doesn't refer to a directory or it
+ * refers to something that doesn't exist.
+ *
+ * Assume that means we're running a version of
+ * Wireshark we've built in a build directory,
+ * in which case {datafile dir}\python is the
+ * top-level plugins source directory, and use
+ * that directory and set the "we're running in
+ * a build directory" flag, so the plugin
+ * scanner will check all subdirectories of that
+ * directory for python dissectors.
+ */
+ g_free( (gpointer) wspython_dir);
+ wspython_dir = g_strdup_printf("%s\\python", get_datafile_dir());
+ running_in_build_directory_flag = TRUE;
+ }
+#else
+ if (running_in_build_directory_flag) {
+ /*
+ * We're (probably) being run from the build directory and
+ * weren't started with special privileges, so we'll use
+ * the "python" subdirectory of the datafile directory
+ * (the datafile directory is the build directory).
+ */
+ wspython_dir = g_strdup_printf("%s/epan/wspython/", get_datafile_dir());
+ } else {
+ if (getenv("WIRESHARK_PYTHON_DIR") && !started_with_special_privs()) {
+ /*
+ * The user specified a different directory for plugins
+ * and we aren't running with special privileges.
+ */
+ wspython_dir = g_strdup(getenv("WIRESHARK_PYTHON_DIR"));
+ } else {
+ wspython_dir = PYTHON_DIR;
+ }
+ }
+#endif
+}
+#endif /* HAVE_PYTHON */
+
+/*
+ * Get the directory in which the python dissectors are stored.
+ */
+const char *
+get_wspython_dir(void)
+{
+#ifdef HAVE_PYTHON
+ if (!wspython_dir) init_wspython_dir();
+ return wspython_dir;
+#else
+ return NULL;
+#endif
+}
+
+
+#if defined(HAVE_PLUGINS) || defined(HAVE_LUA_5_1)
/*
* Find the directory where the plugins are stored.
*
* (the datafile directory is the build directory).
*/
plugin_dir = g_strdup_printf("%s/plugins", get_datafile_dir());
- } else
- plugin_dir = PLUGIN_DIR;
+ } else {
+ if (getenv("WIRESHARK_PLUGIN_DIR") && !started_with_special_privs()) {
+ /*
+ * The user specified a different directory for plugins
+ * and we aren't running with special privileges.
+ */
+ plugin_dir = g_strdup(getenv("WIRESHARK_PLUGIN_DIR"));
+ } else {
+ plugin_dir = PLUGIN_DIR;
+ }
+ }
#endif
}
-#endif /* HAVE_PLUGINS */
+#endif /* HAVE_PLUGINS || HAVE_LUA_5_1 */
/*
* Get the directory in which the plugins are stored.
const char *
get_plugin_dir(void)
{
-#ifdef HAVE_PLUGINS
+#if defined(HAVE_PLUGINS) || defined(HAVE_LUA_5_1)
if (!plugin_dir) init_plugin_dir();
return plugin_dir;
#else
- return NULL;
+ return NULL;
#endif
}
envvar = getenv(varname);
/* since GLib 2.6 we need an utf8 version of the filename */
-#if (GLIB_MAJOR_VERSION > 2 || (GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION >= 6))
+#if GLIB_CHECK_VERSION(2,6,0)
/* using the wide char version of getenv should work under all circumstances */
/* convert given varname to utf16, needed by _wgetenv */
void
set_profile_name(const gchar *profilename)
{
- if (persconfprofile) {
- g_free (persconfprofile);
- }
+ g_free (persconfprofile);
- if (profilename && strlen(profilename) > 0 &&
+ if (profilename && strlen(profilename) > 0 &&
strcmp(profilename, DEFAULT_PROFILE) != 0) {
persconfprofile = g_strdup (profilename);
} else {
}
}
+void
+profile_store_persconffiles(gboolean store)
+{
+ if (store) {
+ profile_files = g_hash_table_new (g_str_hash, g_str_equal);
+ }
+ do_store_persconffiles = store;
+}
+
/*
* Get the directory in which personal configuration files reside;
* in UNIX-compatible systems, it's ".wireshark", under the user's home
{
static char *profiles_dir = NULL;
- if (profiles_dir) {
- g_free (profiles_dir);
- }
-
+ g_free (profiles_dir);
profiles_dir = g_strdup_printf ("%s%s%s", get_persconffile_dir_no_profile (),
G_DIR_SEPARATOR_S, PROFILES_DIR);
{
static char *persconffile_profile_dir = NULL;
- if (persconffile_profile_dir) {
- g_free (persconffile_profile_dir);
- }
+ g_free (persconffile_profile_dir);
if (profilename && strlen(profilename) > 0 &&
strcmp(profilename, DEFAULT_PROFILE) != 0) {
- persconffile_profile_dir = g_strdup_printf ("%s%s%s", get_profiles_dir (),
+ persconffile_profile_dir = g_strdup_printf ("%s%s%s", get_profiles_dir (),
G_DIR_SEPARATOR_S, profilename);
} else {
- persconffile_profile_dir = g_strdup_printf (get_persconffile_dir_no_profile ());
+ persconffile_profile_dir = g_strdup (get_persconffile_dir_no_profile ());
}
return persconffile_profile_dir;
static int
delete_directory (const char *directory, char **pf_dir_path_return)
{
- ETH_DIR *dir;
- ETH_DIRENT *file;
+ WS_DIR *dir;
+ WS_DIRENT *file;
gchar *filename;
int ret = 0;
- if ((dir = eth_dir_open(directory, 0, NULL)) != NULL) {
- while ((file = eth_dir_read_name(dir)) != NULL) {
- filename = g_strdup_printf ("%s%s%s", directory, G_DIR_SEPARATOR_S,
- eth_dir_get_name(file));
+ if ((dir = ws_dir_open(directory, 0, NULL)) != NULL) {
+ while ((file = ws_dir_read_name(dir)) != NULL) {
+ filename = g_strdup_printf ("%s%s%s", directory, G_DIR_SEPARATOR_S,
+ ws_dir_get_name(file));
if (test_for_directory(filename) != EISDIR) {
- ret = eth_remove(filename);
+ ret = ws_remove(filename);
#if 0
} else {
/* The user has manually created a directory in the profile directory */
}
g_free (filename);
}
- eth_dir_close(dir);
+ ws_dir_close(dir);
}
- if (ret == 0 && (ret = eth_remove(directory)) != 0) {
+ if (ret == 0 && (ret = ws_remove(directory)) != 0) {
*pf_dir_path_return = g_strdup (directory);
}
rename_persconffile_profile(const char *fromname, const char *toname,
char **pf_from_dir_path_return, char **pf_to_dir_path_return)
{
- char *from_dir = g_strdup (get_persconffile_dir(fromname));
- char *to_dir = g_strdup (get_persconffile_dir(toname));
- int ret = 0;
-
- ret = eth_rename (from_dir, to_dir);
- if (ret != 0) {
- *pf_from_dir_path_return = g_strdup (from_dir);
- *pf_to_dir_path_return = g_strdup (to_dir);
- }
+ char *from_dir = g_strdup (get_persconffile_dir(fromname));
+ char *to_dir = g_strdup (get_persconffile_dir(toname));
+ int ret = 0;
- g_free (from_dir);
- g_free (to_dir);
+ ret = ws_rename (from_dir, to_dir);
+ if (ret != 0) {
+ *pf_from_dir_path_return = g_strdup (from_dir);
+ *pf_to_dir_path_return = g_strdup (to_dir);
+ }
- return ret;
+ g_free (from_dir);
+ g_free (to_dir);
+
+ return ret;
}
/*
* If not then create it.
*/
pf_dir_path = get_profiles_dir ();
- if (eth_stat(pf_dir_path, &s_buf) != 0 && errno == ENOENT) {
- ret = eth_mkdir(pf_dir_path, 0755);
+ if (ws_stat(pf_dir_path, &s_buf) != 0 && errno == ENOENT) {
+ ret = ws_mkdir(pf_dir_path, 0755);
if (ret == -1) {
*pf_dir_path_return = g_strdup(pf_dir_path);
return ret;
}
pf_dir_path = get_persconffile_dir(profilename);
- if (eth_stat(pf_dir_path, &s_buf) != 0 && errno == ENOENT) {
+ if (ws_stat(pf_dir_path, &s_buf) != 0 && errno == ENOENT) {
#ifdef _WIN32
/*
* Does the parent directory of that directory
pf_dir_parent_path_len = strlen(pf_dir_parent_path);
if (pf_dir_parent_path_len > 0
&& pf_dir_parent_path[pf_dir_parent_path_len - 1] != ':'
- && eth_stat(pf_dir_parent_path, &s_buf) != 0) {
+ && ws_stat(pf_dir_parent_path, &s_buf) != 0) {
/*
* No, it doesn't exist - make it first.
*/
- ret = eth_mkdir(pf_dir_parent_path, 0755);
+ ret = ws_mkdir(pf_dir_parent_path, 0755);
if (ret == -1) {
*pf_dir_path_return = pf_dir_parent_path;
return -1;
}
}
g_free(pf_dir_path_copy);
- ret = eth_mkdir(pf_dir_path, 0755);
+ ret = ws_mkdir(pf_dir_path, 0755);
#else
- ret = eth_mkdir(pf_dir_path, 0755);
+ ret = ws_mkdir(pf_dir_path, 0755);
#endif
} else {
/*
return create_persconffile_profile(persconfprofile, pf_dir_path_return);
}
+#if ! GLIB_CHECK_VERSION(2,14,0)
+static void
+hash_table_get_keys(gpointer key, gpointer value _U_, gpointer user_data)
+{
+ GList **files = ((GList **)user_data);
+ *files = g_list_append (*files, key);
+}
+#endif
+
+int
+copy_persconffile_profile(const char *toname, const char *fromname, char **pf_filename_return,
+ char **pf_to_dir_path_return, char **pf_from_dir_path_return)
+{
+ gchar *from_dir = g_strdup (get_persconffile_dir(fromname));
+ gchar *to_dir = g_strdup (get_persconffile_dir(toname));
+ gchar *filename, *from_file, *to_file;
+ GList *files, *file;
+
+#if GLIB_CHECK_VERSION(2,14,0)
+ files = g_hash_table_get_keys(profile_files);
+#else
+ files = NULL;
+ g_hash_table_foreach(profile_files, hash_table_get_keys, &files);
+#endif
+
+ file = g_list_first(files);
+ while (file) {
+ filename = (gchar *)file->data;
+ from_file = g_strdup_printf ("%s%s%s", from_dir, G_DIR_SEPARATOR_S, filename);
+ to_file = g_strdup_printf ("%s%s%s", to_dir, G_DIR_SEPARATOR_S, filename);
+
+ if (file_exists(from_file) && !copy_file_binary_mode(from_file, to_file)) {
+ *pf_filename_return = g_strdup(filename);
+ *pf_to_dir_path_return = to_dir;
+ *pf_from_dir_path_return = from_dir;
+ g_free (from_file);
+ g_free (to_file);
+ return -1;
+ }
+
+ g_free (from_file);
+ g_free (to_file);
+
+ file = g_list_next(file);
+ }
+
+ g_list_free (files);
+ g_free (from_dir);
+ g_free (to_dir);
+
+ return 0;
+}
+
/*
* Get the (default) directory in which personal data is stored.
*
- * On Win32, this is the "My Documents" folder in the personal profile.
+ * On Win32, this is the "My Documents" folder in the personal profile,
+ * except that, if we're running from a U3 device, this is the
+ * "$U3_DEVICE_DOCUMENT_PATH\My Captures" folder.
* On UNIX this is simply the current directory.
- * On a U3 device this is "$U3_DEVICE_DOCUMENT_PATH\My Captures" folder.
*/
/* XXX - should this and the get_home_dir() be merged? */
-extern char *
+extern const char *
get_persdatafile_dir(void)
{
#ifdef _WIN32
- char *u3devicedocumentpath;
- TCHAR tszPath[MAX_PATH];
+ char *u3devicedocumentpath;
+ TCHAR tszPath[MAX_PATH];
char *szPath;
BOOL bRet;
-
/* Return the cached value, if available */
if (persdatafile_dir != NULL)
return persdatafile_dir;
u3devicedocumentpath = getenv_utf8("U3_DEVICE_DOCUMENT_PATH");
if (u3devicedocumentpath != NULL) {
+ /* the "My Captures" sub-directory is created (if it doesn't
+ exist) by u3util.exe when the U3 Wireshark is first run */
- /* the "My Captures" sub-directory is created (if it doesn't exist)
- by u3util.exe when the U3 Wireshark is first run */
-
- szPath = g_strdup_printf("%s%s", u3devicedocumentpath, U3_MY_CAPTURES);
+ szPath = g_strdup_printf("%s%s", u3devicedocumentpath, U3_MY_CAPTURES);
- persdatafile_dir = szPath;
- return szPath;
-
- } else {
- /* Hint: SHGetFolderPath is not available on MSVC 6 - without Platform SDK */
- bRet = SHGetSpecialFolderPath(NULL, tszPath, CSIDL_PERSONAL, FALSE);
- if(bRet == TRUE) {
- szPath = utf_16to8(tszPath);
persdatafile_dir = szPath;
return szPath;
} else {
- return "";
+ /*
+ * Hint: SHGetFolderPath is not available on MSVC 6 - without
+ * Platform SDK
+ */
+ bRet = SHGetSpecialFolderPath(NULL, tszPath, CSIDL_PERSONAL,
+ FALSE);
+ if(bRet == TRUE) {
+ szPath = utf_16to8(tszPath);
+ persdatafile_dir = szPath;
+ return szPath;
+ } else {
+ return "";
+ }
}
- }
#else
- return "";
+ return "";
#endif
}
* subdirectory of the user's home directory, and check whether that
* exists; if it does, we return that, so that configuration files
* from earlier versions can be read.
+ *
+ * The returned file name was g_malloc()'d so it must be g_free()d when the
+ * caller is done with it.
*/
char *
get_persconffile_path(const char *filename, gboolean from_profile, gboolean for_writing
struct stat s_buf;
char *old_path;
#endif
+ if (do_store_persconffiles && from_profile && !g_hash_table_lookup (profile_files, filename)) {
+ /* Store filenames so we know which filenames belongs to a configuration profile */
+ g_hash_table_insert (profile_files, g_strdup(filename), g_strdup(filename));
+ }
if (from_profile) {
path = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s",
}
#ifdef _WIN32
if (!for_writing) {
- if (eth_stat(path, &s_buf) != 0 && errno == ENOENT) {
+ if (ws_stat(path, &s_buf) != 0 && errno == ENOENT) {
/*
* OK, it's not in the personal configuration file
* directory; is it in the ".wireshark" subdirectory
old_path = g_strdup_printf(
"%s" G_DIR_SEPARATOR_S ".wireshark" G_DIR_SEPARATOR_S "%s",
get_home_dir(), filename);
- if (eth_stat(old_path, &s_buf) == 0) {
+ if (ws_stat(old_path, &s_buf) == 0) {
/*
* OK, it exists; return it instead.
*/
* (move this e.g. to main.c and have set_persconffile_dir() instead in this file?)
*/
int
-filesystem_opt(int opt _U_, const char *optarg)
+filesystem_opt(int opt _U_, const char *optstr)
{
- gchar *p, *colonp;
+ gchar *p, *colonp;
- colonp = strchr(optarg, ':');
- if (colonp == NULL) {
- return 1;
- }
+ colonp = strchr(optstr, ':');
+ if (colonp == NULL) {
+ return 1;
+ }
- p = colonp;
- *p++ = '\0';
-
- /*
- * Skip over any white space (there probably won't be any, but
- * as we allow it in the preferences file, we might as well
- * allow it here).
- */
- while (isspace((guchar)*p))
- p++;
- if (*p == '\0') {
- /*
- * Put the colon back, so if our caller uses, in an
- * error message, the string they passed us, the message
- * looks correct.
- */
- *colonp = ':';
- return 1;
- }
+ p = colonp;
+ *p++ = '\0';
- /* directory should be existing */
- /* XXX - is this a requirement? */
- if(test_for_directory(p) != EISDIR) {
- /*
- * Put the colon back, so if our caller uses, in an
- * error message, the string they passed us, the message
- * looks correct.
- */
- *colonp = ':';
- return 1;
- }
+ /*
+ * Skip over any white space (there probably won't be any, but
+ * as we allow it in the preferences file, we might as well
+ * allow it here).
+ */
+ while (isspace((guchar)*p))
+ p++;
+ if (*p == '\0') {
+ /*
+ * Put the colon back, so if our caller uses, in an
+ * error message, the string they passed us, the message
+ * looks correct.
+ */
+ *colonp = ':';
+ return 1;
+ }
- if (strcmp(optarg,"persconf") == 0) {
- persconffile_dir = p;
- } else if (strcmp(optarg,"persdata") == 0) {
- persdatafile_dir = p;
- /* XXX - might need to add the temp file path */
- } else {
- return 1;
- }
- *colonp = ':'; /* put the colon back */
- return 0;
+ /* directory should be existing */
+ /* XXX - is this a requirement? */
+ if(test_for_directory(p) != EISDIR) {
+ /*
+ * Put the colon back, so if our caller uses, in an
+ * error message, the string they passed us, the message
+ * looks correct.
+ */
+ *colonp = ':';
+ return 1;
+ }
+
+ if (strcmp(optstr,"persconf") == 0) {
+ persconffile_dir = p;
+ } else if (strcmp(optstr,"persdata") == 0) {
+ persdatafile_dir = p;
+ /* XXX - might need to add the temp file path */
+ } else {
+ return 1;
+ }
+ *colonp = ':'; /* put the colon back */
+ return 0;
}
/*
* Construct the path name of a global configuration file, given the
* file name.
+ *
+ * The returned file name was g_malloc()'d so it must be g_free()d when the
+ * caller is done with it.
*/
char *
get_datafile_path(const char *filename)
{
return g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", get_datafile_dir(),
- filename);
+ filename);
+}
+
+/* Get the personal plugin dir */
+/* Return value is malloced so the caller should g_free() it. */
+char *
+get_plugins_pers_dir(void)
+{
+ return get_persconffile_path(PLUGINS_DIR_NAME, FALSE, FALSE);
}
/* Delete a file */
gboolean
deletefile(const char *path)
{
- return eth_unlink(path) == 0;
+ return ws_unlink(path) == 0;
}
/*
*/
char *get_tempfile_path(const char *filename)
{
-
return g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", g_get_tmp_dir(), filename);
}
break;
#endif
+ case WTAP_ERR_SHORT_WRITE:
+ errmsg = "A full write couldn't be done to the file \"%s\".";
+ break;
+
default:
g_snprintf(errmsg_errno, sizeof(errmsg_errno),
"An error occurred while writing to the file \"%%s\": %s.",
gboolean
file_exists(const char *fname)
{
- struct stat file_stat;
-
+ struct stat file_stat;
#ifdef _WIN32
- /*
- * This is a bit tricky on win32. The st_ino field is documented as:
- * "The inode, and therefore st_ino, has no meaning in the FAT, ..."
- * but it *is* set to zero if stat() returns without an error,
- * so this is working, but maybe not quite the way expected. ULFL
- */
- file_stat.st_ino = 1; /* this will make things work if an error occured */
- eth_stat(fname, &file_stat);
- if (file_stat.st_ino == 0) {
- return TRUE;
- } else {
- return FALSE;
- }
+ /*
+ * This is a bit tricky on win32. The st_ino field is documented as:
+ * "The inode, and therefore st_ino, has no meaning in the FAT, ..."
+ * but it *is* set to zero if stat() returns without an error,
+ * so this is working, but maybe not quite the way expected. ULFL
+ */
+ file_stat.st_ino = 1; /* this will make things work if an error occured */
+ ws_stat(fname, &file_stat);
+ if (file_stat.st_ino == 0) {
+ return TRUE;
+ } else {
+ return FALSE;
+ }
#else
- if (eth_stat(fname, &file_stat) != 0 && errno == ENOENT) {
- return FALSE;
- } else {
- return TRUE;
- }
+ if (ws_stat(fname, &file_stat) != 0 && errno == ENOENT) {
+ return FALSE;
+ } else {
+ return TRUE;
+ }
#endif
-
}
/*
gboolean
files_identical(const char *fname1, const char *fname2)
{
- /* Two different implementations, because:
- *
- * - _fullpath is not available on UN*X, so we can't get full
- * paths and compare them (which wouldn't work with hard links
- * in any case);
- *
- * - st_ino isn't filled in with a meaningful value on Windows.
- */
+ /* Two different implementations, because:
+ *
+ * - _fullpath is not available on UN*X, so we can't get full
+ * paths and compare them (which wouldn't work with hard links
+ * in any case);
+ *
+ * - st_ino isn't filled in with a meaningful value on Windows.
+ */
#ifdef _WIN32
- char full1[MAX_PATH], full2[MAX_PATH];
-
- /*
- * Get the absolute full paths of the file and compare them.
- * That won't work if you have hard links, but those aren't
- * much used on Windows, even though NTFS supports them.
- *
- * XXX - will _fullpath work with UNC?
- */
- if( _fullpath( full1, fname1, MAX_PATH ) == NULL ) {
- return FALSE;
- }
+ char full1[MAX_PATH], full2[MAX_PATH];
- if( _fullpath( full2, fname2, MAX_PATH ) == NULL ) {
- return FALSE;
- }
+ /*
+ * Get the absolute full paths of the file and compare them.
+ * That won't work if you have hard links, but those aren't
+ * much used on Windows, even though NTFS supports them.
+ *
+ * XXX - will _fullpath work with UNC?
+ */
+ if( _fullpath( full1, fname1, MAX_PATH ) == NULL ) {
+ return FALSE;
+ }
- if(strcmp(full1, full2) == 0) {
- return TRUE;
- } else {
- return FALSE;
- }
+ if( _fullpath( full2, fname2, MAX_PATH ) == NULL ) {
+ return FALSE;
+ }
+
+ if(strcmp(full1, full2) == 0) {
+ return TRUE;
+ } else {
+ return FALSE;
+ }
#else
- struct stat filestat1, filestat2;
-
- /*
- * Compare st_dev and st_ino.
- */
- if (eth_stat(fname1, &filestat1) == -1)
- return FALSE; /* can't get info about the first file */
- if (eth_stat(fname2, &filestat2) == -1)
- return FALSE; /* can't get info about the second file */
- return (filestat1.st_dev == filestat2.st_dev &&
- filestat1.st_ino == filestat2.st_ino);
+ struct stat filestat1, filestat2;
+
+ /*
+ * Compare st_dev and st_ino.
+ */
+ if (ws_stat(fname1, &filestat1) == -1)
+ return FALSE; /* can't get info about the first file */
+ if (ws_stat(fname2, &filestat2) == -1)
+ return FALSE; /* can't get info about the second file */
+ return (filestat1.st_dev == filestat2.st_dev &&
+ filestat1.st_ino == filestat2.st_ino);
#endif
}
+/*
+ * Copy a file in binary mode, for those operating systems that care about
+ * such things. This should be OK for all files, even text files, as
+ * we'll copy the raw bytes, and we don't look at the bytes as we copy
+ * them.
+ *
+ * Returns TRUE on success, FALSE on failure. If a failure, it also
+ * displays a simple dialog window with the error message.
+ */
+gboolean
+copy_file_binary_mode(const char *from_filename, const char *to_filename)
+{
+ int from_fd, to_fd, nread, nwritten, err;
+ guint8 pd[65536];
+
+ /* Copy the raw bytes of the file. */
+ from_fd = ws_open(from_filename, O_RDONLY | O_BINARY, 0000 /* no creation so don't matter */);
+ if (from_fd < 0) {
+ report_open_failure(from_filename, errno, FALSE);
+ goto done;
+ }
+
+ /* Use open() instead of creat() so that we can pass the O_BINARY
+ flag, which is relevant on Win32; it appears that "creat()"
+ may open the file in text mode, not binary mode, but we want
+ to copy the raw bytes of the file, so we need the output file
+ to be open in binary mode. */
+ to_fd = ws_open(to_filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
+ if (to_fd < 0) {
+ report_open_failure(to_filename, errno, TRUE);
+ ws_close(from_fd);
+ goto done;
+ }
+
+ while ((nread = ws_read(from_fd, pd, sizeof pd)) > 0) {
+ nwritten = ws_write(to_fd, pd, nread);
+ if (nwritten < nread) {
+ if (nwritten < 0)
+ err = errno;
+ else
+ err = WTAP_ERR_SHORT_WRITE;
+ report_write_failure(to_filename, err);
+ ws_close(from_fd);
+ ws_close(to_fd);
+ goto done;
+ }
+ }
+ if (nread < 0) {
+ err = errno;
+ report_read_failure(from_filename, err);
+ ws_close(from_fd);
+ ws_close(to_fd);
+ goto done;
+ }
+ ws_close(from_fd);
+ if (ws_close(to_fd) < 0) {
+ report_write_failure(to_filename, errno);
+ goto done;
+ }
+
+ return TRUE;
+
+done:
+ return FALSE;
+}
+
+/*
+ * Editor modelines
+ *
+ * Local Variables:
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: t
+ * End:
+ *
+ * ex: set shiftwidth=4 tabstop=4 noexpandtab
+ * :indentSize=4:tabSize=4:noTabs=false:
+ */