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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 * Required with GNU libc to get dladdr().
29 * We define it here because <dlfcn.h> apparently gets included by
30 * one of the headers we include below.
54 #ifdef HAVE_SYS_STAT_H
62 #include <wsutil/unicode-utils.h>
65 #include <mach-o/dyld.h>
68 #include <sys/utsname.h>
71 #include <sys/types.h>
72 #include <sys/sysctl.h>
80 #include "filesystem.h"
81 #include <wsutil/report_err.h>
82 #include <wsutil/privileges.h>
83 #include <wsutil/file_util.h>
85 #include <wiretap/wtap.h> /* for WTAP_ERR_SHORT_WRITE */
87 #define PROFILES_DIR "profiles"
88 #define PLUGINS_DIR_NAME "plugins"
90 #define U3_MY_CAPTURES "\\My Captures"
92 char *persconffile_dir = NULL;
93 char *persdatafile_dir = NULL;
94 char *persconfprofile = NULL;
96 static gboolean do_store_persconffiles = FALSE;
97 static GHashTable *profile_files = NULL;
100 * Given a pathname, return a pointer to the last pathname separator
101 * character in the pathname, or NULL if the pathname contains no
105 find_last_pathname_separator(const char *path)
113 * We have to scan for '\' or '/'.
114 * Get to the end of the string.
116 separator = strchr(path, '\0'); /* points to ending '\0' */
117 while (separator > path) {
119 if (c == '\\' || c == '/')
120 return separator; /* found it */
124 * OK, we didn't find any, so no directories - but there might
125 * be a drive letter....
127 return strchr(path, ':');
129 separator = strrchr(path, '/');
135 * Given a pathname, return the last component.
138 get_basename(const char *path)
140 const char *filename;
142 g_assert(path != NULL);
143 filename = find_last_pathname_separator(path);
144 if (filename == NULL) {
146 * There're no directories, drive letters, etc. in the
147 * name; the pathname *is* the file name.
152 * Skip past the pathname or drive letter separator.
160 * Given a pathname, return a string containing everything but the
161 * last component. NOTE: this overwrites the pathname handed into
165 get_dirname(char *path)
169 g_assert(path != NULL);
170 separator = find_last_pathname_separator(path);
171 if (separator == NULL) {
173 * There're no directories, drive letters, etc. in the
174 * name; there is no directory path to return.
180 * Get rid of the last pathname separator and the final file
186 * "path" now contains the pathname of the directory containing
187 * the file/directory to which it referred.
193 * Given a pathname, return:
195 * the errno, if an attempt to "stat()" the file fails;
197 * EISDIR, if the attempt succeeded and the file turned out
200 * 0, if the attempt succeeded and the file turned out not
205 * Visual C++ on Win32 systems doesn't define these. (Old UNIX systems don't
206 * define them either.)
208 * Visual C++ on Win32 systems doesn't define S_IFIFO, it defines _S_IFIFO.
211 #define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
214 #define S_IFIFO _S_IFIFO
217 #define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO)
220 #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
224 test_for_directory(const char *path)
228 if (ws_stat64(path, &statb) < 0)
231 if (S_ISDIR(statb.st_mode))
238 test_for_fifo(const char *path)
242 if (ws_stat64(path, &statb) < 0)
245 if (S_ISFIFO(statb.st_mode))
252 * Directory from which the executable came.
254 static char *progfile_dir;
258 * Directory of the application bundle in which we're contained,
259 * if we're contained in an application bundle. Otherwise, NULL.
261 * Note: Table 2-5 "Subdirectories of the Contents directory" of
263 * https://developer.apple.com/library/mac/documentation/CoreFoundation/Conceptual/CFBundles/BundleTypes/BundleTypes.html#//apple_ref/doc/uid/10000123i-CH101-SW1
265 * says that the "Frameworks" directory
267 * Contains any private shared libraries and frameworks used by the
268 * executable. The frameworks in this directory are revision-locked
269 * to the application and cannot be superseded by any other, even
270 * newer, versions that may be available to the operating system. In
271 * other words, the frameworks included in this directory take precedence
272 * over any other similarly named frameworks found in other parts of
273 * the operating system. For information on how to add private
274 * frameworks to your application bundle, see Framework Programming Guide.
276 * so if we were to ship with any frameworks (e.g. Qt) we should
277 * perhaps put them in a Frameworks directory rather than under
280 * It also says that the "PlugIns" directory
282 * Contains loadable bundles that extend the basic features of your
283 * application. You use this directory to include code modules that
284 * must be loaded into your applicationbs process space in order to
285 * be used. You would not use this directory to store standalone
288 * Our plugins are just raw .so/.dylib files; I don't know whether by
289 * "bundles" they mean application bundles (i.e., directory hierarchies)
290 * or just "bundles" in the Mach-O sense (which are an image type that
291 * can be loaded with dlopen() but not linked as libraries; our plugins
292 * are, I think, built as dylibs and can be loaded either way).
294 * And it says that the "SharedSupport" directory
296 * Contains additional non-critical resources that do not impact the
297 * ability of the application to run. You might use this directory to
298 * include things like document templates, clip art, and tutorials
299 * that your application expects to be present but that do not affect
300 * the ability of your application to run.
302 * I don't think I'd put the files that currently go under Resources/share
303 * into that category; they're not, for example, sample Lua scripts that
304 * don't actually get run by Wireshark, they're configuration/data files
305 * for Wireshark whose absence might not prevent Wireshark from running
306 * but that would affect how it behaves when run.
308 static char *appbundle_dir;
312 * TRUE if we're running from the build directory and we aren't running
313 * with special privileges.
315 static gboolean running_in_build_directory_flag = FALSE;
319 * Get the pathname of the executable using various platform-
320 * dependent mechanisms for various UN*Xes.
322 * These calls all should return something independent of the argv[0]
323 * passed to the program, so it shouldn't be fooled by an argv[0]
324 * that doesn't match the executable path.
326 * Sadly, not all UN*Xes necessarily have dladdr(), and those that
327 * do don't necessarily have dladdr(main) return information about
328 * the executable image, and those that do aren't necessarily running
329 * on a platform wherein the executable image can get its own path
330 * from the kernel (either by a call or by it being handed to it along
331 * with argv[] and the environment), and those that can don't
332 * necessarily use that to supply the path you get from dladdr(main),
333 * so we try this first and, if that fails, use dladdr(main) if
336 * In particular, some dynamic linkers supply a dladdr() such that
337 * dladdr(main) just returns something derived from argv[0], so
338 * just using dladdr(main) is the wrong thing to do if there's
339 * another mechanism that can get you a more reliable version of
340 * the executable path.
342 * However, at least in newer versions of DragonFly BSD, the dynamic
343 * linker *does* get it from the aux vector passed to the program
344 * by the kernel, readlink /proc/curproc/file - which came first?
346 * On OpenBSD, dladdr(main) returns a value derived from argv[0],
347 * and there doesn't appear to be any way to get the executable path
348 * from the kernel, so we're out of luck there.
350 * So, on platforms where some versions have a version of dladdr()
351 * that gives an argv[0]-based path and that also have a mechanism
352 * to get a more reliable version of the path, we try that. On
353 * other platforms, we return NULL. If our caller gets back a NULL
354 * from us, it falls back on dladdr(main) if dladdr() is available,
355 * and if that fails or is unavailable, it falls back on processing
358 * This is not guaranteed to return an absolute path; if it doesn't,
359 * our caller must prepend the current directory if it's a path.
361 * This is not guaranteed to return the "real path"; it might return
362 * something with symbolic links in the path. Our caller must
363 * use realpath() if they want the real thing, but that's also true of
364 * something obtained by looking at argv[0].
367 get_executable_path(void)
369 #if defined(__APPLE__)
370 char *executable_path;
371 uint32_t path_buf_size;
373 path_buf_size = PATH_MAX;
374 executable_path = (char *)g_malloc(path_buf_size);
375 if (_NSGetExecutablePath(executable_path, &path_buf_size) == -1) {
376 executable_path = (char *)g_realloc(executable_path, path_buf_size);
377 if (_NSGetExecutablePath(executable_path, &path_buf_size) == -1)
380 return executable_path;
381 #elif defined(__linux__)
383 * In older versions of GNU libc's dynamic linker, as used on Linux,
384 * dladdr(main) supplies a path based on argv[0], so we use
385 * /proc/self/exe instead; there are Linux distributions with
386 * kernels that support /proc/self/exe and those older versions
387 * of the dynamic linker, and this will get a better answer on
390 * It only works on Linux 2.2 or later, so we just give up on
393 * XXX - are there OS versions that support "exe" but not "self"?
396 static char executable_path[PATH_MAX];
398 if (uname(&name) == -1)
400 if (strncmp(name.release, "1.", 2) == 0)
401 return NULL; /* Linux 1.x */
402 if (strcmp(name.release, "2.0") == 0 ||
403 strncmp(name.release, "2.0.", 4) == 0 ||
404 strcmp(name.release, "2.1") == 0 ||
405 strncmp(name.release, "2.1.", 4) == 0)
406 return NULL; /* Linux 2.0.x or 2.1.x */
407 if (readlink("/proc/self/exe", executable_path, sizeof executable_path) == -1)
409 return executable_path;
410 #elif defined(__FreeBSD__) && defined(KERN_PROC_PATHNAME)
412 * In older versions of FreeBSD's dynamic linker, dladdr(main)
413 * supplies a path based on argv[0], so we use the KERN_PROC_PATHNAME
414 * sysctl instead; there are, I think, versions of FreeBSD
415 * that support the sysctl that have and those older versions
416 * of the dynamic linker, and this will get a better answer on
420 char executable_path*;
421 size_t path_buf_size;
425 mib[2] = KERN_PROC_PATHNAME;
427 path_buf_size = PATH_MAX;
428 executable_path = (char *)g_malloc(path_buf_size);
429 if (sysctl(mib, 4, executable_path, &path_buf_size, NULL, 0) == -1) {
432 executable_path = (char *)g_realloc(executable_path, path_buf_size);
433 if (sysctl(mib, 4, executable_path, &path_buf_size, NULL, 0) == -1)
436 return executable_path;
437 #elif defined(__NetBSD__)
439 * In all versions of NetBSD's dynamic linker as of 2013-08-12,
440 * dladdr(main) supplies a path based on argv[0], so we use
441 * /proc/curproc/exe instead.
443 * XXX - are there OS versions that support "exe" but not "curproc"
444 * or "self"? Are there any that support "self" but not "curproc"?
446 static char executable_path[PATH_MAX];
448 if (readlink("/proc/curproc/exe", executable_path, sizeof executable_path) == -1)
450 return executable_path;
451 #elif defined(__DragonFly__)
453 * In older versions of DragonFly BSD's dynamic linker, dladdr(main)
454 * supplies a path based on argv[0], so we use /proc/curproc/file
455 * instead; it appears to be supported by all versions of DragonFly
458 static char executable_path[PATH_MAX];
460 if (readlink("/proc/curproc/file", executable_path, sizeof executable_path) == -1)
462 return executable_path;
463 #elif (defined(sun) || defined(__sun)) && defined(HAVE_GETEXECNAME)
465 * It appears that getexecname() dates back to at least Solaris 8,
466 * but /proc/*/path is first documented in the Solaris 10 documentation,
467 * so we use getexecname() if available, rather than /proc/self/path/a.out
468 * (which isn't documented, but appears to be a symlink to the
469 * executable image file).
471 return getexecname();
473 /* Fill in your favorite UN*X's code here, if there is something */
480 * Get the pathname of the directory from which the executable came,
481 * and save it for future use. Returns NULL on success, and a
482 * g_mallocated string containing an error on failure.
485 init_progfile_dir(const char *arg0
489 , int (*main_addr)(int, char **)
490 #if defined(_WIN32) || !defined(HAVE_DLADDR)
496 TCHAR prog_pathname_w[_MAX_PATH+2];
504 * Attempt to get the full pathname of the currently running
507 if (GetModuleFileName(NULL, prog_pathname_w, G_N_ELEMENTS(prog_pathname_w)) != 0 && GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
509 * XXX - Should we use g_utf16_to_utf8(), as in
512 prog_pathname = utf_16to8(prog_pathname_w);
514 * We got it; strip off the last component, which would be
515 * the file name of the executable, giving us the pathname
516 * of the directory where the executable resides.
518 progfile_dir = g_path_get_dirname(prog_pathname);
519 if (progfile_dir != NULL) {
520 return NULL; /* we succeeded */
523 * OK, no. What do we do now?
525 return g_strdup_printf("No \\ in executable pathname \"%s\"",
530 * Oh, well. Return an indication of the error.
532 error = GetLastError();
533 if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
534 NULL, error, 0, (LPTSTR) &msg_w, 0, NULL) == 0) {
536 * Gak. We can't format the message.
538 return g_strdup_printf("GetModuleFileName failed: %u (FormatMessage failed: %u)",
539 error, GetLastError());
541 msg = utf_16to8(msg_w);
544 * "FormatMessage()" "helpfully" sticks CR/LF at the
545 * end of the message. Get rid of it.
547 msglen = strlen(msg);
549 msg[msglen - 1] = '\0';
550 msg[msglen - 2] = '\0';
552 return g_strdup_printf("GetModuleFileName failed: %s (%u)",
559 const char *execname;
564 char *path_start, *path_end;
565 size_t path_component_len;
571 * Check whether WIRESHARK_RUN_FROM_BUILD_DIRECTORY is set in the
572 * environment; if so, set running_in_build_directory_flag if we
573 * weren't started with special privileges. (If we were started
574 * with special privileges, it's not safe to allow the user to point
575 * us to some other directory; running_in_build_directory_flag, when
576 * set, causes us to look for plugins and the like in the build
579 if (getenv("WIRESHARK_RUN_FROM_BUILD_DIRECTORY") != NULL
580 && !started_with_special_privs())
581 running_in_build_directory_flag = TRUE;
583 execname = get_executable_path();
585 if (execname == NULL) {
587 * Try to use dladdr() to find the pathname of the executable.
588 * dladdr() is not guaranteed to give you anything better than
589 * argv[0] (i.e., it might not contain a / at all, much less
590 * being an absolute path), and doesn't appear to do so on
591 * Linux, but on other platforms it could give you an absolute
592 * path and obviate the need for us to determine the absolute
595 if (dladdr((void *)main_addr, &info))
596 execname = info.dli_fname;
599 if (execname == NULL) {
601 * OK, guess based on argv[0].
607 * Try to figure out the directory in which the currently running
608 * program resides, given something purporting to be the executable
609 * name (from dladdr() or from the argv[0] it was started with.
610 * That might be the absolute path of the program, or a path relative
611 * to the current directory of the process that started it, or
612 * just a name for the program if it was started from the command
613 * line and was searched for in $PATH. It's not guaranteed to be
614 * any of those, however, so there are no guarantees....
616 if (execname[0] == '/') {
618 * It's an absolute path.
620 prog_pathname = g_strdup(execname);
621 } else if (strchr(execname, '/') != NULL) {
623 * It's a relative path, with a directory in it.
624 * Get the current directory, and combine it
625 * with that directory.
627 path_max = pathconf(".", _PC_PATH_MAX);
628 if (path_max == -1) {
630 * We have no idea how big a buffer to
631 * allocate for the current directory.
633 return g_strdup_printf("pathconf failed: %s\n",
636 curdir = (char *)g_malloc(path_max);
637 if (getcwd(curdir, path_max) == NULL) {
639 * It failed - give up, and just stick
643 return g_strdup_printf("getcwd failed: %s\n",
646 path = g_strdup_printf("%s/%s", curdir, execname);
648 prog_pathname = path;
651 * It's just a file name.
652 * Search the path for a file with that name
655 prog_pathname = NULL; /* haven't found it yet */
656 pathstr = getenv("PATH");
657 path_start = pathstr;
658 if (path_start != NULL) {
659 while (*path_start != '\0') {
660 path_end = strchr(path_start, ':');
661 if (path_end == NULL)
662 path_end = path_start + strlen(path_start);
663 path_component_len = path_end - path_start;
664 path = (char *)g_malloc(path_component_len + 1
665 + strlen(execname) + 1);
666 memcpy(path, path_start, path_component_len);
667 path[path_component_len] = '\0';
668 strncat(path, "/", 2);
669 strncat(path, execname, strlen(execname) + 1);
670 if (access(path, X_OK) == 0) {
674 prog_pathname = path;
679 * That's not it. If there are more
680 * path components to test, try them.
682 if (*path_end == '\0') {
684 * There's nothing more to try.
688 if (*path_end == ':')
690 path_start = path_end;
693 if (prog_pathname == NULL) {
695 * Program not found in path.
697 return g_strdup_printf("\"%s\" not found in \"%s\"",
703 * XXX - should we pick a default?
705 return g_strdup("PATH isn't set");
710 * OK, we have what we think is the pathname
713 * First, find the last "/" in the directory,
714 * as that marks the end of the directory pathname.
716 dir_end = strrchr(prog_pathname, '/');
717 if (dir_end != NULL) {
719 * Found it. Strip off the last component,
720 * as that's the path of the program.
725 * Is there a "/.libs" at the end?
727 dir_end = strrchr(prog_pathname, '/');
728 if (dir_end != NULL) {
729 if (strcmp(dir_end, "/.libs") == 0) {
732 * Strip that off; it's an
733 * artifact of libtool.
738 * This presumably means we're run from
739 * the libtool wrapper, which probably
740 * means we're being run from the build
741 * directory. If we weren't started
742 * with special privileges, set
743 * running_in_build_directory_flag.
745 * XXX - should we check whether what
746 * follows ".libs/" begins with "lt-"?
748 if (!started_with_special_privs())
749 running_in_build_directory_flag = TRUE;
753 if (!started_with_special_privs()) {
755 * Scan up the path looking for a component
756 * named "Contents". If we find it, we assume
757 * we're in a bundle, and that the top-level
758 * directory of the bundle is the one containing
761 * Not all executables are in the Contents/MacOS
762 * directory, so we can't just check for those
763 * in the path and strip them off.
765 * XXX - should we assume that it's either
766 * Contents/MacOS or Resources/bin?
768 char *component_end, *p;
770 component_end = strchr(prog_pathname, '\0');
773 while (p >= prog_pathname && *p != '/')
775 if (p == prog_pathname) {
777 * We're looking at the first component of
778 * the pathname now, so we're definitely
779 * not in a bundle, even if we're in
784 if (strncmp(p, "/Contents", component_end - p) == 0) {
786 appbundle_dir = (char *)g_malloc(p - prog_pathname + 1);
787 memcpy(appbundle_dir, prog_pathname, p - prog_pathname);
788 appbundle_dir[p - prog_pathname] = '\0';
800 * OK, we have the path we want.
802 progfile_dir = prog_pathname;
806 * This "shouldn't happen"; we apparently
807 * have no "/" in the pathname.
808 * Just free up prog_pathname.
810 retstr = g_strdup_printf("No / found in \"%s\"", prog_pathname);
811 g_free(prog_pathname);
818 * Get the directory in which the program resides.
821 get_progfile_dir(void)
827 * Get the directory in which the global configuration and data files are
830 * On Windows, we use the directory in which the executable for this
833 * On UN*X, we use the DATAFILE_DIR value supplied by the configure
834 * script, unless we think we're being run from the build directory,
835 * in which case we use the directory in which the executable for this
838 * XXX - if we ever make libwireshark a real library, used by multiple
839 * applications (more than just TShark and versions of Wireshark with
840 * various UIs), should the configuration files belong to the library
841 * (and be shared by all those applications) or to the applications?
843 * If they belong to the library, that could be done on UNIX by the
844 * configure script, but it's trickier on Windows, as you can't just
845 * use the pathname of the executable.
847 * If they belong to the application, that could be done on Windows
848 * by using the pathname of the executable, but we'd have to have it
849 * passed in as an argument, in some call, on UNIX.
851 * Note that some of those configuration files might be used by code in
852 * libwireshark, some of them might be used by dissectors (would they
853 * belong to libwireshark, the application, or a separate library?),
854 * and some of them might be used by other code (the Wireshark preferences
855 * file includes resolver preferences that control the behavior of code
856 * in libwireshark, dissector preferences, and UI preferences, for
860 get_datafile_dir(void)
863 char *u3deviceexecpath;
865 static const char *datafile_dir = NULL;
867 if (datafile_dir != NULL)
872 * See if we are running in a U3 environment.
874 u3deviceexecpath = getenv_utf8("U3_DEVICE_EXEC_PATH");
876 if (u3deviceexecpath != NULL) {
878 * We are; use the U3 device executable path.
880 datafile_dir = u3deviceexecpath;
883 * Do we have the pathname of the program? If so, assume we're
884 * running an installed version of the program. If we fail,
885 * we don't change "datafile_dir", and thus end up using the
888 * XXX - does NSIS put the installation directory into
889 * "\HKEY_LOCAL_MACHINE\SOFTWARE\Wireshark\InstallDir"?
890 * If so, perhaps we should read that from the registry,
893 if (progfile_dir != NULL) {
895 * Yes, we do; use that.
897 datafile_dir = progfile_dir;
901 * Fall back on the default installation directory.
903 datafile_dir = "C:\\Program Files\\Wireshark\\";
907 if (running_in_build_directory_flag) {
909 * We're (probably) being run from the build directory and
910 * weren't started with special privileges.
912 * The data files we want are the ones from the source
913 * directory; to handle builds out of the source tree,
914 * we check whether WIRESHARK_SRC_DIR is set and, if so,
915 * use that as the source directory.
917 datafile_dir = getenv("WIRESHARK_SRC_DIR");
918 if (datafile_dir != NULL)
923 * Well, that didn't work.
924 * Check again whether we were (probably) run from the build
925 * directory and started without special privileges, and also
926 * check whether we were able to determine the directory in
927 * which the program was found.
929 * (running_in_build_directory_flag is never set to TRUE
930 * if we're started with special privileges, so we need
931 * only check it; we don't need to call started_with_special_privs().)
933 if (running_in_build_directory_flag && progfile_dir != NULL) {
935 * We're (probably) being run from the build directory and
936 * weren't started with special privileges, and we were
937 * able to determine the directory in which the program
938 * was found. Assume that directory is the build
939 * directory and that it's the same as the source
942 datafile_dir = progfile_dir;
945 * Return the directory specified when the build was
946 * configured, prepending the run path prefix if it exists.
948 if (getenv("WIRESHARK_DATA_DIR") && !started_with_special_privs()) {
950 * The user specified a different directory for data files
951 * and we aren't running with special privileges.
952 * XXX - We might be able to dispense with the priv check
954 datafile_dir = g_strdup(getenv("WIRESHARK_DATA_DIR"));
958 * If we're running from an app bundle and weren't started
959 * with special privileges, use the Contents/Resources/share/wireshark
960 * subdirectory of the app bundle.
962 * (appbundle_dir is not set to a non-null value if we're
963 * started with special privileges, so we need only check
964 * it; we don't need to call started_with_special_privs().)
966 else if (appbundle_dir != NULL) {
967 datafile_dir = g_strdup_printf("%s/Contents/Resources/share/wireshark",
972 datafile_dir = DATAFILE_DIR;
982 * Find the directory where the python dissectors are stored.
984 * On Windows, we use the "py_dissector" subdirectory of the datafile directory.
986 * On UN*X, we use the PYTHON_DIR value supplied by the configure
987 * script, unless we think we're being run from the build directory,
988 * in which case we use the "py_dissector" subdirectory of the datafile directory.
990 * In both cases, we then use the subdirectory of that directory whose
991 * name is the version number.
993 * XXX - if we think we're being run from the build directory, perhaps we
994 * should have the plugin code not look in the version subdirectory
995 * of the plugin directory, but look in all of the subdirectories
996 * of the plugin directory, so it can just fetch the plugins built
997 * as part of the build process.
999 static const char *wspython_dir = NULL;
1002 init_wspython_dir(void)
1006 * On Windows, the data file directory is the installation
1007 * directory; the python dissectors are stored under it.
1009 * Assume we're running the installed version of Wireshark;
1010 * on Windows, the data file directory is the directory
1011 * in which the Wireshark binary resides.
1013 wspython_dir = g_strdup_printf("%s\\python\\%s", get_datafile_dir(),
1017 * Make sure that pathname refers to a directory.
1019 if (test_for_directory(wspython_dir) != EISDIR) {
1021 * Either it doesn't refer to a directory or it
1022 * refers to something that doesn't exist.
1024 * Assume that means we're running a version of
1025 * Wireshark we've built in a build directory,
1026 * in which case {datafile dir}\python is the
1027 * top-level plugins source directory, and use
1028 * that directory and set the "we're running in
1029 * a build directory" flag, so the plugin
1030 * scanner will check all subdirectories of that
1031 * directory for python dissectors.
1033 g_free( (gpointer) wspython_dir);
1034 wspython_dir = g_strdup_printf("%s\\python", get_datafile_dir());
1035 running_in_build_directory_flag = TRUE;
1038 if (running_in_build_directory_flag) {
1040 * We're (probably) being run from the build directory and
1041 * weren't started with special privileges, so we'll use
1042 * the "python" subdirectory of the datafile directory
1043 * (the datafile directory is the build directory).
1045 wspython_dir = g_strdup_printf("%s/epan/wspython/", get_datafile_dir());
1047 if (getenv("WIRESHARK_PYTHON_DIR") && !started_with_special_privs()) {
1049 * The user specified a different directory for plugins
1050 * and we aren't running with special privileges.
1052 wspython_dir = g_strdup(getenv("WIRESHARK_PYTHON_DIR"));
1056 * If we're running from an app bundle and weren't started
1057 * with special privileges, use the Contents/Resources/lib/wireshark/python
1058 * subdirectory of the app bundle.
1060 * (appbundle_dir is not set to a non-null value if we're
1061 * started with special privileges, so we need only check
1062 * it; we don't need to call started_with_special_privs().)
1064 else if (appbundle_dir != NULL) {
1065 wspython_dir = g_strdup_printf("%s/Contents/Resources/lib/wireshark/python",
1070 wspython_dir = PYTHON_DIR;
1075 #endif /* HAVE_PYTHON */
1078 * Get the directory in which the python dissectors are stored.
1081 get_wspython_dir(void)
1084 if (!wspython_dir) init_wspython_dir();
1085 return wspython_dir;
1092 #if defined(HAVE_PLUGINS) || defined(HAVE_LUA)
1094 * Find the directory where the plugins are stored.
1096 * On Windows, we use the "plugin" subdirectory of the datafile directory.
1098 * On UN*X, we use the PLUGIN_DIR value supplied by the configure
1099 * script, unless we think we're being run from the build directory,
1100 * in which case we use the "plugin" subdirectory of the datafile directory.
1102 * In both cases, we then use the subdirectory of that directory whose
1103 * name is the version number.
1105 * XXX - if we think we're being run from the build directory, perhaps we
1106 * should have the plugin code not look in the version subdirectory
1107 * of the plugin directory, but look in all of the subdirectories
1108 * of the plugin directory, so it can just fetch the plugins built
1109 * as part of the build process.
1111 static const char *plugin_dir = NULL;
1114 init_plugin_dir(void)
1118 * On Windows, the data file directory is the installation
1119 * directory; the plugins are stored under it.
1121 * Assume we're running the installed version of Wireshark;
1122 * on Windows, the data file directory is the directory
1123 * in which the Wireshark binary resides.
1125 plugin_dir = g_strdup_printf("%s\\plugins\\%s", get_datafile_dir(),
1129 * Make sure that pathname refers to a directory.
1131 if (test_for_directory(plugin_dir) != EISDIR) {
1133 * Either it doesn't refer to a directory or it
1134 * refers to something that doesn't exist.
1136 * Assume that means we're running a version of
1137 * Wireshark we've built in a build directory,
1138 * in which case {datafile dir}\plugins is the
1139 * top-level plugins source directory, and use
1140 * that directory and set the "we're running in
1141 * a build directory" flag, so the plugin
1142 * scanner will check all subdirectories of that
1143 * directory for plugins.
1145 g_free( (gpointer) plugin_dir);
1146 plugin_dir = g_strdup_printf("%s\\plugins", get_datafile_dir());
1147 running_in_build_directory_flag = TRUE;
1150 if (running_in_build_directory_flag) {
1152 * We're (probably) being run from the build directory and
1153 * weren't started with special privileges, so we'll use
1154 * the "plugins" subdirectory of the datafile directory
1155 * (the datafile directory is the build directory).
1157 plugin_dir = g_strdup_printf("%s/plugins", get_datafile_dir());
1159 if (getenv("WIRESHARK_PLUGIN_DIR") && !started_with_special_privs()) {
1161 * The user specified a different directory for plugins
1162 * and we aren't running with special privileges.
1164 plugin_dir = g_strdup(getenv("WIRESHARK_PLUGIN_DIR"));
1168 * If we're running from an app bundle and weren't started
1169 * with special privileges, use the Contents/Resources/lib/wireshark/plugins
1170 * subdirectory of the app bundle.
1172 * (appbundle_dir is not set to a non-null value if we're
1173 * started with special privileges, so we need only check
1174 * it; we don't need to call started_with_special_privs().)
1176 else if (appbundle_dir != NULL) {
1177 plugin_dir = g_strdup_printf("%s/Contents/Resources/lib/wireshark/plugins",
1182 plugin_dir = PLUGIN_DIR;
1187 #endif /* HAVE_PLUGINS || HAVE_LUA */
1190 * Get the directory in which the plugins are stored.
1193 get_plugin_dir(void)
1195 #if defined(HAVE_PLUGINS) || defined(HAVE_LUA)
1196 if (!plugin_dir) init_plugin_dir();
1204 * Get the flag indicating whether we're running from a build
1208 running_in_build_directory(void)
1210 return running_in_build_directory_flag;
1214 * Get the directory in which files that, at least on UNIX, are
1215 * system files (such as "/etc/ethers") are stored; on Windows,
1216 * there's no "/etc" directory, so we get them from the global
1217 * configuration and data file directory.
1220 get_systemfile_dir(void)
1223 return get_datafile_dir();
1230 * Name of directory, under the user's home directory, in which
1231 * personal configuration files are stored.
1234 #define PF_DIR "Wireshark"
1237 * XXX - should this be ".libepan"? For backwards-compatibility, I'll keep
1238 * it ".wireshark" for now.
1240 #define PF_DIR ".wireshark"
1244 set_profile_name(const gchar *profilename)
1246 g_free (persconfprofile);
1248 if (profilename && strlen(profilename) > 0 &&
1249 strcmp(profilename, DEFAULT_PROFILE) != 0) {
1250 persconfprofile = g_strdup (profilename);
1252 /* Default Profile */
1253 persconfprofile = NULL;
1258 get_profile_name(void)
1260 if (persconfprofile) {
1261 return persconfprofile;
1263 return DEFAULT_PROFILE;
1268 is_default_profile(void)
1270 return (!persconfprofile || strcmp(persconfprofile, DEFAULT_PROFILE) == 0) ? TRUE : FALSE;
1274 has_global_profiles(void)
1278 const gchar *global_dir = get_global_profiles_dir();
1280 gboolean has_global = FALSE;
1282 if ((test_for_directory(global_dir) == EISDIR) &&
1283 ((dir = ws_dir_open(global_dir, 0, NULL)) != NULL))
1285 while ((file = ws_dir_read_name(dir)) != NULL) {
1286 filename = g_strdup_printf ("%s%s%s", global_dir, G_DIR_SEPARATOR_S,
1287 ws_dir_get_name(file));
1288 if (test_for_directory(filename) == EISDIR) {
1302 profile_store_persconffiles(gboolean store)
1305 profile_files = g_hash_table_new (g_str_hash, g_str_equal);
1307 do_store_persconffiles = store;
1311 * Get the directory in which personal configuration files reside;
1312 * in UNIX-compatible systems, it's ".wireshark", under the user's home
1313 * directory, and on Windows systems, it's "Wireshark", under %APPDATA%
1314 * or, if %APPDATA% isn't set, it's "%USERPROFILE%\Application Data"
1315 * (which is what %APPDATA% normally is on Windows 2000).
1318 get_persconffile_dir_no_profile(void)
1322 char *userprofiledir;
1323 char *u3appdatapath;
1325 const char *homedir;
1329 /* Return the cached value, if available */
1330 if (persconffile_dir != NULL)
1331 return persconffile_dir;
1335 * See if we are running in a U3 environment.
1337 u3appdatapath = getenv_utf8("U3_APP_DATA_PATH");
1338 if (u3appdatapath != NULL) {
1340 * We are; use the U3 application data path.
1342 persconffile_dir = u3appdatapath;
1345 * Use %APPDATA% or %USERPROFILE%, so that configuration
1346 * files are stored in the user profile, rather than in
1347 * the home directory. The Windows convention is to store
1348 * configuration information in the user profile, and doing
1349 * so means you can use Wireshark even if the home directory
1350 * is an inaccessible network drive.
1352 appdatadir = getenv_utf8("APPDATA");
1353 if (appdatadir != NULL) {
1355 * Concatenate %APPDATA% with "\Wireshark".
1357 persconffile_dir = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s",
1358 appdatadir, PF_DIR);
1361 * OK, %APPDATA% wasn't set, so use
1362 * %USERPROFILE%\Application Data.
1364 userprofiledir = getenv_utf8("USERPROFILE");
1365 if (userprofiledir != NULL) {
1366 persconffile_dir = g_strdup_printf(
1367 "%s" G_DIR_SEPARATOR_S "Application Data" G_DIR_SEPARATOR_S "%s",
1368 userprofiledir, PF_DIR);
1371 * Give up and use "C:".
1373 persconffile_dir = g_strdup_printf("C:" G_DIR_SEPARATOR_S "%s", PF_DIR);
1379 * If $HOME is set, use that.
1381 homedir = getenv("HOME");
1382 if (homedir == NULL) {
1384 * Get their home directory from the password file.
1385 * If we can't even find a password file entry for them,
1388 pwd = getpwuid(getuid());
1390 homedir = pwd->pw_dir;
1395 persconffile_dir = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", homedir, PF_DIR);
1398 return persconffile_dir;
1402 get_profiles_dir(void)
1404 static char *profiles_dir = NULL;
1406 g_free (profiles_dir);
1407 profiles_dir = g_strdup_printf ("%s%s%s", get_persconffile_dir_no_profile (),
1408 G_DIR_SEPARATOR_S, PROFILES_DIR);
1410 return profiles_dir;
1414 get_global_profiles_dir(void)
1416 static char *global_profiles_dir = NULL;
1418 if (!global_profiles_dir) {
1419 global_profiles_dir = g_strdup_printf ("%s%s%s", get_datafile_dir(),
1420 G_DIR_SEPARATOR_S, PROFILES_DIR);
1423 return global_profiles_dir;
1427 get_persconffile_dir(const gchar *profilename)
1429 static char *persconffile_profile_dir = NULL;
1431 g_free (persconffile_profile_dir);
1433 if (profilename && strlen(profilename) > 0 &&
1434 strcmp(profilename, DEFAULT_PROFILE) != 0) {
1435 persconffile_profile_dir = g_strdup_printf ("%s%s%s", get_profiles_dir (),
1436 G_DIR_SEPARATOR_S, profilename);
1438 persconffile_profile_dir = g_strdup (get_persconffile_dir_no_profile ());
1441 return persconffile_profile_dir;
1445 profile_exists(const gchar *profilename, gboolean global)
1448 gchar *path = g_strdup_printf ("%s%s%s", get_global_profiles_dir(),
1449 G_DIR_SEPARATOR_S, profilename);
1450 if (test_for_directory (path) == EISDIR) {
1456 if (test_for_directory (get_persconffile_dir (profilename)) == EISDIR) {
1465 delete_directory (const char *directory, char **pf_dir_path_return)
1472 if ((dir = ws_dir_open(directory, 0, NULL)) != NULL) {
1473 while ((file = ws_dir_read_name(dir)) != NULL) {
1474 filename = g_strdup_printf ("%s%s%s", directory, G_DIR_SEPARATOR_S,
1475 ws_dir_get_name(file));
1476 if (test_for_directory(filename) != EISDIR) {
1477 ret = ws_remove(filename);
1480 /* The user has manually created a directory in the profile directory */
1481 /* I do not want to delete the directory recursively yet */
1482 ret = delete_directory (filename, pf_dir_path_return);
1486 *pf_dir_path_return = filename;
1494 if (ret == 0 && (ret = ws_remove(directory)) != 0) {
1495 *pf_dir_path_return = g_strdup (directory);
1502 delete_persconffile_profile(const char *profilename, char **pf_dir_path_return)
1504 const char *profile_dir = get_persconffile_dir(profilename);
1507 if (test_for_directory (profile_dir) == EISDIR) {
1508 ret = delete_directory (profile_dir, pf_dir_path_return);
1515 rename_persconffile_profile(const char *fromname, const char *toname,
1516 char **pf_from_dir_path_return, char **pf_to_dir_path_return)
1518 char *from_dir = g_strdup (get_persconffile_dir(fromname));
1519 char *to_dir = g_strdup (get_persconffile_dir(toname));
1522 ret = ws_rename (from_dir, to_dir);
1524 *pf_from_dir_path_return = g_strdup (from_dir);
1525 *pf_to_dir_path_return = g_strdup (to_dir);
1535 * Create the directory that holds personal configuration files, if
1536 * necessary. If we attempted to create it, and failed, return -1 and
1537 * set "*pf_dir_path_return" to the pathname of the directory we failed
1538 * to create (it's g_mallocated, so our caller should free it); otherwise,
1542 create_persconffile_profile(const char *profilename, char **pf_dir_path_return)
1544 const char *pf_dir_path;
1546 char *pf_dir_path_copy, *pf_dir_parent_path;
1547 size_t pf_dir_parent_path_len;
1554 * Create the "Default" personal configuration files directory, if necessary.
1556 if (create_persconffile_profile (NULL, pf_dir_path_return) == -1) {
1561 * Check if profiles directory exists.
1562 * If not then create it.
1564 pf_dir_path = get_profiles_dir ();
1565 if (ws_stat64(pf_dir_path, &s_buf) != 0 && errno == ENOENT) {
1566 ret = ws_mkdir(pf_dir_path, 0755);
1568 *pf_dir_path_return = g_strdup(pf_dir_path);
1574 pf_dir_path = get_persconffile_dir(profilename);
1575 if (ws_stat64(pf_dir_path, &s_buf) != 0 && errno == ENOENT) {
1578 * Does the parent directory of that directory
1579 * exist? %APPDATA% may not exist even though
1580 * %USERPROFILE% does.
1582 * We check for the existence of the directory
1583 * by first checking whether the parent directory
1584 * is just a drive letter and, if it's not, by
1585 * doing a "stat()" on it. If it's a drive letter,
1586 * or if the "stat()" succeeds, we assume it exists.
1588 pf_dir_path_copy = g_strdup(pf_dir_path);
1589 pf_dir_parent_path = get_dirname(pf_dir_path_copy);
1590 pf_dir_parent_path_len = strlen(pf_dir_parent_path);
1591 if (pf_dir_parent_path_len > 0
1592 && pf_dir_parent_path[pf_dir_parent_path_len - 1] != ':'
1593 && ws_stat64(pf_dir_parent_path, &s_buf) != 0) {
1595 * No, it doesn't exist - make it first.
1597 ret = ws_mkdir(pf_dir_parent_path, 0755);
1599 *pf_dir_path_return = pf_dir_parent_path;
1603 g_free(pf_dir_path_copy);
1604 ret = ws_mkdir(pf_dir_path, 0755);
1606 ret = ws_mkdir(pf_dir_path, 0755);
1610 * Something with that pathname exists; if it's not
1611 * a directory, we'll get an error if we try to put
1612 * something in it, so we don't fail here, we wait
1613 * for that attempt fo fail.
1618 *pf_dir_path_return = g_strdup(pf_dir_path);
1623 create_persconffile_dir(char **pf_dir_path_return)
1625 return create_persconffile_profile(persconfprofile, pf_dir_path_return);
1629 copy_persconffile_profile(const char *toname, const char *fromname, gboolean from_global,
1630 char **pf_filename_return, char **pf_to_dir_path_return, char **pf_from_dir_path_return)
1633 gchar *to_dir = g_strdup (get_persconffile_dir(toname));
1634 gchar *filename, *from_file, *to_file;
1635 GList *files, *file;
1638 if (strcmp(fromname, DEFAULT_PROFILE) == 0) {
1639 from_dir = g_strdup (get_global_profiles_dir());
1641 from_dir = g_strdup_printf ("%s%s%s", get_global_profiles_dir(), G_DIR_SEPARATOR_S, fromname);
1644 from_dir = g_strdup (get_persconffile_dir(fromname));
1647 files = g_hash_table_get_keys(profile_files);
1648 file = g_list_first(files);
1650 filename = (gchar *)file->data;
1651 from_file = g_strdup_printf ("%s%s%s", from_dir, G_DIR_SEPARATOR_S, filename);
1652 to_file = g_strdup_printf ("%s%s%s", to_dir, G_DIR_SEPARATOR_S, filename);
1654 if (file_exists(from_file) && !copy_file_binary_mode(from_file, to_file)) {
1655 *pf_filename_return = g_strdup(filename);
1656 *pf_to_dir_path_return = to_dir;
1657 *pf_from_dir_path_return = from_dir;
1666 file = g_list_next(file);
1669 g_list_free (files);
1677 * Get the (default) directory in which personal data is stored.
1679 * On Win32, this is the "My Documents" folder in the personal profile,
1680 * except that, if we're running from a U3 device, this is the
1681 * "$U3_DEVICE_DOCUMENT_PATH\My Captures" folder.
1682 * On UNIX this is simply the current directory.
1684 /* XXX - should this and the get_home_dir() be merged? */
1686 get_persdatafile_dir(void)
1689 char *u3devicedocumentpath;
1690 TCHAR tszPath[MAX_PATH];
1694 /* Return the cached value, if available */
1695 if (persdatafile_dir != NULL)
1696 return persdatafile_dir;
1699 * See if we are running in a U3 environment.
1701 u3devicedocumentpath = getenv_utf8("U3_DEVICE_DOCUMENT_PATH");
1703 if (u3devicedocumentpath != NULL) {
1704 /* the "My Captures" sub-directory is created (if it doesn't
1705 exist) by u3util.exe when the U3 Wireshark is first run */
1707 szPath = g_strdup_printf("%s%s", u3devicedocumentpath, U3_MY_CAPTURES);
1709 persdatafile_dir = szPath;
1713 * Hint: SHGetFolderPath is not available on MSVC 6 - without
1716 bRet = SHGetSpecialFolderPath(NULL, tszPath, CSIDL_PERSONAL,
1719 szPath = utf_16to8(tszPath);
1720 persdatafile_dir = szPath;
1733 * Returns the user's home directory on Win32.
1738 static const char *home = NULL;
1739 char *homedrive, *homepath;
1743 /* Return the cached value, if available */
1748 * XXX - should we use USERPROFILE anywhere in this process?
1749 * Is there a chance that it might be set but one or more of
1750 * HOMEDRIVE or HOMEPATH isn't set?
1752 homedrive = getenv_utf8("HOMEDRIVE");
1753 if (homedrive != NULL) {
1754 homepath = getenv_utf8("HOMEPATH");
1755 if (homepath != NULL) {
1757 * This is cached, so we don't need to worry about
1758 * allocating multiple ones of them.
1760 homestring = g_strdup_printf("%s%s", homedrive, homepath);
1763 * Trim off any trailing slash or backslash.
1765 lastsep = find_last_pathname_separator(homestring);
1766 if (lastsep != NULL && *(lastsep + 1) == '\0') {
1768 * Last separator is the last character
1769 * in the string. Nuke it.
1778 * Give up and use C:.
1788 * Construct the path name of a personal configuration file, given the
1791 * On Win32, if "for_writing" is FALSE, we check whether the file exists
1792 * and, if not, construct a path name relative to the ".wireshark"
1793 * subdirectory of the user's home directory, and check whether that
1794 * exists; if it does, we return that, so that configuration files
1795 * from earlier versions can be read.
1797 * The returned file name was g_malloc()'d so it must be g_free()d when the
1798 * caller is done with it.
1801 get_persconffile_path(const char *filename, gboolean from_profile)
1804 if (do_store_persconffiles && from_profile && !g_hash_table_lookup (profile_files, filename)) {
1805 /* Store filenames so we know which filenames belongs to a configuration profile */
1806 g_hash_table_insert (profile_files, g_strdup(filename), g_strdup(filename));
1810 path = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s",
1811 get_persconffile_dir(persconfprofile), filename);
1813 path = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s",
1814 get_persconffile_dir(NULL), filename);
1821 * process command line option belonging to the filesystem settings
1822 * (move this e.g. to main.c and have set_persconffile_dir() instead in this file?)
1825 filesystem_opt(int opt _U_, const char *optstr)
1829 colonp = strchr(optstr, ':');
1830 if (colonp == NULL) {
1838 * Skip over any white space (there probably won't be any, but
1839 * as we allow it in the preferences file, we might as well
1842 while (isspace((guchar)*p))
1846 * Put the colon back, so if our caller uses, in an
1847 * error message, the string they passed us, the message
1854 /* directory should be existing */
1855 /* XXX - is this a requirement? */
1856 if(test_for_directory(p) != EISDIR) {
1858 * Put the colon back, so if our caller uses, in an
1859 * error message, the string they passed us, the message
1866 if (strcmp(optstr,"persconf") == 0) {
1867 persconffile_dir = p;
1868 } else if (strcmp(optstr,"persdata") == 0) {
1869 persdatafile_dir = p;
1870 /* XXX - might need to add the temp file path */
1874 *colonp = ':'; /* put the colon back */
1879 * Construct the path name of a global configuration file, given the
1882 * The returned file name was g_malloc()'d so it must be g_free()d when the
1883 * caller is done with it.
1886 get_datafile_path(const char *filename)
1889 return g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", get_datafile_dir(), filename);
1892 /* Get the personal plugin dir */
1893 /* Return value is malloced so the caller should g_free() it. */
1895 get_plugins_pers_dir(void)
1897 return get_persconffile_path(PLUGINS_DIR_NAME, FALSE);
1902 deletefile(const char *path)
1904 return ws_unlink(path) == 0;
1908 * Construct and return the path name of a file in the
1909 * appropriate temporary file directory.
1911 char *get_tempfile_path(const char *filename)
1913 return g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s", g_get_tmp_dir(), filename);
1917 * Return an error message for UNIX-style errno indications on open or
1918 * create operations.
1921 file_open_error_message(int err, gboolean for_writing)
1924 static char errmsg_errno[1024+1];
1930 errmsg = "The path to the file \"%s\" doesn't exist.";
1932 errmsg = "The file \"%s\" doesn't exist.";
1937 errmsg = "You don't have permission to create or write to the file \"%s\".";
1939 errmsg = "You don't have permission to read the file \"%s\".";
1943 errmsg = "\"%s\" is a directory (folder), not a file.";
1947 errmsg = "The file \"%s\" could not be created because there is no space left on the file system.";
1952 errmsg = "The file \"%s\" could not be created because you are too close to, or over, your disk quota.";
1957 errmsg = "The file \"%s\" could not be created because an invalid filename was specified.";
1962 * The problem probably has nothing to do with how much RAM the
1963 * user has on their machine, so don't confuse them by saying
1964 * "memory". The problem is probably either virtual address
1965 * space or swap space.
1967 #if GLIB_SIZEOF_VOID_P == 4
1969 * ILP32; we probably ran out of virtual address space.
1971 #define ENOMEM_REASON "it can't be handled by a 32-bit application"
1974 * LP64 or LLP64; we probably ran out of swap space.
1978 * You need to make the pagefile bigger.
1980 #define ENOMEM_REASON "the pagefile is too small"
1981 #elif defined(__APPLE__)
1983 * dynamic_pager couldn't, or wouldn't, create more swap files.
1985 #define ENOMEM_REASON "your system ran out of swap file space"
1988 * Either you have a fixed swap partition or a fixed swap file,
1989 * and it needs to be made bigger.
1991 * This is UN*X, but it's not OS X, so we assume the user is
1994 #define ENOMEM_REASON "your system is out of swap space"
1996 #endif /* GLIB_SIZEOF_VOID_P == 4 */
1998 errmsg = "The file \"%s\" could not be created because " ENOMEM_REASON ".";
2000 errmsg = "The file \"%s\" could not be opened because " ENOMEM_REASON ".";
2004 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
2005 "The file \"%%s\" could not be %s: %s.",
2006 for_writing ? "created" : "opened",
2008 errmsg = errmsg_errno;
2015 * Return an error message for UNIX-style errno indications on write
2019 file_write_error_message(int err)
2022 static char errmsg_errno[1024+1];
2027 errmsg = "The file \"%s\" could not be saved because there is no space left on the file system.";
2032 errmsg = "The file \"%s\" could not be saved because you are too close to, or over, your disk quota.";
2037 g_snprintf(errmsg_errno, sizeof(errmsg_errno),
2038 "An error occurred while writing to the file \"%%s\": %s.",
2040 errmsg = errmsg_errno;
2048 file_exists(const char *fname)
2050 ws_statb64 file_stat;
2058 * This is a bit tricky on win32. The st_ino field is documented as:
2059 * "The inode, and therefore st_ino, has no meaning in the FAT, ..."
2060 * but it *is* set to zero if stat() returns without an error,
2061 * so this is working, but maybe not quite the way expected. ULFL
2063 file_stat.st_ino = 1; /* this will make things work if an error occurred */
2064 ws_stat64(fname, &file_stat);
2065 if (file_stat.st_ino == 0) {
2071 if (ws_stat64(fname, &file_stat) != 0 && errno == ENOENT) {
2080 * Check that the from file is not the same as to file
2081 * We do it here so we catch all cases ...
2082 * Unfortunately, the file requester gives us an absolute file
2083 * name and the read file name may be relative (if supplied on
2084 * the command line), so we can't just compare paths. From Joerg Mayer.
2087 files_identical(const char *fname1, const char *fname2)
2089 /* Two different implementations, because:
2091 * - _fullpath is not available on UN*X, so we can't get full
2092 * paths and compare them (which wouldn't work with hard links
2095 * - st_ino isn't filled in with a meaningful value on Windows.
2098 char full1[MAX_PATH], full2[MAX_PATH];
2101 * Get the absolute full paths of the file and compare them.
2102 * That won't work if you have hard links, but those aren't
2103 * much used on Windows, even though NTFS supports them.
2105 * XXX - will _fullpath work with UNC?
2107 if( _fullpath( full1, fname1, MAX_PATH ) == NULL ) {
2111 if( _fullpath( full2, fname2, MAX_PATH ) == NULL ) {
2115 if(strcmp(full1, full2) == 0) {
2121 ws_statb64 filestat1, filestat2;
2124 * Compare st_dev and st_ino.
2126 if (ws_stat64(fname1, &filestat1) == -1)
2127 return FALSE; /* can't get info about the first file */
2128 if (ws_stat64(fname2, &filestat2) == -1)
2129 return FALSE; /* can't get info about the second file */
2130 return (filestat1.st_dev == filestat2.st_dev &&
2131 filestat1.st_ino == filestat2.st_ino);
2136 * Copy a file in binary mode, for those operating systems that care about
2137 * such things. This should be OK for all files, even text files, as
2138 * we'll copy the raw bytes, and we don't look at the bytes as we copy
2141 * Returns TRUE on success, FALSE on failure. If a failure, it also
2142 * displays a simple dialog window with the error message.
2145 copy_file_binary_mode(const char *from_filename, const char *to_filename)
2147 int from_fd, to_fd, err;
2148 ssize_t nread, nwritten;
2151 /* Copy the raw bytes of the file. */
2152 from_fd = ws_open(from_filename, O_RDONLY | O_BINARY, 0000 /* no creation so don't matter */);
2154 report_open_failure(from_filename, errno, FALSE);
2158 /* Use open() instead of creat() so that we can pass the O_BINARY
2159 flag, which is relevant on Win32; it appears that "creat()"
2160 may open the file in text mode, not binary mode, but we want
2161 to copy the raw bytes of the file, so we need the output file
2162 to be open in binary mode. */
2163 to_fd = ws_open(to_filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
2165 report_open_failure(to_filename, errno, TRUE);
2170 #define FS_READ_SIZE 65536
2171 pd = (guint8 *)g_malloc(FS_READ_SIZE);
2172 while ((nread = ws_read(from_fd, pd, FS_READ_SIZE)) > 0) {
2173 nwritten = ws_write(to_fd, pd, nread);
2174 if (nwritten < nread) {
2178 err = WTAP_ERR_SHORT_WRITE;
2179 report_write_failure(to_filename, err);
2187 report_read_failure(from_filename, err);
2193 if (ws_close(to_fd) < 0) {
2194 report_write_failure(to_filename, errno);
2213 * indent-tabs-mode: nil
2216 * ex: set shiftwidth=4 tabstop=8 expandtab:
2217 * :indentSize=4:tabSize=8:noTabs=true: