2 * Filesystem utility routines
4 * $Id: filesystem.c,v 1.29 2004/01/24 02:01:43 guy Exp $
6 * Ethereal - Network traffic analyzer
7 * By Gerald Combs <gerald@ethereal.com>
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.
40 #ifdef HAVE_SYS_STAT_H
49 #include <direct.h> /* to declare "mkdir()" on Windows */
56 #include "filesystem.h"
59 * Given a pathname, return a pointer to the last pathname separator
60 * character in the pathname, or NULL if the pathname contains no
64 find_last_pathname_separator(char *path)
72 * We have to scan for '\' or '/'.
73 * Get to the end of the string.
75 separator = path + strlen(path); /* points to ending '\0' */
76 while (separator > path) {
78 if (c == '\\' || c == '/')
79 return separator; /* found it */
83 * OK, we didn't find any, so no directories - but there might
84 * be a drive letter....
86 return strchr(path, ':');
88 separator = strrchr(path, '/');
94 * Given a pathname, return the last component.
97 get_basename(char *path)
101 g_assert(path != NULL);
102 filename = find_last_pathname_separator(path);
103 if (filename == NULL) {
105 * There're no directories, drive letters, etc. in the
106 * name; the pathname *is* the file name.
111 * Skip past the pathname or drive letter separator.
119 * Given a pathname, return a string containing everything but the
120 * last component. NOTE: this overwrites the pathname handed into
124 get_dirname(char *path)
128 g_assert(path != NULL);
129 separator = find_last_pathname_separator(path);
130 if (separator == NULL) {
132 * There're no directories, drive letters, etc. in the
133 * name; there is no directory path to return.
139 * Get rid of the last pathname separator and the final file
145 * "path" now contains the pathname of the directory containing
146 * the file/directory to which it referred.
152 * Given a pathname, return:
154 * the errno, if an attempt to "stat()" the file fails;
156 * EISDIR, if the attempt succeeded and the file turned out
159 * 0, if the attempt succeeded and the file turned out not
164 * Visual C++ on Win32 systems doesn't define these. (Old UNIX systems don't
165 * define them either.)
167 * Visual C++ on Win32 systems doesn't define S_IFIFO, it defines _S_IFIFO.
170 #define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
173 #define S_IFIFO _S_IFIFO
176 #define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO)
179 #define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
183 test_for_directory(const char *path)
187 if (stat(path, &statb) < 0)
190 if (S_ISDIR(statb.st_mode))
197 test_for_fifo(const char *path)
201 if (stat(path, &statb) < 0)
204 if (S_ISFIFO(statb.st_mode))
211 * Get the directory in which Ethereal's global configuration and data
214 * XXX - if we ever make libethereal a real library, used by multiple
215 * applications (more than just Tethereal and versions of Ethereal with
216 * various UIs), should the configuration files belong to the library
217 * (and be shared by all those applications) or to the applications?
219 * If they belong to the library, that could be done on UNIX by the
220 * configure script, but it's trickier on Windows, as you can't just
221 * use the pathname of the executable.
223 * If they belong to the application, that could be done on Windows
224 * by using the pathname of the executable, but we'd have to have it
225 * passed in as an argument, in some call, on UNIX.
227 * Note that some of those configuration files might be used by code in
228 * libethereal, some of them might be used by dissectors (would they
229 * belong to libethereal, the application, or a separate library?),
230 * and some of them might be used by other code (the Ethereal preferences
231 * file includes resolver preferences that control the behavior of code
232 * in libethereal, dissector preferences, and UI preferences, for
236 get_datafile_dir(void)
239 char prog_pathname[_MAX_PATH+2];
241 size_t datafile_dir_len;
242 static char *datafile_dir;
245 * Have we already gotten the pathname?
246 * If so, just return it.
248 if (datafile_dir != NULL)
253 * Start out by assuming it's the default installation directory.
255 datafile_dir = "C:\\Program Files\\Ethereal\\";
258 * Now we attempt to get the full pathname of the currently running
259 * program, under the assumption that we're running an installed
260 * version of the program. If we fail, we don't change "datafile_dir",
261 * and thus end up using the default.
263 * XXX - does NSIS put the installation directory into
264 * "\HKEY_LOCAL_MACHINE\SOFTWARE\Ethereal\InstallDir"?
265 * If so, perhaps we should read that from the registry,
268 if (GetModuleFileName(NULL, prog_pathname, sizeof prog_pathname) != 0) {
270 * If the program is an installed version, the full pathname
271 * includes the pathname of the directory in which it was
272 * installed; get that directory's pathname, and construct
273 * from it the pathname of the directory in which the
274 * plugins were installed.
276 * First, find the last "\\" in the directory, as that
277 * marks the end of the directory pathname.
279 * XXX - Can the pathname be something such as
280 * "C:ethereal.exe"? Or is it always a full pathname
281 * beginning with "\\" after the drive letter?
283 dir_end = strrchr(prog_pathname, '\\');
284 if (dir_end != NULL) {
286 * Found it - now figure out how long the datafile
287 * directory pathname will be.
289 datafile_dir_len = (dir_end - prog_pathname);
292 * Allocate a buffer for the plugin directory
293 * pathname, and construct it.
295 datafile_dir = g_malloc(datafile_dir_len + 1);
296 strncpy(datafile_dir, prog_pathname, datafile_dir_len);
297 datafile_dir[datafile_dir_len] = '\0';
303 * Just use DATAFILE_DIR, as that's what the configure script
311 * Get the directory in which files that, at least on UNIX, are
312 * system files (such as "/etc/ethers") are stored; on Windows,
313 * there's no "/etc" directory, so we get them from the Ethereal
314 * global configuration and data file directory.
317 get_systemfile_dir(void)
320 return get_datafile_dir();
327 * Name of directory, under the user's home directory, in which
328 * personal configuration files are stored.
331 #define PF_DIR "Ethereal"
334 * XXX - should this be ".libepan"? For backwards-compatibility, I'll keep
335 * it ".ethereal" for now.
337 #define PF_DIR ".ethereal"
341 * Get the directory in which personal configuration files reside;
342 * in UNIX-compatible systems, it's ".ethereal", under the user's home
343 * directory, and on Windows systems, it's "Ethereal", under %APPDATA%
344 * or, if %APPDATA% isn't set, it's "%USERPROFILE%\Application Data"
345 * (which is what %APPDATA% normally is on Windows 2000).
348 get_persconffile_dir(void)
352 char *userprofiledir;
357 static char *pf_dir = NULL;
359 /* Return the cached value, if available */
365 * Use %APPDATA% or %USERPROFILE%, so that configuration files are
366 * stored in the user profile, rather than in the home directory.
367 * The Windows convention is to store configuration information
368 * in the user profile, and doing so means you can use
369 * Ethereal even if the home directory is an inaccessible
372 appdatadir = getenv("APPDATA");
373 if (appdatadir != NULL) {
375 * Concatenate %APPDATA% with "\Ethereal".
377 pf_dir = g_malloc(strlen(appdatadir) + strlen(PF_DIR) + 2);
378 sprintf(pf_dir, "%s" G_DIR_SEPARATOR_S "%s", appdatadir,
382 * OK, %APPDATA% wasn't set, so use
383 * %USERPROFILE%\Application Data.
385 userprofiledir = getenv("USERPROFILE");
386 if (userprofiledir != NULL) {
387 pf_dir = g_malloc(strlen(userprofiledir) +
388 strlen("Application Data") + strlen(PF_DIR) + 3);
390 "%s" G_DIR_SEPARATOR_S "Application Data" G_DIR_SEPARATOR_S "%s",
391 userprofiledir, PF_DIR);
394 * Give up and use "C:".
396 pf_dir = g_malloc(strlen("C:") + strlen(PF_DIR) + 2);
397 sprintf(pf_dir, "C:" G_DIR_SEPARATOR_S "%s", PF_DIR);
402 * If $HOME is set, use that.
404 homedir = getenv("HOME");
405 if (homedir == NULL) {
407 * Get their home directory from the password file.
408 * If we can't even find a password file entry for them,
411 pwd = getpwuid(getuid());
414 * This is cached, so we don't need to worry
415 * about allocating multiple ones of them.
417 homedir = g_strdup(pwd->pw_dir);
421 pf_dir = g_malloc(strlen(homedir) + strlen(PF_DIR) + 2);
422 sprintf(pf_dir, "%s" G_DIR_SEPARATOR_S "%s", homedir, PF_DIR);
429 * Create the directory that holds personal configuration files, if
430 * necessary. If we attempted to create it, and failed, return -1 and
431 * set "*pf_dir_path_return" to the pathname of the directory we failed
432 * to create (it's g_mallocated, so our caller should free it); otherwise,
436 create_persconffile_dir(char **pf_dir_path_return)
438 const char *pf_dir_path;
440 char *pf_dir_path_copy, *pf_dir_parent_path;
441 size_t pf_dir_parent_path_len;
446 pf_dir_path = get_persconffile_dir();
447 if (stat(pf_dir_path, &s_buf) != 0 && errno == ENOENT) {
450 * Does the parent directory of that directory
451 * exist? %APPDATA% may not exist even though
452 * %USERPROFILE% does.
454 * We check for the existence of the directory
455 * by first checking whether the parent directory
456 * is just a drive letter and, if it's not, by
457 * doing a "stat()" on it. If it's a drive letter,
458 * or if the "stat()" succeeds, we assume it exists.
460 pf_dir_path_copy = g_strdup(pf_dir_path);
461 pf_dir_parent_path = get_dirname(pf_dir_path_copy);
462 pf_dir_parent_path_len = strlen(pf_dir_parent_path);
463 if (pf_dir_parent_path_len > 0
464 && pf_dir_parent_path[pf_dir_parent_path_len - 1] != ':'
465 && stat(pf_dir_parent_path, &s_buf) != 0) {
467 * No, it doesn't exist - make it first.
469 ret = mkdir(pf_dir_parent_path);
471 *pf_dir_path_return = pf_dir_parent_path;
475 g_free(pf_dir_path_copy);
476 ret = mkdir(pf_dir_path);
478 ret = mkdir(pf_dir_path, 0755);
482 * Something with that pathname exists; if it's not
483 * a directory, we'll get an error if we try to put
484 * something in it, so we don't fail here, we wait
485 * for that attempt fo fail.
490 *pf_dir_path_return = g_strdup(pf_dir_path);
496 * Returns the user's home directory on Win32.
501 static const char *home = NULL;
502 char *homedrive, *homepath;
506 /* Return the cached value, if available */
511 * XXX - should we use USERPROFILE anywhere in this process?
512 * Is there a chance that it might be set but one or more of
513 * HOMEDRIVE or HOMEPATH isn't set?
515 homedrive = getenv("HOMEDRIVE");
516 if (homedrive != NULL) {
517 homepath = getenv("HOMEPATH");
518 if (homepath != NULL) {
520 * This is cached, so we don't need to worry about
521 * allocating multiple ones of them.
524 g_malloc(strlen(homedrive) + strlen(homepath) + 1);
525 strcpy(homestring, homedrive);
526 strcat(homestring, homepath);
529 * Trim off any trailing slash or backslash.
531 lastsep = find_last_pathname_separator(homestring);
532 if (lastsep != NULL && *(lastsep + 1) == '\0') {
534 * Last separator is the last character
535 * in the string. Nuke it.
544 * Give up and use C:.
554 * Construct the path name of a personal configuration file, given the
557 * On Win32, if "for_writing" is FALSE, we check whether the file exists
558 * and, if not, construct a path name relative to the ".ethereal"
559 * subdirectory of the user's home directory, and check whether that
560 * exists; if it does, we return that, so that configuration files
561 * from earlier versions can be read.
564 get_persconffile_path(const char *filename, gboolean for_writing
576 path = (gchar *) g_malloc(strlen(get_persconffile_dir()) +
577 strlen(filename) + 2);
578 sprintf(path, "%s" G_DIR_SEPARATOR_S "%s", get_persconffile_dir(),
582 if (stat(path, &s_buf) != 0 && errno == ENOENT) {
584 * OK, it's not in the personal configuration file
585 * directory; is it in the ".ethereal" subdirectory
586 * of their home directory?
588 old_path = (gchar *) g_malloc(strlen(get_home_dir()) +
589 strlen(".ethereal") + strlen(filename) + 3);
591 "%s" G_DIR_SEPARATOR_S ".ethereal" G_DIR_SEPARATOR_S "%s",
592 get_home_dir(), filename);
593 if (stat(old_path, &s_buf) == 0) {
595 * OK, it exists; return it instead.
608 * Construct the path name of a global configuration file, given the
612 get_datafile_path(const char *filename)
616 path = (gchar *) g_malloc(strlen(get_datafile_dir()) +
617 strlen(filename) + 2);
618 sprintf(path, "%s" G_DIR_SEPARATOR_S "%s", get_datafile_dir(),
626 deletefile(const char *path)
628 return unlink(path) == 0;
632 * Construct and return the path name of a file in the
633 * appropriate temporary file directory.
635 char *get_tempfile_path(const char *filename)
639 path = (gchar *) g_malloc(strlen(g_get_tmp_dir()) +
640 strlen(filename) + 2);
641 sprintf(path, "%s" G_DIR_SEPARATOR_S "%s", g_get_tmp_dir(), filename);
647 * Return an error message for UNIX-style errno indications on open or
651 file_open_error_message(int err, gboolean for_writing)
654 static char errmsg_errno[1024+1];
660 errmsg = "The path to the file \"%s\" does not exist.";
662 errmsg = "The file \"%s\" does not exist.";
667 errmsg = "You do not have permission to create or write to the file \"%s\".";
669 errmsg = "You do not have permission to read the file \"%s\".";
673 errmsg = "\"%s\" is a directory (folder), not a file.";
677 errmsg = "The file \"%s\" could not be created because there is no space left on the file system.";
682 errmsg = "The file \"%s\" could not be created because you are too close to, or over, your disk quota.";
687 snprintf(errmsg_errno, sizeof(errmsg_errno),
688 "The file \"%%s\" could not be %s: %s.",
689 for_writing ? "created" : "opened",
691 errmsg = errmsg_errno;
698 * Return an error message for UNIX-style errno indications on write
702 file_write_error_message(int err)
705 static char errmsg_errno[1024+1];
710 errmsg = "The file \"%s\" could not be saved because there is no space left on the file system.";
715 errmsg = "The file \"%s\" could not be saved because you are too close to, or over, your disk quota.";
720 snprintf(errmsg_errno, sizeof(errmsg_errno),
721 "An error occurred while writing to the file \"%%s\": %s.",
723 errmsg = errmsg_errno;