4

I'm running CentOS Linux.

I create a directory as follows using a proprietary filesystem:

$ mkdir fooDir

First, I check the inode value using "ls -ldi":

$ ls -ldi
total 4
9223372036854783200 drwxrwxrwx 2 root root 4096 Jan  6 20:58 fooDir

Then, I confirm same inode is being reported by 'stat':

$ stat /fooDir
  File: `/fooDir'
  Size: 4096        Blocks: 8          IO Block: 4096   directory
Device: 14h/20d Inode: 9223372036854783200  Links: 2
Access: (0777/drwxrwxrwx)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2016-01-06 20:58:13.000000000 +0000
Modify: 2016-01-06 20:58:13.000000000 +0000
Change: 2016-01-06 20:58:23.000000000 +0000

But then I switch over to run in python's interactive prompt and run om.stat against the directory:

$ python
Python 2.6.6 (r266:84292, Jun 18 2012, 14:18:47) 
[GCC 4.4.6 20110731 (Red Hat 4.4.6-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> os.stat("/fooDir")
posix.stat_result(st_mode=16895, st_ino=-9223372036854768416, st_dev=20L, st_nlink=2, st_uid=0, st_gid=0, st_size=4096, st_atime=1452113893, st_mtime=1452113893, st_ctime=1452113903)
>>> 

The inode value being returned by Python's om.stat does not match the value as reported by stat command.

The filesystem has assigned the inode value of 9223372036854783200 to the directory 'fooDir', however Python's om.stat is returning "-9223372036854768416".

I can handle that its treating it as a signed value and hence the negative sign, but I'm confused about why its a completely different value.

Any ideas on what's happening here? Why Python's os stat returning the wrong inode value? (or wrong according to 'stat' command)

jersey bean
  • 3,321
  • 4
  • 28
  • 43
  • You say you're using a proprietary filesystem. Perhaps that's the problem? That inode number looks awfully big. – John Gordon Jan 06 '16 at 21:47
  • Those numbers are equivalent mod 2**64 – kalhartt Jan 06 '16 at 21:48
  • "I can handle that its treating it as a signed value and hence the negative sign,". Converting from signed to unsigned is not just adding/removing the sign prefix. Those two numbers are the same if you do the correct conversion. Have a read of some of these resources: [What is “2's Complement”?](https://stackoverflow.com/questions/1049722/what-is-2s-complement) and [wikipedia Two's complement page](https://en.wikipedia.org/wiki/Two's_complement). – kaylum Jan 06 '16 at 21:56
  • This happens in python 2.x because in python 3.8.6 it works properly – somenxavier Nov 15 '20 at 21:41

2 Answers2

5

I figured out the inode number was intended to be an unsigned long, but om.stat was interpreting as a negative number because the MSB was set.

So now that I know its supposed to be an unsigned, I can solve this issue as follows:

import ctypes
ctypes.c_ulong(os.stat(<dir here>).st_ino).value

An easier solution, without other dependencies:

if inode < 0 :
    inode += 1 << 64
Massimo
  • 3,171
  • 3
  • 28
  • 41
jersey bean
  • 3,321
  • 4
  • 28
  • 43
-1

Use commands modules, it will return the same result.

   import commands
   commands.getstatusoutput('stat /fooDir')
Hendri Tobing
  • 351
  • 5
  • 14
  • You are probably right, but that does not answer the question because in that case you would simply bypass python 'os.stat()' command – arainone Jan 06 '16 at 23:28
  • Thanks for your response. Yes, that's probably a good workaround. But still doesn't answer the question. – jersey bean Jan 21 '16 at 17:15