add functions file_exists and file_identical to filesystem.c (coming from file.c)

svn path=/trunk/; revision=14057
This commit is contained in:
Ulf Lamping 2005-04-12 21:12:19 +00:00
parent 4929e662b8
commit 6e38159c25
4 changed files with 96 additions and 18 deletions

View File

@ -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
}

View File

@ -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 */

View File

@ -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

21
file.c
View File

@ -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.",