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

Launching program from outside it's directory causes it to fail in locating relay.jar #6

Open
ThinkDigitalSoftware opened this issue Apr 7, 2017 · 10 comments

Comments

@ThinkDigitalSoftware
Copy link

ThinkDigitalSoftware commented Apr 7, 2017

This can cause issues when trying to write scripts that reside outside the project's directory.

$ ./gnirehtet/gnirehtet rt
Starting gnirehtet...
'adb' reverse tcp:31416 tcp:31416
'adb' shell am startservice -a com.genymobile.gnirehtet.START
Starting service: Intent { act=com.genymobile.gnirehtet.START }
'java' -jar relay.jar
Error: Unable to access jarfile relay.jar

$ cd ./gnirehtet/

$ ./gnirehtet rt
Starting gnirehtet...
'adb' reverse tcp:31416 tcp:31416
'adb' shell am startservice -a com.genymobile.gnirehtet.START
Starting service: Intent { act=com.genymobile.gnirehtet.START }
'java' -jar relay.jar
2017-04-07 12:08:07.947 I Main: Starting server...

@Zero3K
Copy link

Zero3K commented Apr 9, 2017

I thought that issue was fixed in an earlier version according to the commit log.

@ThinkDigitalSoftware
Copy link
Author

ThinkDigitalSoftware commented Apr 9, 2017 via email

@Zero3K
Copy link

Zero3K commented Apr 9, 2017

So, you ran v1.0.1 of it?

rom1v added a commit that referenced this issue Apr 10, 2017
The gnirehtet script assumed that it was called from its own directory.

To overcome this limitation, detect and move to the physical directory
of the script. This requires to resolve symlinks first (in case the
script is called through a symlink).

Unfortunately, "realpath" is not always installed by default, and the
"readlink" behavior is different on Mac OS. Therefore, implement a best
effort solution.

See <http://stackoverflow.com/a/17744637/1987178>.

Related to <#6>.
@rom1v
Copy link
Collaborator

rom1v commented Apr 10, 2017

Thank you for the report. Indeed, the script assumed (for simplicity) that it was called from its own directory.

To make it callable from everywhere, we have to detect the actual directory of the script (after symlink resolution), which is unfortunately not easily done by calling commands in a portable way.

Requirements: all the following commands must work on both Linux and Mac OS.

The simple one, from the current directory:

./gnirehtet rt

By calling bash:

bash gnirehtet rt

From another directory:

gnirehtet/gnirehtet rt

From a symlink:

ln -s /the/gnirehtet/directory/gnirehtet /tmp
/tmp/gnirehtet rt

I implemented a best effort solution (53542df, not merged into master), but by default it does not work on Mac OS (it seems that brew install coreutils fixes the problem), so this would break the script even with ./gnirehtet rt for them. So I think we have to find a better solution.

@rom1v rom1v self-assigned this Apr 10, 2017
@ThinkDigitalSoftware
Copy link
Author

ThinkDigitalSoftware commented Apr 10, 2017 via email

@rom1v
Copy link
Collaborator

rom1v commented Apr 10, 2017

@ThinkDigitalRepair The problem is to determine the physical directory of gnirehtet (which is not necessarily the current directory) from the command from which the script has been called (in the variable $0).

For instance, suppose you have these files:

/home/rom/gnirehtet/gnirehtet
/home/rom/gnirehtet/gnirehtet.apk
/home/rom/gnirehtet/relay.jar

And a symlink pointing in /usr/bin, created by:

ln -s /home/rom/gnirehtet/gnirehtet /usr/bin/

Then, when you call gnirehtet or /usr/bin/gnirehtet, we have to retrieve the path /home/rom/gnirehtet to find relay.jar. In theory, this is straightforward: resolve symlinks recursively, then take the directory of the result.

The problem is to do it in a way that is:

  • portable (works natively on Linux and Mac OS)
  • simple (do not reimplement the whell)
  • consistent (should work whether it is called from another directory and/or a symlink and/or by calling bash explicitly…)

This can cause issues when trying to write scripts that reside outside the project's directory.

Meanwhile, of course, as a workaround, you can enclose the call to gnirehtet by calls to cd:

cd /the/directory/of/gnirehtet
./gnirehtet rt
cd -

@ThinkDigitalSoftware
Copy link
Author

ThinkDigitalSoftware commented Apr 10, 2017 via email

@rom1v
Copy link
Collaborator

rom1v commented Apr 10, 2017

Would it be possible to encapsulate relay.jar in the gnirehtet executable?

Would it be preferable to do so?

gnirehtet is just a bash script.

@ThinkDigitalSoftware
Copy link
Author

ThinkDigitalSoftware commented Apr 10, 2017 via email

@ewoks
Copy link

ewoks commented Apr 11, 2017

@rom1v definitely it wouldn't be preferable

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

No branches or pull requests

4 participants