1
0
mirror of https://github.com/TomHarte/CLK.git synced 2026-01-26 06:16:22 +00:00
Files
CLK/OSBindings/Mac/Clock Signal/File Observer/CSFileContentChangeObserver.m
2025-02-22 22:40:15 -05:00

48 lines
975 B
Objective-C

//
// CSFileObserver.m
// Clock Signal
//
// Created by Thomas Harte on 22/02/2025.
// Copyright © 2025 Thomas Harte. All rights reserved.
//
#import "CSFileContentChangeObserver.h"
@implementation CSFileContentChangeObserver {
int _fileDescriptor;
dispatch_source_t _source;
}
- (nullable instancetype)initWithURL:(nonnull NSURL *)url handler:(nonnull dispatch_block_t)handler {
if(!url.isFileURL) {
return nil;
}
self = [super init];
if(self) {
_fileDescriptor = open(url.fileSystemRepresentation, O_EVTONLY);
if(_fileDescriptor <= 0) {
return nil;
}
_source = dispatch_source_create(
DISPATCH_SOURCE_TYPE_VNODE,
(uintptr_t)_fileDescriptor,
DISPATCH_VNODE_WRITE,
dispatch_get_global_queue(QOS_CLASS_DEFAULT, 0)
);
dispatch_source_set_event_handler(_source, handler);
dispatch_resume(_source);
}
return self;
}
- (void)dealloc {
if(_fileDescriptor) {
close(_fileDescriptor);
dispatch_source_cancel(_source);
}
}
@end