2 * Filesystem utility routines
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
45 #ifdef HAVE_SYS_STAT_H
53 #include "epan/unicode-utils.h"
58 #include "filesystem.h"
59 #include "privileges.h"
60 #include <wiretap/file_util.h>
62 #define PROFILES_DIR "profiles"
63 #define U3_MY_CAPTURES "\\My Captures"
65 char *persconffile_dir = NULL;
66 char *persdatafile_dir = NULL;
67 char *persconfprofile = NULL;
70 * Given a pathname, return a pointer to the last pathname separator
71 * character in the pathname, or NULL if the pathname contains no
75 find_last_pathname_separator(const char *path)
83 * We have to scan for '\' or '/'.
84 * Get to the end of the string.
86 separator = strchr(path, '\0'); /* points to ending '\0' */
87 while (separator > path) {
89 if (c == '\\' || c == '/')
90 return separator; /* found it */
94 * OK, we didn't find any, so no directories - but there might
95 * be a drive letter....
97 return strchr(path, ':');
99 separator = strrchr(path, '/');
105 * Given a pathname, return the last component.
108 get_basename(const char *path)
110 const char *filename;
112 g_assert(path != NULL);
113 filename = find_last_pathname_separator(path);
114 if (filename == NULL) {
116 * There're no directories, drive letters, etc. in the
117 * name; the pathname *is* the file name.
122 * Skip past the pathname or drive letter separator.
130 * Given a pathname, return a string containing everything but the
131 * last component. NOTE: this overwrites the pathname handed into
135 get_dirname(char *path)
139 g_assert(path != NULL);
140 separator = find_last_pathname_separator(path);
141 if (separator == NULL) {
143 * There're no directories, drive letters, etc. in the
144 * name; there is no directory path to return.
150 * Get rid of the last pathname separator and the final file
156 * "path" now contains the pathname of the directory containing
157 * the file/directory to which it referred.
163 * Given a pathname, return:
165 * the errno, if an attempt to "stat()" the file fails;
167 * EISDIR, if the attempt succeeded and the file turned out
170 * 0, if the attempt succeeded and the file turned out not
175 * Visual C++ on Win32 systems doesn't define these. (Old UNIX systems don't
176 * define them either.)
178 * Visual C++ on Win32 systems doesn't define S_IFIFO, it defines _S_IFIFO.
181 #define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
184 #define S_IFIFO _S_IFIFO
187 #define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO)
190 #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
194 test_for_directory(const char *path)
198 if (eth_stat(path, &statb) < 0)
201 if (S_ISDIR(statb.st_mode))
208 test_for_fifo(const char *path)
212 if (eth_stat(path, &statb) < 0)
215 if (S_ISFIFO(statb.st_mode))
222 * Directory from which the executable came.
224 static char *progfile_dir;
227 * TRUE if we're running from the build directory.
229 static gboolean running_in_build_directory_flag = FALSE;
232 * Get the pathname of the directory from which the executable came,
233 * and save it for future use. Returns NULL on success, and a
234 * g_mallocated string containing an error on failure.
237 init_progfile_dir(const char *arg0
246 TCHAR prog_pathname_w[_MAX_PATH+2];
247 size_t progfile_dir_len;
255 * Attempt to get the full pathname of the currently running
258 if (GetModuleFileName(NULL, prog_pathname_w, sizeof prog_pathname_w) != 0) {
260 * XXX - Should we use g_utf16_to_utf8(), as in
263 prog_pathname = utf_16to8(prog_pathname_w);
265 * We got it; strip off the last component, which would be
266 * the file name of the executable, giving us the pathname
267 * of the directory where the executable resies
269 * First, find the last "\" in the directory, as that
270 * marks the end of the directory pathname.
272 * XXX - Can the pathname be something such as
273 * "C:wireshark.exe"? Or is it always a full pathname
274 * beginning with "\" after the drive letter?
276 dir_end = strrchr(prog_pathname, '\\');
277 if (dir_end != NULL) {
279 * Found it - now figure out how long the program
280 * directory pathname will be.
282 progfile_dir_len = (dir_end - prog_pathname);
285 * Allocate a buffer for the program directory
286 * pathname, and construct it.
288 path = g_malloc(progfile_dir_len + 1);
289 strncpy(path, prog_pathname, progfile_dir_len);
290 path[progfile_dir_len] = '\0';
293 return NULL; /* we succeeded */
296 * OK, no \ - what do we do now?
298 return g_strdup_printf("No \\ in executable pathname \"%s\"",
303 * Oh, well. Return an indication of the error.
305 error = GetLastError();
306 if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
307 NULL, error, 0, (LPTSTR) &msg_w, 0, NULL) == 0) {
309 * Gak. We can't format the message.
311 return g_strdup_printf("GetModuleFileName failed: %u (FormatMessage failed: %u)",
312 error, GetLastError());
314 msg = utf_16to8(msg_w);
317 * "FormatMessage()" "helpfully" sticks CR/LF at the
318 * end of the message. Get rid of it.
320 msglen = strlen(msg);
322 msg[msglen - 1] = '\0';
323 msg[msglen - 2] = '\0';
325 return g_strdup_printf("GetModuleFileName failed: %s (%u)",
333 char *path_start, *path_end;
334 size_t path_component_len;
338 * Check whether WIRESHARK_RUN_FROM_BUILD_DIRECTORY is set in the
339 * environment; if so, set running_in_build_directory_flag if we
340 * weren't started with special privileges. (If we were started
341 * with special privileges, it's not safe to allow the user to point
342 * us to some other directory; running_in_build_directory_flag, when
343 * set, causes us to look for plugins and the like in the build
346 if (getenv("WIRESHARK_RUN_FROM_BUILD_DIRECTORY") != NULL
347 && !started_with_special_privs())
348 running_in_build_directory_flag = TRUE;
351 * Try to figure out the directory in which the currently running
352 * program resides, given the argv[0] it was started with. That
353 * might be the absolute path of the program, or a path relative
354 * to the current directory of the process that started it, or
355 * just a name for the program if it was started from the command
356 * line and was searched for in $PATH. It's not guaranteed to be
357 * any of those, however, so there are no guarantees....
359 if (arg0[0] == '/') {
361 * It's an absolute path.
363 prog_pathname = g_strdup(arg0);
364 } else if (strchr(arg0, '/') != NULL) {
366 * It's a relative path, with a directory in it.
367 * Get the current directory, and combine it
368 * with that directory.
370 path_max = pathconf(".", _PC_PATH_MAX);
371 if (path_max == -1) {
373 * We have no idea how big a buffer to
374 * allocate for the current directory.
376 return g_strdup_printf("pathconf failed: %s\n",
379 curdir = g_malloc(path_max);
380 if (getcwd(curdir, path_max) == NULL) {
382 * It failed - give up, and just stick
386 return g_strdup_printf("getcwd failed: %s\n",
389 path = g_strdup_printf("%s/%s", curdir, arg0);
391 prog_pathname = path;
394 * It's just a file name.
395 * Search the path for a file with that name
398 prog_pathname = NULL; /* haven't found it yet */
399 pathstr = getenv("PATH");
400 path_start = pathstr;
401 if (path_start != NULL) {
402 while (*path_start != '\0') {
403 path_end = strchr(path_start, ':');
404 if (path_end == NULL)
405 path_end = path_start + strlen(path_start);
406 path_component_len = path_end - path_start;
407 path = g_malloc(path_component_len + 1
409 memcpy(path, path_start, path_component_len);
410 path[path_component_len] = '\0';
411 strncat(path, "/", 2);
412 strncat(path, arg0, strlen(arg0) + 1);
413 if (access(path, X_OK) == 0) {
417 prog_pathname = path;
422 * That's not it. If there are more
423 * path components to test, try them.
425 if (*path_end == '\0') {
427 * There's nothing more to try.
431 if (*path_end == ':')
433 path_start = path_end;
436 if (prog_pathname == NULL) {
438 * Program not found in path.
440 return g_strdup_printf("\"%s\" not found in \"%s\"",
446 * XXX - should we pick a default?
448 return g_strdup("PATH isn't set");
453 * OK, we have what we think is the pathname
456 * First, find the last "/" in the directory,
457 * as that marks the end of the directory pathname.
459 dir_end = strrchr(prog_pathname, '/');
460 if (dir_end != NULL) {
462 * Found it. Strip off the last component,
463 * as that's the path of the program.
468 * Is there a "/.libs" at the end?
470 dir_end = strrchr(prog_pathname, '/');
471 if (dir_end != NULL) {
472 if (strcmp(dir_end, "/.libs") == 0) {
475 * Strip that off; it's an
476 * artifact of libtool.
481 * This presumably means we're run from
482 * the libtool wrapper, which probably
483 * means we're being run from the build
484 * directory. If we weren't started
485 * with special privileges, set
486 * running_in_build_directory_flag.
488 * XXX - should we check whether what
489 * follows ".libs/" begins with "lt-"?
491 if (!started_with_special_privs())
492 running_in_build_directory_flag = TRUE;
497 * OK, we have the path we want.
499 progfile_dir = prog_pathname;
503 * This "shouldn't happen"; we apparently
504 * have no "/" in the pathname.
505 * Just free up prog_pathname.
507 retstr = g_strdup_printf("No / found in \"%s\"", prog_pathname);
508 g_free(prog_pathname);
515 * Get the directory in which the program resides.
518 get_progfile_dir(void)
524 * Get the directory in which the global configuration and data files are
527 * On Windows, we use the directory in which the executable for this
530 * On UN*X, we use the DATAFILE_DIR value supplied by the configure
531 * script, unless we think we're being run from the build directory,
532 * in which case we use the directory in which the executable for this
535 * XXX - if we ever make libwireshark a real library, used by multiple
536 * applications (more than just TShark and versions of Wireshark with
537 * various UIs), should the configuration files belong to the library
538 * (and be shared by all those applications) or to the applications?
540 * If they belong to the library, that could be done on UNIX by the
541 * configure script, but it's trickier on Windows, as you can't just
542 * use the pathname of the executable.
544 * If they belong to the application, that could be done on Windows
545 * by using the pathname of the executable, but we'd have to have it
546 * passed in as an argument, in some call, on UNIX.
548 * Note that some of those configuration files might be used by code in
549 * libwireshark, some of them might be used by dissectors (would they
550 * belong to libwireshark, the application, or a separate library?),
551 * and some of them might be used by other code (the Wireshark preferences
552 * file includes resolver preferences that control the behavior of code
553 * in libwireshark, dissector preferences, and UI preferences, for
557 get_datafile_dir(void)
560 char *u3deviceexecpath;
562 static char *datafile_dir = NULL;
564 if (datafile_dir != NULL)
569 * See if we are running in a U3 environment.
571 u3deviceexecpath = getenv_utf8("U3_DEVICE_EXEC_PATH");
573 if (u3deviceexecpath != NULL) {
575 * We are; use the U3 device executable path.
577 datafile_dir = u3deviceexecpath;
580 * Do we have the pathname of the program? If so, assume we're
581 * running an installed version of the program. If we fail,
582 * we don't change "datafile_dir", and thus end up using the
585 * XXX - does NSIS put the installation directory into
586 * "\HKEY_LOCAL_MACHINE\SOFTWARE\Wireshark\InstallDir"?
587 * If so, perhaps we should read that from the registry,
590 if (progfile_dir != NULL) {
592 * Yes, we do; use that.
594 datafile_dir = progfile_dir;
598 * Fall back on the default installation directory.
600 datafile_dir = "C:\\Program Files\\Wireshark\\";
604 if (running_in_build_directory_flag && progfile_dir != NULL) {
606 * We're (probably) being run from the build directory and
607 * weren't started with special privileges, and we were
608 * able to determine the directory in which the program
609 * was found, so use that.
611 datafile_dir = progfile_dir;
614 * Return the directory specified when the build
617 datafile_dir = DATAFILE_DIR;
626 * Find the directory where the plugins are stored.
628 * On Windows, we use the "plugin" subdirectory of the datafile directory.
630 * On UN*X, we use the PLUGIN_DIR value supplied by the configure
631 * script, unless we think we're being run from the build directory,
632 * in which case we use the "plugin" subdirectory of the datafile directory.
634 * In both cases, we then use the subdirectory of that directory whose
635 * name is the version number.
637 * XXX - if we think we're being run from the build directory, perhaps we
638 * should have the plugin code not look in the version subdirectory
639 * of the plugin directory, but look in all of the subdirectories
640 * of the plugin directory, so it can just fetch the plugins built
641 * as part of the build process.
643 static const char *plugin_dir = NULL;
646 init_plugin_dir(void)
650 * On Windows, the data file directory is the installation
651 * directory; the plugins are stored under it.
653 * Assume we're running the installed version of Wireshark;
654 * on Windows, the data file directory is the directory
655 * in which the Wireshark binary resides.
657 plugin_dir = g_strdup_printf("%s\\plugins\\%s", get_datafile_dir(),
661 * Make sure that pathname refers to a directory.
663 if (test_for_directory(plugin_dir) != EISDIR) {
665 * Either it doesn't refer to a directory or it
666 * refers to something that doesn't exist.
668 * Assume that means we're running a version of
669 * Wireshark we've built in a build directory,
670 * in which case {datafile dir}\plugins is the
671 * top-level plugins source directory, and use
672 * that directory and set the "we're running in
673 * a build directory" flag, so the plugin
674 * scanner will check all subdirectories of that
675 * directory for plugins.
677 g_free( (gpointer) plugin_dir);
678 plugin_dir = g_strdup_printf("%s\\plugins", get_datafile_dir());
679 running_in_build_directory_flag = TRUE;
682 if (running_in_build_directory_flag) {
684 * We're (probably) being run from the build directory and
685 * weren't started with special privileges, so we'll use
686 * the "plugins" subdirectory of the datafile directory
687 * (the datafile directory is the build directory).
689 plugin_dir = g_strdup_printf("%s/plugins", get_datafile_dir());
691 plugin_dir = PLUGIN_DIR;
694 #endif /* HAVE_PLUGINS */
697 * Get the directory in which the plugins are stored.
703 if (!plugin_dir) init_plugin_dir();
711 * Get the flag indicating whether we're running from a build
715 running_in_build_directory(void)
717 return running_in_build_directory_flag;
721 * Get the directory in which files that, at least on UNIX, are
722 * system files (such as "/etc/ethers") are stored; on Windows,
723 * there's no "/etc" directory, so we get them from the global
724 * configuration and data file directory.
727 get_systemfile_dir(void)
730 return get_datafile_dir();
737 * Name of directory, under the user's home directory, in which
738 * personal configuration files are stored.
741 #define PF_DIR "Wireshark"
744 * XXX - should this be ".libepan"? For backwards-compatibility, I'll keep
745 * it ".wireshark" for now.
747 #define PF_DIR ".wireshark"
751 /* utf8 version of getenv, needed to get win32 filename paths */
752 char *getenv_utf8(const char *varname)
758 envvar = getenv(varname);
760 /* since GLib 2.6 we need an utf8 version of the filename */
761 #if (GLIB_MAJOR_VERSION > 2 || (GLIB_MAJOR_VERSION == 2 && GLIB_MINOR_VERSION >= 6))
762 /* using the wide char version of getenv should work under all circumstances */
764 /* convert given varname to utf16, needed by _wgetenv */
765 varnamew = g_utf8_to_utf16(varname, -1, NULL, NULL, NULL);
766 if (varnamew == NULL) {
770 /* use wide char version of getenv */
771 envvarw = _wgetenv(varnamew);
773 if (envvarw == NULL) {
777 /* convert value to utf8 */
778 envvar = g_utf16_to_utf8(envvarw, -1, NULL, NULL, NULL);
787 set_profile_name(const gchar *profilename)
789 if (persconfprofile) {
790 g_free (persconfprofile);
793 if (profilename && strlen(profilename) > 0 &&
794 strcmp(profilename, DEFAULT_PROFILE) != 0) {
795 persconfprofile = g_strdup (profilename);
797 /* Default Profile */
798 persconfprofile = NULL;
803 get_profile_name(void)
805 if (persconfprofile) {
806 return persconfprofile;
808 return DEFAULT_PROFILE;
813 * Get the directory in which personal configuration files reside;
814 * in UNIX-compatible systems, it's ".wireshark", under the user's home
815 * directory, and on Windows systems, it's "Wireshark", under %APPDATA%
816 * or, if %APPDATA% isn't set, it's "%USERPROFILE%\Application Data"
817 * (which is what %APPDATA% normally is on Windows 2000).
820 get_persconffile_dir_no_profile(void)
824 char *userprofiledir;
831 /* Return the cached value, if available */
832 if (persconffile_dir != NULL)
833 return persconffile_dir;
837 * See if we are running in a U3 environment.
839 u3appdatapath = getenv_utf8("U3_APP_DATA_PATH");
840 if (u3appdatapath != NULL) {
842 * We are; use the U3 application data path.
844 persconffile_dir = u3appdatapath;
847 * Use %APPDATA% or %USERPROFILE%, so that configuration
848 * files are stored in the user profile, rather than in
849 * the home directory. The Windows convention is to store
850 * configuration information in the user profile, and doing
851 * so means you can use Wireshark even if the home directory
852 * is an inaccessible network drive.
854 appdatadir = getenv_utf8("APPDATA");
855 if (appdatadir != NULL) {
857 * Concatenate %APPDATA% with "\Wireshark".
859 persconffile_dir = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s",
863 * OK, %APPDATA% wasn't set, so use
864 * %USERPROFILE%\Application Data.
866 userprofiledir = getenv_utf8("USERPROFILE");
867 if (userprofiledir != NULL) {
868 persconffile_dir = g_strdup_printf(
869 "%s" G_DIR_SEPARATOR_S "Application Data" G_DIR_SEPARATOR_S "%s",
870 userprofiledir, PF_DIR);
873 * Give up and use "C:".
875 persconffile_dir = g_strdup_printf("C:" G_DIR_SEPARATOR_S "%s", PF_DIR);
881 * If $HOME is set, use that.
883 homedir = getenv("HOME");
884 if (homedir == NULL) {
886 * Get their home directory from the password file.
887 * If we can't even find a password file entry for them,
890 pwd = getpwuid(getuid());
893 * This is cached, so we don't need to worry
894 * about allocating multiple ones of them.
896 homedir = g_strdup(pwd->pw_dir);
900 persconffile_dir = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", homedir, PF_DIR);
903 return persconffile_dir;
907 get_profiles_dir(void)
909 static char *profiles_dir = NULL;
912 g_free (profiles_dir);
915 profiles_dir = g_strdup_printf ("%s%s%s", get_persconffile_dir_no_profile (),
916 G_DIR_SEPARATOR_S, PROFILES_DIR);
922 get_persconffile_dir(const gchar *profilename)
924 static char *persconffile_profile_dir = NULL;
926 if (persconffile_profile_dir) {
927 g_free (persconffile_profile_dir);
930 if (profilename && strlen(profilename) > 0 &&
931 strcmp(profilename, DEFAULT_PROFILE) != 0) {
932 persconffile_profile_dir = g_strdup_printf ("%s%s%s", get_profiles_dir (),
933 G_DIR_SEPARATOR_S, profilename);
935 persconffile_profile_dir = g_strdup_printf (get_persconffile_dir_no_profile ());
938 return persconffile_profile_dir;
942 profile_exists(const gchar *profilename)
944 if (test_for_directory (get_persconffile_dir (profilename)) == EISDIR) {
952 delete_directory (const char *directory, char **pf_dir_path_return)
959 if ((dir = eth_dir_open(directory, 0, NULL)) != NULL) {
960 while ((file = eth_dir_read_name(dir)) != NULL) {
961 filename = g_strdup_printf ("%s%s%s", directory, G_DIR_SEPARATOR_S,
962 eth_dir_get_name(file));
963 if (test_for_directory(filename) != EISDIR) {
964 ret = eth_remove(filename);
967 /* The user has manually created a directory in the profile directory */
968 /* I do not want to delete the directory recursively yet */
969 ret = delete_directory (filename, pf_dir_path_return);
973 *pf_dir_path_return = filename;
981 if (ret == 0 && (ret = eth_remove(directory)) != 0) {
982 *pf_dir_path_return = g_strdup (directory);
989 delete_persconffile_profile(const char *profilename, char **pf_dir_path_return)
991 const char *profile_dir = get_persconffile_dir(profilename);
994 if (test_for_directory (profile_dir) == EISDIR) {
995 ret = delete_directory (profile_dir, pf_dir_path_return);
1002 rename_persconffile_profile(const char *fromname, const char *toname,
1003 char **pf_from_dir_path_return, char **pf_to_dir_path_return)
1005 char *from_dir = g_strdup (get_persconffile_dir(fromname));
1006 char *to_dir = g_strdup (get_persconffile_dir(toname));
1009 ret = eth_rename (from_dir, to_dir);
1011 *pf_from_dir_path_return = g_strdup (from_dir);
1012 *pf_to_dir_path_return = g_strdup (to_dir);
1022 * Create the directory that holds personal configuration files, if
1023 * necessary. If we attempted to create it, and failed, return -1 and
1024 * set "*pf_dir_path_return" to the pathname of the directory we failed
1025 * to create (it's g_mallocated, so our caller should free it); otherwise,
1029 create_persconffile_profile(const char *profilename, char **pf_dir_path_return)
1031 const char *pf_dir_path;
1033 char *pf_dir_path_copy, *pf_dir_parent_path;
1034 size_t pf_dir_parent_path_len;
1041 * Check if profiles directory exists.
1042 * If not then create it.
1044 pf_dir_path = get_profiles_dir ();
1045 if (eth_stat(pf_dir_path, &s_buf) != 0 && errno == ENOENT) {
1046 ret = eth_mkdir(pf_dir_path, 0755);
1048 *pf_dir_path_return = g_strdup(pf_dir_path);
1054 pf_dir_path = get_persconffile_dir(profilename);
1055 if (eth_stat(pf_dir_path, &s_buf) != 0 && errno == ENOENT) {
1058 * Does the parent directory of that directory
1059 * exist? %APPDATA% may not exist even though
1060 * %USERPROFILE% does.
1062 * We check for the existence of the directory
1063 * by first checking whether the parent directory
1064 * is just a drive letter and, if it's not, by
1065 * doing a "stat()" on it. If it's a drive letter,
1066 * or if the "stat()" succeeds, we assume it exists.
1068 pf_dir_path_copy = g_strdup(pf_dir_path);
1069 pf_dir_parent_path = get_dirname(pf_dir_path_copy);
1070 pf_dir_parent_path_len = strlen(pf_dir_parent_path);
1071 if (pf_dir_parent_path_len > 0
1072 && pf_dir_parent_path[pf_dir_parent_path_len - 1] != ':'
1073 && eth_stat(pf_dir_parent_path, &s_buf) != 0) {
1075 * No, it doesn't exist - make it first.
1077 ret = eth_mkdir(pf_dir_parent_path, 0755);
1079 *pf_dir_path_return = pf_dir_parent_path;
1083 g_free(pf_dir_path_copy);
1084 ret = eth_mkdir(pf_dir_path, 0755);
1086 ret = eth_mkdir(pf_dir_path, 0755);
1090 * Something with that pathname exists; if it's not
1091 * a directory, we'll get an error if we try to put
1092 * something in it, so we don't fail here, we wait
1093 * for that attempt fo fail.
1098 *pf_dir_path_return = g_strdup(pf_dir_path);
1103 create_persconffile_dir(char **pf_dir_path_return)
1105 return create_persconffile_profile(persconfprofile, pf_dir_path_return);
1109 * Get the (default) directory in which personal data is stored.
1111 * On Win32, this is the "My Documents" folder in the personal profile.
1112 * On UNIX this is simply the current directory.
1113 * On a U3 device this is "$U3_DEVICE_DOCUMENT_PATH\My Captures" folder.
1115 /* XXX - should this and the get_home_dir() be merged? */
1117 get_persdatafile_dir(void)
1120 char *u3devicedocumentpath;
1121 TCHAR tszPath[MAX_PATH];
1126 /* Return the cached value, if available */
1127 if (persdatafile_dir != NULL)
1128 return persdatafile_dir;
1131 * See if we are running in a U3 environment.
1133 u3devicedocumentpath = getenv_utf8("U3_DEVICE_DOCUMENT_PATH");
1135 if (u3devicedocumentpath != NULL) {
1137 /* the "My Captures" sub-directory is created (if it doesn't exist)
1138 by u3util.exe when the U3 Wireshark is first run */
1140 szPath = g_strdup_printf("%s%s", u3devicedocumentpath, U3_MY_CAPTURES);
1142 persdatafile_dir = szPath;
1146 /* Hint: SHGetFolderPath is not available on MSVC 6 - without Platform SDK */
1147 bRet = SHGetSpecialFolderPath(NULL, tszPath, CSIDL_PERSONAL, FALSE);
1149 szPath = utf_16to8(tszPath);
1150 persdatafile_dir = szPath;
1163 * Returns the user's home directory on Win32.
1168 static const char *home = NULL;
1169 char *homedrive, *homepath;
1173 /* Return the cached value, if available */
1178 * XXX - should we use USERPROFILE anywhere in this process?
1179 * Is there a chance that it might be set but one or more of
1180 * HOMEDRIVE or HOMEPATH isn't set?
1182 homedrive = getenv_utf8("HOMEDRIVE");
1183 if (homedrive != NULL) {
1184 homepath = getenv_utf8("HOMEPATH");
1185 if (homepath != NULL) {
1187 * This is cached, so we don't need to worry about
1188 * allocating multiple ones of them.
1190 homestring = g_strdup_printf("%s%s", homedrive, homepath);
1193 * Trim off any trailing slash or backslash.
1195 lastsep = find_last_pathname_separator(homestring);
1196 if (lastsep != NULL && *(lastsep + 1) == '\0') {
1198 * Last separator is the last character
1199 * in the string. Nuke it.
1208 * Give up and use C:.
1218 * Construct the path name of a personal configuration file, given the
1221 * On Win32, if "for_writing" is FALSE, we check whether the file exists
1222 * and, if not, construct a path name relative to the ".wireshark"
1223 * subdirectory of the user's home directory, and check whether that
1224 * exists; if it does, we return that, so that configuration files
1225 * from earlier versions can be read.
1228 get_persconffile_path(const char *filename, gboolean from_profile, gboolean for_writing
1241 path = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s",
1242 get_persconffile_dir(persconfprofile), filename);
1244 path = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s",
1245 get_persconffile_dir(NULL), filename);
1249 if (eth_stat(path, &s_buf) != 0 && errno == ENOENT) {
1251 * OK, it's not in the personal configuration file
1252 * directory; is it in the ".wireshark" subdirectory
1253 * of their home directory?
1255 old_path = g_strdup_printf(
1256 "%s" G_DIR_SEPARATOR_S ".wireshark" G_DIR_SEPARATOR_S "%s",
1257 get_home_dir(), filename);
1258 if (eth_stat(old_path, &s_buf) == 0) {
1260 * OK, it exists; return it instead.
1273 * process command line option belonging to the filesystem settings
1274 * (move this e.g. to main.c and have set_persconffile_dir() instead in this file?)
1277 filesystem_opt(int opt _U_, const char *optarg)
1281 colonp = strchr(optarg, ':');
1282 if (colonp == NULL) {
1290 * Skip over any white space (there probably won't be any, but
1291 * as we allow it in the preferences file, we might as well
1294 while (isspace((guchar)*p))
1298 * Put the colon back, so if our caller uses, in an
1299 * error message, the string they passed us, the message
1306 /* directory should be existing */
1307 /* XXX - is this a requirement? */
1308 if(test_for_directory(p) != EISDIR) {
1310 * Put the colon back, so if our caller uses, in an
1311 * error message, the string they passed us, the message
1318 if (strcmp(optarg,"persconf") == 0) {
1319 persconffile_dir = p;
1320 } else if (strcmp(optarg,"persdata") == 0) {
1321 persdatafile_dir = p;
1322 /* XXX - might need to add the temp file path */
1326 *colonp = ':'; /* put the colon back */
1331 * Construct the path name of a global configuration file, given the
1335 get_datafile_path(const char *filename)
1338 return g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", get_datafile_dir(),
1344 deletefile(const char *path)
1346 return eth_unlink(path) == 0;
1350 * Construct and return the path name of a file in the
1351 * appropriate temporary file directory.
1353 char *get_tempfile_path(const char *filename)
1356 return g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", g_get_tmp_dir(), filename);
1360 * Return an error message for UNIX-style errno indications on open or
1361 * create operations.
1364 file_open_error_message(int err, gboolean for_writing)
1367 static char errmsg_errno[1024+1];
1373 errmsg = "The path to the file \"%s\" doesn't exist.";
1375 errmsg = "The file \"%s\" doesn't exist.";
1380 errmsg = "You don't have permission to create or write to the file \"%s\".";
1382 errmsg = "You don't have permission to read the file \"%s\".";
1386 errmsg = "\"%s\" is a directory (folder), not a file.";
1390 errmsg = "The file \"%s\" could not be created because there is no space left on the file system.";
1395 errmsg = "The file \"%s\" could not be created because you are too close to, or over, your disk quota.";
1400 errmsg = "The file \"%s\" could not be created because an invalid filename was specified.";
1404 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
1405 "The file \"%%s\" could not be %s: %s.",
1406 for_writing ? "created" : "opened",
1408 errmsg = errmsg_errno;
1415 * Return an error message for UNIX-style errno indications on write
1419 file_write_error_message(int err)
1422 static char errmsg_errno[1024+1];
1427 errmsg = "The file \"%s\" could not be saved because there is no space left on the file system.";
1432 errmsg = "The file \"%s\" could not be saved because you are too close to, or over, your disk quota.";
1437 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
1438 "An error occurred while writing to the file \"%%s\": %s.",
1440 errmsg = errmsg_errno;
1448 file_exists(const char *fname)
1450 struct stat file_stat;
1455 * This is a bit tricky on win32. The st_ino field is documented as:
1456 * "The inode, and therefore st_ino, has no meaning in the FAT, ..."
1457 * but it *is* set to zero if stat() returns without an error,
1458 * so this is working, but maybe not quite the way expected. ULFL
1460 file_stat.st_ino = 1; /* this will make things work if an error occured */
1461 eth_stat(fname, &file_stat);
1462 if (file_stat.st_ino == 0) {
1468 if (eth_stat(fname, &file_stat) != 0 && errno == ENOENT) {
1478 * Check that the from file is not the same as to file
1479 * We do it here so we catch all cases ...
1480 * Unfortunately, the file requester gives us an absolute file
1481 * name and the read file name may be relative (if supplied on
1482 * the command line), so we can't just compare paths. From Joerg Mayer.
1485 files_identical(const char *fname1, const char *fname2)
1487 /* Two different implementations, because:
1489 * - _fullpath is not available on UN*X, so we can't get full
1490 * paths and compare them (which wouldn't work with hard links
1493 * - st_ino isn't filled in with a meaningful value on Windows.
1496 char full1[MAX_PATH], full2[MAX_PATH];
1499 * Get the absolute full paths of the file and compare them.
1500 * That won't work if you have hard links, but those aren't
1501 * much used on Windows, even though NTFS supports them.
1503 * XXX - will _fullpath work with UNC?
1505 if( _fullpath( full1, fname1, MAX_PATH ) == NULL ) {
1509 if( _fullpath( full2, fname2, MAX_PATH ) == NULL ) {
1513 if(strcmp(full1, full2) == 0) {
1519 struct stat filestat1, filestat2;
1522 * Compare st_dev and st_ino.
1524 if (eth_stat(fname1, &filestat1) == -1)
1525 return FALSE; /* can't get info about the first file */
1526 if (eth_stat(fname2, &filestat2) == -1)
1527 return FALSE; /* can't get info about the second file */
1528 return (filestat1.st_dev == filestat2.st_dev &&
1529 filestat1.st_ino == filestat2.st_ino);