comparison liboctave/system/lo-sysdep.cc @ 32086:3c608abd55f5

Move "same_file" from liboctinterp to liboctave (bug #63803). * libinterp/corefcn/utils.cc, utils.h (same_file): Deprecate function. * libinterp/corefcn/sysdep.cc, sysdep.h (same_file_internal): Remove function. * liboctave/system/lo-sysdep.cc, lo-sysdep.h (same_file): Move function from liboctinterp to liboctave. * libgui/src/m-editor/file-editor.cc (file_editor::find_tab_widget), libinterp/corefcn/fcn-info.cc (file_editor::find_tab_widget), libinterp/corefcn/interpreter.cc (interpreter::execute_startup_files), libinterp/corefcn/load-path.cc (load_path::append): Use function in updated namespace.
author Markus Mützel <markus.muetzel@gmx.de>
date Sat, 06 May 2023 17:48:27 +0200
parents ed3a18fe328a
children 85255746fc21
comparison
equal deleted inserted replaced
32085:cf03230c0363 32086:3c608abd55f5
424 424
425 if (! fs) 425 if (! fs)
426 msg = fs.error (); 426 msg = fs.error ();
427 427
428 return (fs && fs.is_dir ()); 428 return (fs && fs.is_dir ());
429
430 #endif
431 }
432
433 // Return TRUE if FILE1 and FILE2 refer to the same (physical) file.
434
435 bool same_file (const std::string& file1, const std::string& file2)
436 {
437 #if defined (OCTAVE_USE_WINDOWS_API)
438
439 // FIXME: When Octave switches to C++17, consider replacing this function
440 // by https://en.cppreference.com/w/cpp/filesystem/equivalent.
441
442 bool retval = false;
443
444 std::wstring file1w = sys::u8_to_wstring (file1);
445 std::wstring file2w = sys::u8_to_wstring (file2);
446 const wchar_t *f1 = file1w.c_str ();
447 const wchar_t *f2 = file2w.c_str ();
448
449 bool f1_is_dir = GetFileAttributesW (f1) & FILE_ATTRIBUTE_DIRECTORY;
450 bool f2_is_dir = GetFileAttributesW (f2) & FILE_ATTRIBUTE_DIRECTORY;
451
452 // Windows native code
453 // Reference: http://msdn2.microsoft.com/en-us/library/aa363788.aspx
454
455 DWORD share = FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE;
456
457 HANDLE hfile1
458 = CreateFileW (f1, 0, share, 0, OPEN_EXISTING,
459 f1_is_dir ? FILE_FLAG_BACKUP_SEMANTICS : 0, 0);
460
461 if (hfile1 != INVALID_HANDLE_VALUE)
462 {
463 HANDLE hfile2
464 = CreateFileW (f2, 0, share, 0, OPEN_EXISTING,
465 f2_is_dir ? FILE_FLAG_BACKUP_SEMANTICS : 0, 0);
466
467 if (hfile2 != INVALID_HANDLE_VALUE)
468 {
469 BY_HANDLE_FILE_INFORMATION hfi1;
470 BY_HANDLE_FILE_INFORMATION hfi2;
471
472 if (GetFileInformationByHandle (hfile1, &hfi1)
473 && GetFileInformationByHandle (hfile2, &hfi2))
474 {
475 retval = (hfi1.dwVolumeSerialNumber == hfi2.dwVolumeSerialNumber
476 && hfi1.nFileIndexHigh == hfi2.nFileIndexHigh
477 && hfi1.nFileIndexLow == hfi2.nFileIndexLow
478 && hfi1.nFileSizeHigh == hfi2.nFileSizeHigh
479 && hfi1.nFileSizeLow == hfi2.nFileSizeLow
480 && hfi1.ftLastWriteTime.dwLowDateTime
481 == hfi2.ftLastWriteTime.dwLowDateTime
482 && hfi1.ftLastWriteTime.dwHighDateTime
483 == hfi2.ftLastWriteTime.dwHighDateTime);
484 }
485
486 CloseHandle (hfile2);
487 }
488
489 CloseHandle (hfile1);
490 }
491
492 return retval;
493
494 #else
495
496 // POSIX Code
497
498 sys::file_stat fs_file1 (file1);
499 sys::file_stat fs_file2 (file2);
500
501 return (fs_file1 && fs_file2
502 && fs_file1.ino () == fs_file2.ino ()
503 && fs_file1.dev () == fs_file2.dev ());
429 504
430 #endif 505 #endif
431 } 506 }
432 507
433 std::FILE * 508 std::FILE *