差分
このページの2つのバージョン間の差分を表示します。
両方とも前のリビジョン 前のリビジョン 次のリビジョン | 前のリビジョン | ||
translation:working:翻訳5 [2016-04-22 12:53] Decomo |
translation:working:翻訳5 [2016-08-24 13:46] (現在) Decomo |
||
---|---|---|---|
行 1: | 行 1: | ||
- | Using the File System Events | + | ====== ファイルシステムイベントAPIを使う ====== |
- | The File System Events | + | ファイルシステムイベントAPIは、はっきりと異なる幾つかの機能グループからなります。 |
+ | '' | ||
+ | '' | ||
- | The life cycle of a file system events stream is as follows: | + | ファイルシステムイベントストリームのライフサイクルは下記の通りです: |
- | The application creates a stream by calling | + | * アプリケーションが'' |
+ | * アプリケーションが'' | ||
+ | * アプリケーションが'' | ||
+ | * アプリケーションが到着したイベントを処理する。APIは手順1で指定されたコールバック関数を呼び、イベントを投稿する。 | ||
+ | * アプリケーションが'' | ||
+ | * もしアプリケーションがストリームを再スタートする必要があれば、手順3へ飛ぶ。 | ||
+ | * アプリケーションが'' | ||
+ | * アプリケーションが'' | ||
+ | * アプリケーションが'' | ||
- | The application schedules the stream on the run loop by calling FSEventStreamScheduleWithRunLoop. | + | 以下の節で、これら手順をより詳しく解説します。 |
- | The application tells the file system events daemon to start sending events by calling FSEventStreamStart. | + | ===== include命令の追加 ===== |
- | The application services events as they arrive. The API posts events by calling the callback function specified in step 1. | + | ファイルシステムイベントストリームAPIを使うには、次のようにCore Servicesフレームワークをインクルードしなければなりません: |
- | + | ||
- | The application tells the daemon to stop sending events by calling FSEventStreamStop. | + | |
- | + | ||
- | If the application needs to restart the stream, go to step 3. | + | |
- | + | ||
- | The application unschedules the event from its run loop by calling FSEventStreamUnscheduleFromRunLoop. | + | |
- | + | ||
- | The application invalidates the stream by calling FSEventStreamInvalidate. | + | |
- | + | ||
- | The application releases its reference to the stream by calling FSEventStreamRelease. | + | |
- | + | ||
- | These steps are explained in more detail in the sections that follow. | + | |
- | Adding Include Directives | + | |
- | + | ||
- | Before you use the file system event stream API, you must include the Core Services | + | |
+ | <code c> | ||
#include < | #include < | ||
+ | </ | ||
- | When you compile, you must include the Core Services | + | コンパイルの際、Core ServicesフレームワークをXcodeのターゲットに追加するか、コマンドラインまたは'' |
- | Creating an Event Stream | + | |
- | + | ||
- | The file system events API supports two types of event streams: per-disk event streams and a per-host event streams. Before you can create a stream, you must decide which type of stream to create: a per-host event stream or a per-disk event stream. You can create these streams by calling the functions FSEventStreamCreate and FSEventStreamCreateRelativeToDevice, | + | |
- | + | ||
- | A per-host event stream consists of events whose IDs are increasing with respect to other events on that host. These IDs are guaranteed to be unique with one exception: if additional disks are added from another computer that was also running OS X v10.5 or later, historical IDs may conflict between these volumes. Any new events will automatically start after the highest-numbered historical ID for any attached drive. | + | |
- | + | ||
- | A per-disk event stream, by contrast, consists of events whose IDs are increasing with respect to previous events on that disk. It does not have any relationship with other events on other disks, and thus you must create a separate event stream for each physical device that you wish to monitor. | + | |
- | + | ||
- | In general, if you are writing software that requires persistence, | + | |
- | + | ||
- | Note: Because disks can be modified by computers running earlier versions of OS X (or potentially other operating systems), you should treat the events list as advisory rather than a definitive list of all changes to the volume. If a disk is modified by a computer running a previous version of OS X, the historical log is discarded. | + | |
- | + | ||
- | For example, backup software should still periodically perform a full sweep of any volume to ensure that no changes fall through the cracks. | + | |
- | + | ||
- | If you are monitoring files on the root file system, either stream mechanism will behave similarly. | + | |
- | + | ||
- | For example, the following snippet shows how to create an event stream: | + | |
- | + | ||
- | /* Define variables and create a CFArray object containing | + | |
- | + | ||
- | | + | |
- | + | ||
- | */ | + | |
- | + | ||
- | CFStringRef mypath = CFSTR("/ | + | |
- | + | ||
- | CFArrayRef pathsToWatch = CFArrayCreate(NULL, | + | |
- | + | ||
- | void *callbackInfo = NULL; // could put stream-specific data here. | + | |
- | + | ||
- | FSEventStreamRef stream; | + | |
- | + | ||
- | CFAbsoluteTime latency = 3.0; /* Latency in seconds */ | + | |
- | + | ||
- | + | ||
- | /* Create the stream, passing in a callback */ | + | ===== イベントストリームの作成 ===== |
- | stream = FSEventStreamCreate(NULL, | + | ファイルシステムイベントAPIは2種類のイベントストリームに対応します。ディスク毎のイベントストリームとホスト毎のイベントストリームです。 |
+ | ストリームを生成する前に、どちらのストリームタイプにするか決めなければなりません。 | ||
+ | '' | ||
- | & | + | ホスト毎イベントストリームは、そのホストにおける他のイベントに関連して増加するIDで構成されます。 |
+ | IDは1つの例外を除いて一意であることが保証されます。OS X v10.5以降の別のマシンで使われていた複数のディスクが追加されると、それらボリューム間で履歴IDが衝突する可能性があります。 | ||
+ | 新規イベントは、追加ディスクの履歴IDの最大値以降で自動的に開始されます。 | ||
- | callbackInfo, | + | 対照的に、ディスク毎イベントストリームは、そのディスクにおける以前のイベントに関連して増加するIDで構成されます。 |
+ | 他のディスクの他のイベントとは一切関係せず、従って、個々の物理デバイスをモニターしたければ個別にイベントストリームを生成せねばなりません。 | ||
- | pathsToWatch, | + | 一般的に、永続性が要求されるソフトウェアを書いているならば、IDの衝突を避けるためにディスク毎ストリームを使うべきです。 |
+ | 一方、ホスト毎ストリームは、キューディレクトリの監視といった、通常実行中のディレクトリやディレクトリツリー内の変更をモニタリングする場合に最も向いています。 | ||
- | kFSEventStreamEventIdSinceNow, | + | < |
+ | 以前のOS Xバージョン(または可能性として他のオペレーティングシステム)が稼動するコンピュータによってディスクは変更される事があるため、イベントリストはそのボリュームに対する全ての変更の確実なリストとしてではなく、< | ||
+ | 前バージョンのOS Xが動いているマシンでディスクを変更した場合、変更ログは破棄されます。 | ||
+ | </ | ||
- | latency, | + | 例えば、バックアップソフトウェアは、変更の見落としがないことを確実にするために、定期的に全てのボリュームをフルスキャンするべきです。 |
- | kFSEventStreamCreateFlagNone /* Flags explained in reference */ | + | ルートファイルシステム上のファイルを監視する場合は、どちらのストリーム機構も同じような振る舞いをします。 |
- | ); | + | 例として、以下の< |
- | Once you have created an event stream, you must schedule it on your application’s run loop. To do this, call FSEventStreamScheduleWithRunLoop, passing in the newly-created | + | <code c> |
+ | /* 監視するパスのCFStringオブジェクトを含む | ||
+ | | ||
+ | */ | ||
+ | CFStringRef mypath = CFSTR("/ | ||
+ | CFArrayRef pathsToWatch = CFArrayCreate(NULL, (const void **)& | ||
+ | void *callbackInfo = NULL; // ここでストリーム固有データを入れられる。 | ||
+ | FSEventStreamRef | ||
+ | CFAbsoluteTime latency = 3.0; /* レイテンシは秒数 */ | ||
- | If you don’t already have a run loop, you will need to devote a thread to this task. After creating a thread using your API of choice, call CFRunLoopGetCurrent to allocate an initial run loop for that thread. Any future calls to CFRunLoopGetCurrent will return the same run loop. | + | /* コールバックを渡してストリームを生成する */ |
+ | stream = FSEventStreamCreate(NULL, | ||
+ | & | ||
+ | callbackInfo, | ||
+ | pathsToWatch, | ||
+ | kFSEventStreamEventIdSinceNow, | ||
+ | latency, | ||
+ | kFSEventStreamCreateFlagNone /* 関数リファレンスで説明されているフラグ */ | ||
+ | ); | ||
+ | </ | ||
- | For example, the following snippet shows how to schedule a stream, called stream, on the current thread’s run loop (not yet running): | + | イベントストリームを生成した時点で、それをアプリケーションの実行ループにスケジューリングすべきです。 |
+ | これを行うには、生成した新規ストリーム、実行ループ、実行ループモードを引数として'' | ||
+ | 実行ループに関する更なる情報は[[https:// | ||
- | FSEventStreamRef stream; | + | もし、まだ実行ループを持っていなければ、このタスクのためにスレッドを割り当てる必要があるでしょう。 |
+ | あなたが選択したAPIを使ってスレッドを生成した後は、そのスレッドに最初の実行ループを確保するために'' | ||
+ | 以後'' | ||
- | | + | 例として、以下の< |
- | | + | <code c> |
+ | FSEventStreamRef stream; | ||
+ | /* 以下の関数を呼ぶ前にストリームを生成する。 */ | ||
+ | FSEventStreamScheduleWithRunLoop(stream, | ||
+ | </ | ||
- | The final step in setting up an event stream is to call FSEventStreamStart. This function tells the event stream to begin sending events. Its sole parameter is the event stream to start. | + | イベントストリーム設定の最終工程は'' |
+ | この関数は、イベントストリームにイベントの送信開始を指示します。 | ||
+ | 開始させるイベントストリームが唯一の引数です。 | ||
- | Once the event stream has been created and scheduled, if your run loop is not already running, you should start it by calling | + | イベントストリームの生成とスケジュールを行い、もしまだ実行ループを走らせていなければ、'' |
- | Handling Events | + | |
- | Your event handler callback must conform to the prototype for FSEventStreamCallback. The parameters are described in the reference documentation for the FSEventStreamCallback data type. | + | ===== イベントの取り扱い ===== |
- | Your event handler receives three lists: a list of paths, a list of identifiers, | + | イベントハンドラーコールバックは'' |
+ | パラメータは'' | ||
- | For each event, you should scan the directory at the specified path, processing its contents as desired. Normally, you need to scan only the exact directory specified by the path. However, there are three situations in which this is not the case: | + | イベントハンドラは、パス、識別子、フラグの3つのリストを受け取ります。 |
+ | 実質的に、これらがイベントのリストを表します。 | ||
+ | The first event consists of the first entry taken from each of the arrays, and so on. | ||
+ | イベント処理の必要に応じて、ハンドラはこれらリストをイテレートして下さい。 | ||
- | If an event in a directory occurs at about the same time as one or more events in a subdirectory of that directory, the events may be coalesced into a single event. In this case, you will receive an event with the kFSEventStreamEventFlagMustScanSubDirs flag set. When you receive such an event, you must recursively rescan | + | For each event, you should scan the directory at the specified |
+ | 通常、そのパスによって示されるディレクトリだけを走査する必要があります。しかしながら、そのケースに当てはまらない3つの状況があります: | ||
- | If a communication error occurs between the kernel and the user-space daemon, you may receive an event with either the kFSEventStreamEventFlagKernelDropped | + | * 1つのディレクトリにおいて、サブディレクトリ内で複数のイベントが同時に発生した場合、それらイベントは1つのイベントにまとめられる可能性があります。このケースでは、'' |
+ | * カーネルとユーザー空間の間でコミュニケーションエラーが発生した場合、'' | ||
+ | < | ||
+ | イベントのドロップ時は'' | ||
+ | 上で述べたように、パスのフルスキャンを行うべきかどうかの判断では、 | ||
Note: When an event is dropped, the kFSEventStreamEventFlagMustScanSubDirs flag is also set. Thus, it is not necessary to explicitly check for the dropped event flags when determining whether to perform a full rescan of a path. The dropped event flags are provided purely for informational purposes. | Note: When an event is dropped, the kFSEventStreamEventFlagMustScanSubDirs flag is also set. Thus, it is not necessary to explicitly check for the dropped event flags when determining whether to perform a full rescan of a path. The dropped event flags are provided purely for informational purposes. | ||