Java的File.setLastModified()在Linux上失败的问题,当你不是文件所有者时。

huangapple go评论93阅读模式
英文:

Java File.setLastModified() fails on Linux when you are not the file owner

问题

我有一个Java的JAR文件,它使用File.setLastModified()方法来根据/mnt/nxx/file.name引用的文件在特定时间戳上设置文件的时间。当文件的所有者是运行Java进程的登录用户的UID时,它按预期工作。但如果登录用户是任何其他UID,则会失败。

该文件托管在外部USB驱动器上,格式为Ext4文件系统,在树莓派上通过/etc/fstab在引导时挂载,使用以下方式:

/dev/sdb1 /mnt/nxx ext4 defaults 0 2

在同一网络上的Ubuntu主机也会在引导时通过/etc/fstab挂载相同的磁盘,使用以下方式:

192.168.x.x:/mnt/nxx /mnt/nxx nfs rw 0 1

只要登录用户具有与树莓派上的UID相同的UID,它也可以成功地设置时间戳。

然而,通过Samba连接到相同磁盘的Windows 10主机(映射为N:\到\RASPI\root\mnt\)并运行相同的Java代码,可以成功地为任何文件设置时间戳,而不管Pi所声称的文件所有者是谁。

所涉及的文件具有777权限,并且如预期的那样,家庭网络中任何主机上的任何用户都可以成功地修改该文件。唯一的问题是,当Linux主机上的用户UID与文件所有者不匹配时,调用setLastModified()的Java代码会失败,即使用户的Java进程与文件的GID相同。

我的目标是能够挂载该磁盘并通过任何方法在家庭网络中的所有主机之间共享它,以便家庭网络中的Windows和Linux主机都可以使用Java代码成功地操作时间戳。

为什么Windows 10能够成功地操作时间戳,而与用户无关,而Linux则不能?是否有一种方式可以在各种fstab挂载指令、smb.conf或其他地方添加/配置选项,以便配置磁盘,使Linux的行为与Windows相同?

顺便说一下:使用NTFS文件系统格式的外部挂载的HDD也存在相同的问题。我尚未尝试过在NTFS驱动器上进行用户映射。

任何帮助将不胜感激。

cw

英文:

I have a java jar file which uses File.setLastModified() to set a specific time stamp for a file by referring to /mnt/nxx/file.name. It works as desired when the file is owned by the same UID as the logged-in user running the java process. But it fails if the logged-in user is any other UID.

The file is hosted on an external USB drive formatted with an Ext4 file system mounted on a Raspberry Pi at boot time via /etc/fstab using:

/dev/sdb1 /mnt/nxx ext4 defaults 0 2

An Ubuntu host on the same network, and which mounts the same disk at boot time via /etc/fstab using:

192.168.x.x:/mnt/nxx /mnt/nxx nfs rw 0 1

also can successfully set the time-stamp as long as the logged-in user has the same UID as on the Pi.

However, a Windows-10 host that connects via Samba to the same disk (mapped as N:\ to \ RASPI\root\mnt) and running the same java code can successfully set the time-stamp for ANY file regardless of the file owner as claimed by the Pi.

The file in question has 777 permissions and, as expected, any user on any host in my home network can successfully modify the file. The only issue is that the java call to setLastModified() fails when the UID of the user on the Linux hosts doesn’t match the file owner -- even if the user java process is running with the same GID as the file.

My goal is to be able to mount the disk and share it across all the hosts in my home network via whatever method so that both the Windows and Linux hosts on my home network can use the java code to successfully manipulate the time-stamps.

Why can Windows-10 successfully manipulate the time-stamp regardless of the user while Linux can not? Is there a way for me to add/configure options in the various fstab mount directives, in smb.conf, or elsewhere to configure the disk so that Linux will behave the same way Windows does?

BTW: The same issue exists with externally mounted HDDs formatted with NTFS file systems. I have not tried usermapping with the NTFS drives.

Any help would be appreciated.

cw

答案1

得分: 2

以下是翻译好的部分:

对于那些遇到这个问题的人,我认为以下进一步的信息提供了一种可行的解决方法,即使潜在的问题似乎仍然存在。

这个问题似乎只在以下情况下表现出来:(1)所涉及的文件不是由正在运行的 Java 进程的 UID 所拥有(注意:文件的读写权限不是问题所在,重要的是文件的所有权),以及(2)所涉及的文件所在的磁盘卷是以与文件所有者不同的 UID 挂载的。在这些条件下,Java 无法如预期般设置最后修改时间。

如果磁盘以 CIFS 形式挂载,或者以与文件所有者的 UID 相匹配的 UID 挂载,那么 Java 就可以成功地管理时间戳。

此外,这个问题只在 Linux 主机上表现出来。无论文件所有权或文件系统如何,在 Windows 主机上似乎都不会出现这个问题。

英文:

Here is some further information for those encountering this issue which I believe offers a viable work-around even if the underlying issue appears to remain.

The issue appears to manifest itself only when (1) the files in question are NOT owned by the UID of the running java process (Note: File read/write permissions are not the issue here, it is file ownership that matters,) and (2) the disk volume on which the files in question reside is mounted under a UID different from that of the file owner. Under those conditions, java fails to set the Last-Modified time as expected.

If the disk is mounted as CIFS or under a UID which matches that of the file owner, then java can successfully manage the time-stamps.

Also, this issue is manifest only on Linux hosts. Windows hosts do not appear to manifest this issue regardless of file ownership or file system.

huangapple
  • 本文由 发表于 2020年4月3日 19:43:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/61011124.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定