【问题标题】:Python's os stat is returning wrong inode valuePython os stat 返回错误的 inode 值
【发布时间】:2016-04-11 03:26:22
【问题描述】:

我正在运行 CentOS Linux。

我使用专有文件系统创建如下目录:

$ mkdir fooDir

首先,我使用“ls -ldi”检查 inode 值:

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

然后,我确认 'stat' 报告了相同的 inode:

$ 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

但随后我切换到在 python 的交互式提示中运行并针对目录运行 om.stat:

$ 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)
>>> 

Python 的 om.stat 返回的 inode 值与 stat 命令报告的值不匹配。

文件系统已将 inode 值 9223372036854783200 分配给目录“fooDir”,但 Python 的 om.stat 返回“-9223372036854768416”。

我可以将其视为有符号值并因此视为负号,但我很困惑为什么它是一个完全不同的值。

对这里发生的事情有任何想法吗?为什么 Python 的 os stat 返回错误的 inode 值? (或根据'stat'命令错误)

【问题讨论】:

  • 您说您使用的是专有文件系统。也许这就是问题所在?这个 inode 数字看起来非常大。
  • 这些数字相当于 mod 2**64
  • “我可以处理它把它当作一个有符号值,因此是负号,”。从有符号转换为无符号不仅仅是添加/删除符号前缀。如果您进行正确的转换,这两个数字是相同的。阅读其中一些资源:What is “2's Complement”?wikipedia Two's complement page
  • 这发生在 python 2.x 中,因为在 python 3.8.6 中它可以正常工作

标签: python linux filesystems stat inode


【解决方案1】:

使用命令模块,它会返回相同的结果。

   import commands
   commands.getstatusoutput('stat /fooDir')

【讨论】:

  • 您可能是对的,但这并不能回答问题,因为在这种情况下,您只需绕过 python 'os.stat()' 命令
  • 感谢您的回复。是的,这可能是一个很好的解决方法。但仍然没有回答问题。
【解决方案2】:

我发现 inode 号本来是 一个无符号长,但 om.stat 正在解释 为负数,因为设置了 MSB。

所以现在我知道它应该是一个未签名的, 我可以通过以下方式解决这个问题:

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

一个更简单的解决方案,没有其他依赖:

if inode < 0 :
    inode += 1 << 64

【讨论】:

    猜你喜欢
    • 2012-05-03
    • 1970-01-01
    • 2010-11-06
    • 2014-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-02
    • 2015-06-26
    相关资源
    最近更新 更多