aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--epan/filesystem.c81
-rw-r--r--epan/filesystem.h10
-rw-r--r--epan/libethereal.def2
-rw-r--r--file.c21
4 files changed, 96 insertions, 18 deletions
diff --git a/epan/filesystem.c b/epan/filesystem.c
index 212f4a5f6c..40337f8201 100644
--- a/epan/filesystem.c
+++ b/epan/filesystem.c
@@ -726,3 +726,84 @@ file_write_error_message(int err)
return errmsg;
}
+
+gboolean
+file_exists(const char *fname)
+{
+ struct stat file_stat;
+
+
+ /*
+ * This is a bit tricky on win32. The st_ino field is documented as:
+ * "The inode, and therefore st_ino, has no meaning in the FAT, ..."
+ * but it *is* set to zero if stat() returns without an error,
+ * so this is working, but maybe not quite the way expected. ULFL
+ */
+ file_stat.st_ino = 1; /* this will make things work if an error occured */
+ stat(fname, &file_stat);
+ if (file_stat.st_ino == 0) {
+ return TRUE;
+ } else {
+ return FALSE;
+ }
+
+}
+
+
+gboolean
+files_identical(const char *fname1, const char *fname2)
+{
+ /* Two different implementations, because:
+ * - _fullpath is not available on unix
+ * - the stat inode will not work as expected on Win32, so two different implementations.
+ *
+ * XXX - will _fullpath work with UNC?
+ */
+#ifdef _WIN32
+ char full1[MAX_PATH], full2[MAX_PATH];
+
+
+ if( _fullpath( full1, fname1, MAX_PATH ) == NULL ) {
+ return FALSE;
+ }
+
+ if( _fullpath( full2, fname2, MAX_PATH ) == NULL ) {
+ return FALSE;
+ }
+
+ if(strcmp(full1, full2) == 0) {
+ return TRUE;
+ } else {
+ return FALSE;
+ }
+#else
+ struct stat infile, outfile;
+ save_callback_args_t callback_args;
+
+ cf_callback_invoke(cf_cb_file_safe_started, (gpointer) fname);
+
+ /*
+ * Check that the from file is not the same as to file
+ * We do it here so we catch all cases ...
+ * Unfortunately, the file requester gives us an absolute file
+ * name and the read file name may be relative (if supplied on
+ * the command line). From Joerg Mayer.
+ *
+ * This is a bit tricky on win32. The st_ino field is documented as:
+ * "The inode, and therefore st_ino, has no meaning in the FAT, ..."
+ * but it *is* set to zero if stat() returns without an error,
+ * so this is not working, as it only checks if both files existing. ULFL
+ */
+ infile.st_ino = 1; /* These prevent us from getting equality */
+ outfile.st_ino = 2; /* If one or other of the files is not accessible */
+ stat(cf->filename, &infile);
+ stat(fname, &outfile);
+ if (infile.st_ino == outfile.st_ino) {
+ return TRUE;
+ } else {
+ return FALSE;
+ }
+
+#endif
+}
+
diff --git a/epan/filesystem.h b/epan/filesystem.h
index d965c26300..534cfe384a 100644
--- a/epan/filesystem.h
+++ b/epan/filesystem.h
@@ -127,4 +127,14 @@ char *file_open_error_message(int err, gboolean for_writing);
*/
char *file_write_error_message(int err);
+/*
+ * Check, if file is existing.
+ */
+extern gboolean file_exists(const char *fname);
+
+/*
+ * Check, if two filenames are identical (with absolute and relative paths).
+ */
+extern gboolean files_identical(const char *fname1, const char *fname2);
+
#endif /* FILESYSTEM_H */
diff --git a/epan/libethereal.def b/epan/libethereal.def
index 23cb0f37d9..04e7a13e8b 100644
--- a/epan/libethereal.def
+++ b/epan/libethereal.def
@@ -204,6 +204,8 @@ FacilityReason_vals DATA
fc_fc4_val DATA
file_open_error_message
file_write_error_message
+file_exists
+files_identical
find_conversation
find_dissector
find_dissector_table
diff --git a/file.c b/file.c
index 4beb1f2065..4fec53463f 100644
--- a/file.c
+++ b/file.c
@@ -3074,28 +3074,13 @@ cf_save(capture_file *cf, const char *fname, packet_range_t *range, guint save_f
int err;
gboolean do_copy;
wtap_dumper *pdh;
- struct stat infile, outfile;
save_callback_args_t callback_args;
cf_callback_invoke(cf_cb_file_safe_started, (gpointer) fname);
- /*
- * Check that the from file is not the same as to file
- * We do it here so we catch all cases ...
- * Unfortunately, the file requester gives us an absolute file
- * name and the read file name may be relative (if supplied on
- * the command line). From Joerg Mayer.
- *
- * This is a bit tricky on win32. The st_ino field is documented as:
- * "The inode, and therefore st_ino, has no meaning in the FAT, ..."
- * but it *is* set to zero if stat() returns without an error,
- * so this is working, but maybe not quite the way expected. ULFL
- */
- infile.st_ino = 1; /* These prevent us from getting equality */
- outfile.st_ino = 2; /* If one or other of the files is not accessible */
- stat(cf->filename, &infile);
- stat(fname, &outfile);
- if (infile.st_ino == outfile.st_ino) {
+ /* don't write over an existing file. */
+ /* this should've been already checked by our caller, just to be sure... */
+ if (file_exists(fname)) {
simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
"%sCapture file: \"%s\" already exists!%s\n\n"
"Please choose a different filename.",