使用 kqueue() 监视目录中的更改的最佳方法是什么?

发布于 2024-09-04 08:29:15 字数 309 浏览 2 评论 0原文

好的:我正在 iPhone OS 应用程序中实现文件共享,当然这意味着文件系统监控。耶!

基本上,当用户将文件操作到 iTunes 中我的应用程序部分时,操作系统会在我可以访问的目录中进行复制和/或删除。因此,我需要通过像 kqueue() 这样的有效机制来监视目录的更改。

我如何实现这个以便我知道文件已完成复制?我的想法是:

  • 使用 kqueue() 进行监控。
  • 在事件发生时,开始(或重置现有)超时。
  • 超时后,开始工作。

但是有没有更好的方法来确保我不会超越操作系统的脚趾?

OK: I'm implementing File Sharing in an iPhone OS app, and of course this means filesystem monitoring. Yay!

Basically, the OS copies and/or deletes from and to a directory I can access when the user manipulates files into my app's section in iTunes. Thus, I need to monitor the directory for changes presumably via an efficient mechanism like a kqueue().

How do I implement this so that I know that the files have finished copying? I was thinking along the lines of:

  • Monitor with kqueue().
  • At event, start (or reset existing) timeout.
  • When timeout elapses, do work.

but is there a better way of doing it that ensures I'm not stepping over the OS's toes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

似狗非友 2024-09-11 08:29:15

这只是部分答案,但可能有点用。

  1. kqueue 机制确实可以作为应用程序的文档目录被修改时获得通知的一种方式。

    kqueue 机制确实可以作为应用程序的文档目录被

  2. 一旦目录被修改且复制文件完成之前,就会发出通知。我在测试中观察到了这一点。

不幸的是,复制完成后没有任何形式的通知。至少,我没有找到。

我希望苹果能够意识到,“iTunes 同步”完成后需要发出通知,因为这似乎是一个普遍问题。

检查新文件的修改时间是否未更改的循环可能有效,但仅在大多数情况下有效。在某些极端情况下,iOS 可以暂停在后台执行各种耗时的工作。

如果有一种方法可以检查特定文件是否被另一个进程打开,那么这就足够了。

杰夫

This is only a partial answer, but might be somewhat useful.

  1. The kqueue mechanism indeed does work as a means to be notified when an app's Documents directory is modified.

  2. The notification comes as soon as the directory is modified and before copying a file is complete. I have observed this in testing.

Unfortunately, there is no form of notification when the copy is done. At least, none that I have found.

What I hope Apple will realize is there needs to be a notification for "iTunes synch" is done, because this seems to be a general problem.

A loop checking if the new file's modification time is not changing might work, but only most of the time. In some extreme circumstances, iOS can pause to do all kinds of time consuming work of its own in the background.

If there is a way to check if a particular file is open by another process, then this would be enough.

Geoff

神回复 2024-09-11 08:29:15

我刚刚实现了这个

它使用调度源而不是队列,但工作正常。

自述文件:

FileSystemEventPublisher

跟踪文件系统中更改的发布者。

用法示例:

let cancellable = DispatchSource.publish(
  .all,
  for: FileManager.default.temporaryDirectory
)
.receive(on: RunLoop.main)
.sink { event in
  print(event)
}
//.
//.
//.
cancellable.cancel()

I just implemented this

It uses a dispatch source instead of queue, but works fine.

README:

FileSystemEventPublisher

A publisher that tracks changes in the file system.

Example usage:

let cancellable = DispatchSource.publish(
  .all,
  for: FileManager.default.temporaryDirectory
)
.receive(on: RunLoop.main)
.sink { event in
  print(event)
}
//.
//.
//.
cancellable.cancel()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文