Basically, the GnuWin32 implementation of stat uses as inode number the FileIndex from the BY_HANDLE_FILE_INFORMATION structure returned by the Win32 API function GetFileInformationByHandle (http://msdn.microsoft.com/library/en-us/fileio/fs/getfileinformationbyhandle.asp). The FileIndex is a 64-bit number that indicates the position of the file in the Master File Table (MFT). It is stable between successive starts of the system, provided the MFT does not overflow (and has to be rebuilt). On WinNT systems (NT, 2K, XP) the FileIndex is also returned for directories, on Win9x (95, 98, ME) it returns zero for directories. It is not stable for files on network drives; successive calls to GetFileInformationByHandle return different values. For directories on 9x and network files, the GnuWin32 stat uses a hashed value of the full path of the file.
The FileIndex consists of two parts: the low 48 bits are the file record number and contain the actual index in the MFT; the high 16 bits are the socalled sequence number: each time the entry in the MFT is reused for another file, the sequence number is increased by one.
The GnuWin32 implementation of stat can be found in io/hxstat64.c and associated files in the LibGw32C package. It uses a series of functions in order to deal with large files (> 4GB), and the problems described above. If these adaptations are not relevant, you had probably best use the standard stat function from msvcrt.dll and separately create an inode number as follows:


#include <sys/stat.h>
#include <io.h>
#include <stdint.h>
#include <windows.h>
#define LODWORD(l) ((DWORD)((DWORDLONG)(l)))
#define HIDWORD(l) ((DWORD)(((DWORDLONG)(l)>>32)&0xFFFFFFFF))
#define MAKEDWORDLONG(a,b) ((DWORDLONG)(((DWORD)(a))|(((DWORDLONG)((DWORD)(b)))<<32)))


#define INOSIZE  (8*sizeof(ino_t))
#define SEQNUMSIZE (16)

ino_t getino (char *path)
{
BY_HANDLE_FILE_INFORMATION FileInformation;
HANDLE hFile;
uint64_t ino64, refnum;
ino_t ino;
/* obtain handle to file "path"
   FILE_FLAG_BACKUP_SEMANTICS is used to open directories
*/
if (!path || !*path) /* path = NULL */
return 0;
if (access (path, F_OK)) /* path does not exist */
return -1;
hFile = CreateFile (path, 0, 0, NULL, OPEN_EXISTING,
   FILE_FLAG_BACKUP_SEMANTICS | FILE_ATTRIBUTE_READONLY,
   NULL);
ZeroMemory (&FileInformation, sizeof(FileInformation));
GetFileInformationByHandle (hFile, &FileInformation);
ino64 = (uint64_t) MAKEDWORDLONG (
  FileInformation.nFileIndexLow, FileInformation.nFileIndexHigh);
refnum = ino64 & ((~(0ULL)) >> SEQNUMSIZE);  /* remove sequence number */
/* transform 64-bits ino into 16-bits by hashing */
ino = (ino_t) (
  ( (LODWORD(refnum)) ^ ((LODWORD(refnum)) >> INOSIZE) )
 ^
  ( (HIDWORD(refnum)) ^ ((HIDWORD(refnum)) >> INOSIZE) )
 );
CloseHandle (hFile);
return ino;
}

An inode for fstat can be implemented similarly, by obtaining the handle from the file descriptor:

/* obtain handle to file descriptor "fd" */
hFile = _get_osfhandle (fd);


Kees Zeelenberg

----- Original Message ----- From: "Torsten Kurbad" <[EMAIL PROTECTED]>
To: <gnuwin32-users@lists.sourceforge.net>
Cc: <[EMAIL PROTECTED]>; <[EMAIL PROTECTED]>
Sent: Thursday, April 14, 2005 5:55 PM
Subject: [GnuWin32-Users] How does coreutils/stat "calculate" an inode on win32 filesystems?



Hello,

we are having a weird problem here:
While developing a portable python application (runs on Linux, MacOS X, and
Win32) we came across the fact, that the MS Visual C libraries always return
"0" as inode value for statbuf->st_ino (and so does python). Nevertheless,
the "stat" command of the win32 coreutils returns a value != 0 for the inode
number and behaves exactly like stat on Posix-Systems. We'd need exactly
that behavior for our project, but we are not quite geniuses in
C-programming... ;o)


Thus, can someone give a hint, where to find the appropriate function in
coreutils that returns a valid inode number?

Thanks in advance!
Best regards,
Torsten



-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now.
http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click
_______________________________________________
GnuWin32-Users mailing list
GnuWin32-Users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/gnuwin32-users






------------------------------------------------------- SF email is sponsored by - The IT Product Guide Read honest & candid reviews on hundreds of IT Products from real users. Discover which products truly live up to the hype. Start reading now. http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click _______________________________________________ GnuWin32-Users mailing list GnuWin32-Users@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/gnuwin32-users

Reply via email to