RunProgram tar with lots of arguments

Linux specific forum
broozar
User
User
Posts: 61
Joined: Sat May 08, 2010 11:21 pm
Location: Berlin, Germany

RunProgram tar with lots of arguments

Post by broozar »

hi all,

i need to extract a single (README) file form a tar.gz archive. the model command is:

Code: Select all

tar --extract --file="/home/user/packs/testpack.tar.gz" -C /tmp/packs/ "testpack/README"
this works fine. i am now trying to convert this into PB's RunProgram:

Code: Select all

RunProgram("tar", "--extract --file=" + #DQUOTE$ + source + #DQUOTE$ + " -C /tmp/packs/ " + #DQUOTE$ +  tarpack + "/" + filename + #DQUOTE$, "/tmp", #PB_Program_Wait)
which results in an error:

Code: Select all

tar: "/home/user/packs/testpack.tar.gz": Cannot open: No such file or directory
tar: Error is not recoverable: exiting now
file permissions are at 755, and the tar.gz is owned by the user. Any ideas/suggestions?
User avatar
Vera
Addict
Addict
Posts: 858
Joined: Tue Aug 11, 2009 1:56 pm
Location: Essen (Germany)

Re: RunProgram tar with lots of arguments

Post by Vera »

Hi broozar,

I finally got it working :-)
... and hope it does for you too

I reconstructed your example except:
- the testpack.tar.gz doesn't include a further folder 'testpack' in it
- I didn't create those /packs subfolders

Code: Select all

source.s = "/home/user/testpack.tar.gz"
filename.s = "README"

;; you can try this:
;params.s = "--extract --file=" + source + " -C /tmp " + filename 
;Debug params
;RunProgram("tar", params, "", #PB_Program_Wait) 

;; or use this way:
RunProgram("tar", "--extract --file=" + source + " -C /tmp " + filename , "/tmp", #PB_Program_Wait) 
broozar
User
User
Posts: 61
Joined: Sat May 08, 2010 11:21 pm
Location: Berlin, Germany

Re: RunProgram tar with lots of arguments

Post by broozar »

thanks for your reply! i did not have the time to try it until today.

I don't quite see where your code diverges from mine, except for the #DQUOTE$ obviously and the folder structure. The thing is, i need both:
- #DQUOTE$ is required because the paths potentially could have spaces in them
- i need the folder structure for the post processing i do

what irritates me is that the command works as long as i type it into the terminal directly, so i suppose i am doing something weird with the translation into PB.

[edit] i figured out that my command works when I remove the #DQUOTE$:

Code: Select all

; does not work:
--file=" + #DQUOTE$ + source + #DQUOTE$
; but this does:
--file=" + source 
unfortunately, i need the quotes in case there are spaces in the file name... any idea how to solve that?
User avatar
Vera
Addict
Addict
Posts: 858
Joined: Tue Aug 11, 2009 1:56 pm
Location: Essen (Germany)

Re: RunProgram tar with lots of arguments

Post by Vera »

Hello broozar,

I gave it another intense research, but nothing what I'd found and tried worked out to process the tar command via RunProgram where the source has quotation marks. Even the trick to directly add single-quotations into the parameter like this :
params.s = "--extract --file='" + source + "' -C /tmp " + filename
wouldn't do. The params-string in itself is fine and would run alike via console.

I came across several hints showing that quoting the source itself didn't work but instead quoting the full argument would do. Still none of my test-variations turned out as wished.

I read an interesting remark by freak (2008) and wonder it it still holds:
freak wrote:On Windows, the parameters are parsed by the system.
On Linux the RunProgram() command itself does the parsing and passes the argv Array when executing the new program.
The PB parsing only looks For quotation marks at the beginning and end of the parameters so far.
Only additional quotations to the filename will successfully extract a file that might have spaces in its name.

I don't know, maybe it's better to create/delete a bash script and run that one?
broozar
User
User
Posts: 61
Joined: Sat May 08, 2010 11:21 pm
Location: Berlin, Germany

Re: RunProgram tar with lots of arguments

Post by broozar »

so quotation marks are apparently out. fortunately, we can escape paths with backslashes, so something like this should be valid:

Code: Select all

/tmp/folder\ with\ spaces/target
but lo and behold, if you put that into RunProgram, it comes out as

Code: Select all

/tmp/folder\\ with\\ spaces/target
this is annoying and I am slowly running out of options. since target and source folders switch dynamically, i also cannot delegate the task to an external bash script.

any other ideas?
infratec
Always Here
Always Here
Posts: 7662
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: RunProgram tar with lots of arguments

Post by infratec »

For testing:

Save this as test_tar.pb compile it and call it with your code.
Then look inside /tmp/out.txt to see with which paramters you called test_tar.

Code: Select all

If CreateFile(0, "/tmp/out.txt")
  For i = 0 To CountProgramParameters() - 1
    WriteStringN(0, ProgramParameter(i))
  Next i
  CloseFile(0)
EndIf
Bernd
broozar
User
User
Posts: 61
Joined: Sat May 08, 2010 11:21 pm
Location: Berlin, Germany

Re: RunProgram tar with lots of arguments

Post by broozar »

interesting idea, here the results with a file named "copy of pack.tar.gz":

1. name only

Code: Select all

-ztf
copy
of
pack.tar.gz
.. as expected: the spaces are taken as delimiters.

2. #DQUOTE$ + name + #DQUOTE$ (or Chr(34) instead of #DQUOTE$)

Code: Select all

-ztf
copy of testpack.tar.gz
so this SHOULD work, however when i do the same with a terminal log, i get "[...]/copy: Cannot open: No such file or directory" which means the space is taken as a delimiter. Also, quotes are not printed into out.txt as I expected.

3. ReplaceString(name, " ", "\ ")

Code: Select all

-ztf
copy\
of\
pack.tar.gz
... which is obviously nonsense. terminal output is: "[...]/copy\\: Cannot open: No such file or directory" which means my single backslash is escaped, rendering it a double backslash.

4. #DQUOTE$ + ReplaceString(name, " ", "\ ") + #DQUOTE$

Code: Select all

-ztf
copy\ of\ pack.tar.gz
... which again SHOULD work, but the terminal output is again "[...]/copy\\" -> escaped backslash

5. #DQUOTE$ + Chr(34) + name + Chr(34) + #DQUOTE$

Code: Select all

-ztf
copy
of
pack.tar.gz""
now this is the weirdest of all... both double quotes at the end instead of where i placed them in the code, and the name is broken up into 3 arguments.

5 shows that there is some serious argument pre-/post-processing going on that I have little influence on. Any idea on how to circumvent it?
User avatar
Vera
Addict
Addict
Posts: 858
Joined: Tue Aug 11, 2009 1:56 pm
Location: Essen (Germany)

Re: RunProgram tar with lots of arguments

Post by Vera »

Hi again,

Thanks Bernd ~ your tricky tool was a good help :D

Well as for me I got it working with both paths (source + file) containing spaces.
For the final [closing?] file-parameter it doesn't matter if it's enclosed by the needed quotes in its path-definition or directly within the run command.

But for the source-path the trick is to enclose the full parameter-sequence to which it belongs with quotes within the run-command.

These work alike:

Code: Select all

source.s = "/home/user/test pack.tar.gz"
filename.s =  Chr(34) + "READ ME" + Chr(34)

RunProgram("/home/user/test_tar", "--extract "+ Chr(34) + "--file=" + source + Chr(34) + " -C /tmp " + filename , "/tmp", #PB_Program_Wait) 
RunProgram("tar", "--extract "+ Chr(34) + "--file=" + source + Chr(34) + " -C /tmp " + filename , "/tmp", #PB_Program_Wait) 

Code: Select all

source.s = "/home/user/test pack.tar.gz"
filename.s =  "READ ME"

RunProgram("/home/user/test_tar", "--extract "+ Chr(34) + "--file=" + source + Chr(34) + " -C /tmp " +  Chr(34) + filename  + Chr(34) , "/tmp", #PB_Program_Wait) 
RunProgram("tar", "--extract "+ Chr(34) + "--file=" + source + Chr(34) + " -C /tmp " + Chr(34) + filename  + Chr(34), "/tmp", #PB_Program_Wait) 
test_tar wrote:--extract
--file=/home/user/test pack.tar.gz
-C
/tmp
READ ME
Note: choosing to place the space-limiter from "--extract --file=" behind extract: "--extract "+Chr(34)+"--file=" changes the output of test_tar to what is shown above.
In the other case you'd see a broken parameter but than the quotations as well. Even this way has no negativ effect on the success


greets ~ Vera
broozar
User
User
Posts: 61
Joined: Sat May 08, 2010 11:21 pm
Location: Berlin, Germany

Re: RunProgram tar with lots of arguments

Post by broozar »

omg fantastic! i was not sure if I could finish the project due to this problem, but now it's fixed! wheee i am so happy right now! thanks a lot!
Post Reply