如何检查两个文件名是否指向相同的物理文件 [英] How to check whether two file names point to the same physical file

查看:77
本文介绍了如何检查两个文件名是否指向相同的物理文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有接受两个文件名作为参数的程序:读取的第一个文件,以创建第二文件。我怎样才能确保该程序不会覆盖第一个文件?

I have a program that accepts two file names as arguments: it reads the first file in order to create the second file. How can I ensure that the program won't overwrite the first file?

限制:


  • 的方法必须保持工作时,文件系统支持(软,硬)链接

  • 文件权限是固定的,它仅需要第一个文件是可读的,第二个文件可写

  • 应该preferably是平台中立的(虽然Linux是主要目标)

推荐答案

在Linux中,打开这两个文件,​​并使用 FSTAT 检查 st_ino中()和 st_dev 是相同的。 打开将遵循符号链接。不要使用与stat 直接,至prevent竞争条件。

On linux, open both files, and use fstat to check if st_ino (edit:) and st_dev are the same. open will follow symbolic links. Don't use stat directly, to prevent race conditions.

这篇关于如何检查两个文件名是否指向相同的物理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆