3

I've been digging for an answer for a long time but I never get to a working code.

I have a code that uses dd to generate a file. Sometimes it takes a few minutes depending on the size and I thought it would be great to have a progress bar. So far everything works and is tied up. The progress bar, however, doesn't update because I need to constantly send values to it. I found a way to get the current value, I managed to get pv to display the current data, but now I can't get the output in real time inside the application, except in the log.

So far, this is the best I got to:

// Action:
// dd if=/dev/zero bs=1048576 count=500 |pv -Wn -s <size>|of=/Users/me/Desktop/asd.img
// Be careful, it generates files of 500MB!!

NSTask * d1Task = [[NSTask alloc] init];
NSTask * pvTask = [[NSTask alloc] init];
NSTask * d2Task = [[NSTask alloc] init];

[d1Task setLaunchPath:@"/bin/dd"];
[pvTask setLaunchPath:@"/Users/me/Desktop/pv"];
[d2Task setLaunchPath:@"/bin/dd"];

// For pvTask I use  something like...
// [[NSBundle mainBundle] pathForAuxiliaryExecutable: @"pv"]
// ... in the final version.

[d1Task setArguments: [NSArray arrayWithObjects:
                         @"if=/dev/zero"
                       , @"bs=1048576"
                       , @"count=500"
                       , nil]];

[pvTask setArguments:[NSArray arrayWithObjects:
                        @"-Wn"
                      , [ NSString stringWithFormat:@"-s %d", 1048576 * 500]
                      , nil]];

[d2Task setArguments: [NSArray arrayWithObjects:
                        @"of=/Users/me/Desktop/file.dmg"
                      , nil]];

NSPipe * pipeBetween1 = [NSPipe pipe];
[d1Task setStandardOutput: pipeBetween1];
[pvTask setStandardInput: pipeBetween1];

NSPipe * pipeBetween2 = [NSPipe pipe];
[pvTask setStandardOutput: pipeBetween2];
[d2Task setStandardInput: pipeBetween2];

// Missing code here

[d1Task launch];
[pvTask launch];
[d2Task launch];

In the missing code part, I tried several things. First I tried an observer, like this:

NSFileHandle * pvOutput = [pipeBetween2 fileHandleForReading];
[pvOutput readInBackgroundAndNotify];

[ [NSNotificationCenter defaultCenter]
     addObserver:self
        selector:@selector(outputData:)
            name:NSFileHandleDataAvailableNotification
          object:pvOutput
];

No success. I get feedback only in the beginning or the end of the execution, but still no feedbacks during it.

I also tried something like:

[pvOutput setReadabilityHandler:^(NSFileHandle *file) {
// Stuff here
}];

But there is no such method here. Maybe my XCode is outdated? (I use 4.2).

Lately I've been trying the same generic code using /sbin/ping pinging 10 times a server, but no success getting the output. How can I do this? Any documents I can read about this subject?

Thanks!

Apollo
  • 1,913
  • 2
  • 19
  • 26
  • Not sure if this is the right solution. But how about running your task in a background thread? – Mikael Jun 04 '13 at 09:23
  • Hi Martin, pv is a pipe viewer. Please refer to this page: http://www.catonmat.net/blog/unix-utilities-pipe-viewer/ – Apollo Jun 04 '13 at 10:00
  • Hi Mikael, I tried also using Grand Central Dispatch. My original code uses GCD. This is a side project to test the observers or any other means of getting the feedback from dd and pv. – Apollo Jun 04 '13 at 10:02
  • 1
    All that `dd` does is to copy data. You can do this better yourself without `NSTask` and you have full access to the progress without hacking about. – trojanfoe Jun 04 '13 at 11:30
  • Hi trojanfoe, I have this code as a result from an earlier problem I had to generate big blank files without having problems with memory.. you can check a question I did a while ago related to this here: http://stackoverflow.com/questions/8874757/save-blank-file-in-objective-c – Apollo Jun 04 '13 at 11:49

1 Answers1

2

The pv tool writes the progress output to standard error, so you should establish another pipe:

NSPipe *pvStderrPipe = [NSPipe pipe];
[pvTask setStandardError:pvStderrPipe];
NSFileHandle *pvError = [pvStderrPipe fileHandleForReading];

[pvError waitForDataInBackgroundAndNotify];
[[NSNotificationCenter defaultCenter] addObserverForName:NSFileHandleDataAvailableNotification
                          object:pvError queue:nil
                          usingBlock:^(NSNotification *note) 
{
     NSData *progressData = [pvError availableData];
     NSString *progressStr = [[NSString alloc] initWithData:progressData encoding:NSUTF8StringEncoding];
     NSLog(@"progress: %@", progressStr);

     [pvError waitForDataInBackgroundAndNotify];
 }];

A completely different solution might be to use the following feature of "dd":

If dd receives a SIGINFO signal, the current input and output block counts will be written to the standard error output in the same format as the standard completion message.

So you could connect a pipe to stderr of "dd" and call

kill([ddTask processIdentifier], SIGINFO);

at regular intervals. Then you don't need "pv" and probably only one "dd" task.

Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • If I use `setStandardError` I get a very long log with `¿¿¿¿...`, the observer still doesn't get updated. I will try the second suggestion, though, I appreciate! – Apollo Jun 04 '13 at 11:21
  • So far, `kill` doesn't seem to do anything, I'll check the docs. I did like this: `while ([ddTask isRunning]) { kill([ddTask processIdentifier], SIGINFO); }`. The file is generated fine, no output. – Apollo Jun 04 '13 at 11:39
  • @Apollo: I have added more sample code that worked in my test application. - The second solution was just an idea. You would have to use timers, a simple while-loop cannot work because program control then never returns to the main runloop. – Martin R Jun 04 '13 at 11:46
  • @Apollo: You are welcome. - But note that trojanfoe made a valid point in his comment. A simple loop that reads chunks from one file and write them to another file should be easy to implement (e.g. using NSFileHandle) and probably more more efficient. – Martin R Jun 04 '13 at 12:05