可以打开一个文件,允许其他进程删除这个文件吗?

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

Is it possible to open a file allowing another processes to delete this file?

问题

似乎默认的os.Open调用允许其他进程写入已打开的文件,但不能删除它。是否可以同时启用删除功能?在.NET中,可以使用FileShare.Delete标志来实现这一点,Go语言中是否有类似的功能?

英文:

It seems the default os.Open call allows another processes to write the opened file, but not to delete it. Is it possible to enable deletion as well? In .NET this can be done using FileShare.Delete flag, is there any analog in Go?

答案1

得分: 6

os.Open将以设置了O_RDONLY标志的文件描述符返回,这意味着只读。你可以使用os.OpenFile来指定自己的标志。

O_RDONLY int = syscall.O_RDONLY // 以只读方式打开文件。
O_WRONLY int = syscall.O_WRONLY // 以只写方式打开文件。
O_RDWR   int = syscall.O_RDWR   // 以读写方式打开文件。
O_APPEND int = syscall.O_APPEND // 写入时将数据追加到文件末尾。
O_CREATE int = syscall.O_CREAT  // 如果文件不存在,则创建一个新文件。
O_EXCL   int = syscall.O_EXCL   // 与O_CREATE一起使用,文件必须不存在。
O_SYNC   int = syscall.O_SYNC   // 以同步I/O方式打开。
O_TRUNC  int = syscall.O_TRUNC  // 如果可能,打开文件时将其截断。

然而,这些模式都不允许在单个文件上有多个写入者。你可以通过execfork来共享文件描述符,但是从两个进程中直接写入文件将导致操作系统决定如何同步这些写入,这通常不是你想要的结果。

在类Unix系统上,当一个进程拥有一个文件描述符时,删除该文件并不会产生影响。不过,我假设Windows可能不会喜欢这样做。

鉴于[标签:windows]标签和@Not_a_Golfer的优秀观察:

如果这是解决你的问题的方法,你应该能够将syscall.FILE_SHARE_DELETE作为一个标志传递给os.OpenFile

如果你需要组合多个标志,可以通过按位或操作符将它们组合在一起:

syscall.FILE_SHARE_DELETE | syscall.SOME_OTHER_FLAG | syscall.AND_A_THIRD_FLAG

(注意,你需要构建一个一致的标志)。

英文:

os.Open will get you a file descriptor with flag O_RDONLY set; that means read only. You can specify your own flag by using os.OpenFile

O_RDONLY int = syscall.O_RDONLY // open the file read-only.
O_WRONLY int = syscall.O_WRONLY // open the file write-only.
O_RDWR   int = syscall.O_RDWR   // open the file read-write.
O_APPEND int = syscall.O_APPEND // append data to the file when writing.
O_CREATE int = syscall.O_CREAT  // create a new file if none exists.
O_EXCL   int = syscall.O_EXCL   // used with O_CREATE, file must not exist
O_SYNC   int = syscall.O_SYNC   // open for synchronous I/O.
O_TRUNC  int = syscall.O_TRUNC  // if possible, truncate file when opened.

None of these modes, however, will allow you to have multiple writers on a single file. You can share the file descriptor by exec-ing or fork-ing but naively writing to the file from both processes will result in the OS deciding how to synchronise those writes -- which is almost never what you want.

Deleting a file while a process has a FD on it doesn't matter on unix-like systems. I'll go ahead and assume Windows won't like that, though.

Edit given the [tag:windows] tag and @Not_a_Golfer's excellent observations:

You should be able to pass syscall.FILE_SHARE_DELETE as a flag to os.OpenFile on Windows, if that is what solves your problem.

If you need to combine several flags you can do so by or-ing them together:

syscall.FILE_SHARE_DELETE | syscall.SOME_OTHER_FLAG | syscall.AND_A_THIRD_FLAG

(note, however, that it's up to you to build a coherent flag)

huangapple
  • 本文由 发表于 2014年5月28日 03:11:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/23897174.html
匿名

发表评论

匿名网友

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

确定