This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Advert's profile picture

Determining The Root Directory Of A Disk, When Using The Startup File.

Started by Advert, 09 February 2012 - 04:29 AM
Advert #1
Posted 09 February 2012 - 05:29 AM
Herro, I've been using computercraft on a good friend's server for a while, messing around with it a bit.

We're considering using it to control a IC2 reactor, amongst other things.

I've noticed a problem, however: There doesn't seem to be an "easy" way of finding out what your base directory is if your code runs from a disk, example:


disk/startup:

print("Startup loaded - Attempting to open ./helloworld")
local res, err = pcall(function() dofile("./helloworld") end)
print(string.format("Result: %s; Error?: %s", res or "", err or ""))
-- Try with disk/ since that's most likely
print("Attempting to open disk/helloworld")
local res, err = pcall(function() dofile("disk/helloworld") end)
print(string.format("Result: %s; Error?: %s", res or "", err or ""))

disk/helloworld:

print("Hello, world!")


I'm currently using disk labels as a way to identify, but this is error prone if the disk label is changed. I could put a marker at the top of the file, and save it as a string, then search for it; but I'm too lazy.


local cLabel = "asdf"
local sides = {"back", "left", "right", "bottom", "top",  "front"}
local sDiskSide = ""
local sDiskDir = ""
for _, s in pairs(sides) do
if disk.getLabel(s) == cLabel then
  sDiskSide = s
  sDiskDir = disk.getMountPath(s)
  break
end
end
assert(sDiskSide ~= "", "Error locating disk.")
Casper7526 #2
Posted 09 February 2012 - 08:39 AM
Currently there is no shell.currPath() function this should be added in 1.3 though it's on the list.
Advert #3
Posted 09 February 2012 - 09:22 AM
Currently there is no shell.currPath() function this should be added in 1.3 though it's on the list.

Alright, thanks ;)/>/>
Espen #4
Posted 09 February 2012 - 12:38 PM
You can workaround this problem pretty easily with the following line of code:
shell.resolve( "." )
Advert #5
Posted 09 February 2012 - 02:53 PM
You can workaround this problem pretty easily with the following line of code:
shell.resolve( "." )

I've tried that, but it does not appear to work.


disk/startup

print("shell.resolve('.'): ", shell.resolve('.'))

Yields:

shell.resolve('.'):
Espen #6
Posted 09 February 2012 - 03:34 PM
You can workaround this problem pretty easily with the following line of code:
shell.resolve( "." )

I've tried that, but it does not appear to work.


disk/startup
print("shell.resolve('.'): ", shell.resolve('.'))

Yields:
shell.resolve('.'):
That is because the startup file on the disk is not executed from within the disk path, but from the root directory instead.
That's why shell.resolve(".") returns an empty string "". It means your current directory is the root directory /
It's like manually typing /disk/startup from within the root directory.
So it is doing exactly what it's supposed to do, but it's just that the computer isn't actually changing into the disk to execute its startup file.

Now, I assume you want a program to 'know' in which folder it is residing, regardless from where it has been executed, right? Well that's something entirely different then.
I'm not aware of any quick solution right now, but I'm sure there is a way. You definitely got me interested now, I'm off researching this.
Sorry I can't help you at the moment, but as soon as I find either a solution or a workaround, I'll let you know. ;)/>/>
Advert #7
Posted 09 February 2012 - 04:19 PM
Currently there is no shell.currPath() function this should be added in 1.3 though it's on the list.
You can workaround this problem pretty easily with the following line of code:
shell.resolve( "." )

I've tried that, but it does not appear to work.


disk/startup
print("shell.resolve('.'): ", shell.resolve('.'))

Yields:
shell.resolve('.'):
That is because the startup file on the disk is not executed from within the disk path, but from the root directory instead.
That's why shell.resolve(".") returns an empty string "". It means your current directory is the root directory /
It's like manually typing /disk/startup from within the root directory.
So it is doing exactly what it's supposed to do, but it's just that the computer isn't actually changing into the disk to execute its startup file.

Now, I assume you want a program to 'know' in which folder it is residing, regardless from where it has been executed, right? Well that's something entirely different then.
I'm not aware of any quick solution right now, but I'm sure there is a way. You definitely got me interested now, I'm off researching this.
Sorry I can't help you at the moment, but as soon as I find either a solution or a workaround, I'll let you know. ;)/>/>


Thanks for your efforts, I have a hacky solution as posted in the OP for now, and Casper stated that this'll be added in the future :)/>/>


I would reccomend something like:
os.getActiveScript() os.getActiveScriptPath()