Skip to content

Instantly share code, notes, and snippets.

@Machx
Created February 11, 2013 22:36
Show Gist options
  • Save Machx/4758261 to your computer and use it in GitHub Desktop.
Save Machx/4758261 to your computer and use it in GitHub Desktop.
#include "copyfile.h"
typedef enum {
_NotStarted = 0,
_InProgress,
_Finished,
} _State;
@implementation BAVAppDelegate
{
copyfile_state_t _copyfileState;
_State _state;
NSTimer *_progressTimer;
}
- (void)applicationDidFinishLaunching:(NSNotification *)notification
{
[self performSelectorInBackground:@selector(copyFile) withObject:nil];
_progressTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(outputProgress) userInfo:@{} repeats:YES];
}
- (void)copyFile
{
NSLog(@"Will start copying");
int returnCode;
_copyfileState = copyfile_state_alloc();
{
const char *fromPath = [[@"~/Desktop/bigfile.mkv" stringByExpandingTildeInPath] UTF8String];
const char *toPath = [[@"~/Desktop/bigfile_copy.mkv" stringByExpandingTildeInPath] UTF8String];
_state = _InProgress;
{
returnCode = copyfile(fromPath, toPath, _copyfileState, COPYFILE_ALL);
}
_state = _Finished;
[_progressTimer performSelectorOnMainThread:@selector(invalidate) withObject:nil waitUntilDone:YES];
_progressTimer = nil;
}
copyfile_state_free(_copyfileState);
NSLog(@"Did finish copying with return code %d", returnCode);
}
- (void)outputProgress
{
switch (_state) {
case _NotStarted:
NSLog(@"Not started yet");
break;
case _Finished:
NSLog(@"Finished");
break;
case _InProgress: {
off_t copiedBytes;
const int returnCode = copyfile_state_get(_copyfileState, COPYFILE_STATE_COPIED, &copiedBytes);
if (returnCode == 0)
NSLog(@"Copied %@ so far", [NSByteCountFormatter stringFromByteCount:copiedBytes countStyle:NSByteCountFormatterCountStyleFile]);
else
NSLog(@"Could not retrieve copyfile state");
break;
}
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment