Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wait forever when running an unzip file command #41

Closed
patchthecode opened this issue Jul 7, 2017 · 12 comments
Closed

Wait forever when running an unzip file command #41

patchthecode opened this issue Jul 7, 2017 · 12 comments

Comments

@patchthecode
Copy link

patchthecode commented Jul 7, 2017

we have this as the command.

run(bash: "unzip /User/myName/Desktop/myZipFile.zip -d /User/myName/Desktop/)

This is just supposed to unzip a rather large zip file (100Mb)
But the Swift shell freezes the app.

We traced it down to where control is frozen, it is in this file Command.swift

public func finish() throws {
   self.waitUntilExit() // <-- It freezes here
   guard self.terminationStatus == 0 else {
      throw CommandError.returnedErrorCode(command: commandAsString()!, errorcode: Int(self.terminationStatus))
   }
}

We are forced to downgrade back to 3.0.0Beta in the mean time. Thanks for your work. Awesome library.

@patchthecode
Copy link
Author

Ok. i tried just unzipping a small text file. Same thing.
Investigating further..

@patchthecode
Copy link
Author

OK. So i have now tried the runAndPrint command

and it correctly unzips the file on terminal, but,
it is sad because i cannot get the output like below

let output = try SwiftShell.runAndPrint("unzip", "/Users/jt/Desktop/Bob/myZipFile.zip", "-d", "/Users/jt/Desktop/Bob")

@kareman
Copy link
Owner

kareman commented Jul 8, 2017

runAndPrint doesn't return anything, so that is expected. Could you try

let output = try SwiftShell.run("unzip", "/Users/jt/Desktop/Bob/myZipFile.zip", "-d", "/Users/jt/Desktop/Bob")

to see if it is bash that never returns. If that doesn't work I'll take a look when I get back to my computer on Monday.

@patchthecode
Copy link
Author

ok. I'll update this over the weekend. And give you the results.

@kareman
Copy link
Owner

kareman commented Jul 10, 2017

I have tested this with runAndPrint using SwiftShell 3.0.1 and it is working fine unless unzip asks for input (if e.g. the files I am unzipping already exist in the destination) and I run it from the terminal. Then the application never finishes. If I run it in Xcode and provide the input in its console it's working fine. It doesn't matter if I build the executable with Xcode or with the Swift Package Manager.

This is very strange, I don't really have any theories at the moment. Have you found any way to work around it?

@patchthecode
Copy link
Author

patchthecode commented Jul 10, 2017

@kareman the workaround for now is to use runAndPrint
Unfortunately, i cannot get a return value because runAndPrint was not built for that.

Although the run command it fails, note that it works with other terminal commands.. it just hangs when unzippng.

I remember way back in my school days something about flushing stdout buffers? But im not sure if this has anything to do with this. It was too long ago.

@kareman
Copy link
Owner

kareman commented Jul 11, 2017

I think the problem only occurs when the “unzip” command asks for an answer to a question (like if it should overwrite already existing files). And it seems it only does this when standard input is a terminal. Some workarounds are

let output = run(bash: "null | unzip /User/myName/Desktop/myZipFile.zip -d /User/myName/Desktop/")

or

let output = "".run("unzip", "/Users/jt/Desktop/Bob/myZipFile.zip", "-d", "/Users/jt/Desktop/Bob")

Or maybe you can call the unzip command with the flags "-n never overwrite existing files" or "-o overwrite files WITHOUT prompting".

Please let me know if any of these suggestions solve your problem.

@Nordeast
Copy link

I am having the same issue here as @patchthecode. The bash command does not seem to ever return.

I can get it to unzip with this command:
run(bash: "unzip ./myfile.zip"

If I then put a flag in the command it no longer returns:
run(bash: "unzip -a ./myfile.zip"
or
run(bash: "unzip ./myfile.zip -d ./myfile2

Interestingly those same commands work fine when I use runAndPrint instead:
try! runAndPrint(bash: "unzip -a ./myfile.zip"
try! runAndPrint(bash: "unzip ./myfile.zip -d ./myfile2"

I am just trying to avoid the console output because its very verbose and doesn't add anything to my script.

@kareman
Copy link
Owner

kareman commented Jul 19, 2017

What happens if you try let output = "".run(bash: "unzip -a ./myfile.zip")?

@Nordeast
Copy link

Yea you are correct it only happens when unzip asks for more input. I thought I was careful to make sure to delete the unzipped file after I was done with it. On a second run of the same script it fails. I was missing a delete of a __MACOSX directory that unzip creates. That was causing unzip to ask for input.

let output = "".run(bash: "unzip -a ./myfile.zip")

Clears the prompt and allows it to continue.

@kareman
Copy link
Owner

kareman commented Jul 22, 2017

Excellent. I'll close this issue now.

@patchthecode feel free to reopen it if your problem was not resolved.

@kareman kareman closed this as completed Jul 22, 2017
@patchthecode
Copy link
Author

@kareman i'll try it today and let you know

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants