Recent

Author Topic: [SOLVED] Is the Same Disk/Partition?  (Read 2534 times)

ϻαϻɾΣɀО

  • Jr. Member
  • **
  • Posts: 54
  • MaMrEzO
[SOLVED] Is the Same Disk/Partition?
« on: May 30, 2016, 08:31:22 pm »
Hi
Is there any standard way to determine two path points to same storage Drive/Disk/Share location?
In linux I found this:
Code: Pascal  [Select][+][-]
  1. var
  2.   sInfo1, sInfo2: TStatfs;
  3. begin
  4.   if (fpStatFS(UTF8ToSys(PathA), @sInfo1) <> - 1) and (fpStatFS(UTF8ToSys(PathB), @sInfo2) <> - 1) then
  5.   begin
  6.     Result := (sInfo1.fsid[0] = sInfo2.fsid[0]) and (sInfo1.fsid[1] = sInfo2.fsid[1]);
  7.   end
  8.   else
  9.     Exit(False);
  10. end
  11.  

Its works for Paths like these:
/home/Pascal/A  <-- /dev/sda1/Pascal/A
/home/Pascal/B  <-- /dev/sdb3/ Mounted here

Also I dont know how to check if these are sym/hard links!

Note for f_fsid:
The general idea is that f_fsid contains some random stuff such that
       the pair (f_fsid,ino) uniquely determines a file
.  Some operating
       systems use (a variation on) the device number, or the device number
       combined with the filesystem type.  Several operating systems
       restrict giving out the f_fsid field to the superuser only (and zero
       it for unprivileged users), because this field is used in the
       filehandle of the filesystem when NFS-exported, and giving it out is
       a security concern.

       Under some operating systems, the fsid can be used as the second
       argument to the sysfs(2) system call.

Under Ubuntu 16.04 64-bit it seems work correctly for none linked folders!

But on windows even for
  D:\A   <-- Local folder on D:
  D:\B   <-- Next partition  mounted hre

Or even mapped Drive
  \\192.168.1.152\SharedFolder  <-- Just a shared folder
  E:\  <-- \\192.168.1.152\SharedFolder Mapped here on E:\

On windows I found something like GetFileInformationByHandle :
  You can compare the VolumeSerialNumber and FileIndex members returned in the BY_HANDLE_FILE_INFORMATION structure to determine if two paths map to the same target; for example, you can compare two file paths and determine if they map to the same directory. [From MSDN]

Until last night I have an stupid idea to compare Size and Available free space to detect it!
But tow Hard disks with same size and partitioned as a just one partition presents same Size,
Or tow path that correctly located under same Partition and while  we have heavy changes on it reports different Available remaining space even !
However I dont test it(Compare of Size and remaining space) of paths!
« Last Edit: May 30, 2016, 09:43:04 pm by ϻαϻɾΣɀО »
Debio-Sql is a new brand of GUI Database tool for the Firebird RDBMS.
http://debio-sql.ariaian.com/

ϻαϻɾΣɀО

  • Jr. Member
  • **
  • Posts: 54
  • MaMrEzO
Re: Is the Same Disk/Partition?
« Reply #1 on: May 30, 2016, 09:39:46 pm »
Finally I have the solution:

Unicode and tested under Ubuntu 16.04 64-bit and Win7 64-bit

Code: Pascal  [Select][+][-]
  1. function IsSameDevice(PathA, PathB: TDFileName): Boolean;
  2. {$IFDEF MSWINDOWS}
  3. var
  4.   AHandle, BHandle: THANDLE;
  5.   AInfo, BInfo: TByHandleFileInformation;
  6. begin
  7.   AHandle := CreateFileW(PWideChar(UTF8Decode((PathA))), GENERIC_READ, FILE_SHARE_READ,
  8.         Nil, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0);
  9.   BHandle := CreateFileW(PWideChar(UTF8Decode((PathB))), GENERIC_READ, FILE_SHARE_READ,
  10.         Nil, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0);
  11.  
  12.   try
  13.     Result := (AHandle <> 0) and (BHandle <> 0) and
  14.       GetFileInformationByHandle(AHandle, AInfo) and
  15.       GetFileInformationByHandle(BHandle, BInfo) and
  16.       (AInfo.dwVolumeSerialNumber = BInfo.dwVolumeSerialNumber) and
  17.       (AInfo.nFileIndexHigh = BInfo.nFileIndexHigh);
  18.   finally
  19.     if AHandle <> 0 then
  20.       FileClose(AHandle);
  21.     if BHandle <> 0 then
  22.       FileClose(BHandle);
  23.   end;
  24. {$ENDIF}
  25. {$IFDEF UNIX}
  26. var
  27.   AInfo, BInfo: TStatfs;
  28. begin
  29.   Result :=
  30.     (fpStatFS(UTF8ToSys(PathA), @AInfo) <> - 1) and
  31.     (fpStatFS(UTF8ToSys(PathB), @BInfo) <> - 1) and
  32.     (AInfo.fsid[0] = BInfo.fsid[0]) and
  33.     (AInfo.fsid[1] = BInfo.fsid[1]);
  34. {$ENDIF}
  35. end;
  36.  
Debio-Sql is a new brand of GUI Database tool for the Firebird RDBMS.
http://debio-sql.ariaian.com/

ASerge

  • Hero Member
  • *****
  • Posts: 2510
Re: [SOLVED] Is the Same Disk/Partition?
« Reply #2 on: July 11, 2016, 04:30:10 pm »
Mistake: windows CreateFile return INVALID_HANLDE_VALUE when fail. It's not equal zero!

 

TinyPortal © 2005-2018