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

Best way to tell if program is on disk

Started by CodingWithClass, 25 May 2014 - 05:05 PM
CodingWithClass #1
Posted 25 May 2014 - 07:05 PM
I need to tell if a program in a subdirectory is on a disk as I am going to allow installing my OS to disk and there are hardcoded paths that need to be changed if on /disk. Almost all (except startup) of the programs are nested deeply in subdirectories, such as:

/bin/init
/boot/kernel/boot
/etc/init.d/on/cossh
.

My question is: What is the best way to tell if program is on disk?
TheOddByte #2
Posted 25 May 2014 - 11:00 PM
Have you tried shell.resolve?

local path = shell.resolve( shell.getRunningProgram() )
if path:find( "disk" ) then
    print("It's running on a disk")
else
    print("It's not running on a disk")
end
This is just a simple and easy way todo it, there could also be a folder called disk which would make the code above useless.

So here's a more proper way to check if it's on a disk


local function isOnDisk()
    local disks = {}
    for i, v in ipairs( rs.getSides() ) do
        if disk.isPresent( v ) then
            table.insert( disks, disk.getMountPath( v ) )
        end
    end

    local path = shell.resolve( shell.getRunningProgram() )
    for i, v in ipairs( disks ) do
        if path:find( v ) then
            return true
        end
    end
    return false
end

if isOnDisk() then
    print( "It's running on a disk" )
else
    print( "It's not running on a disk" )
end
CodingWithClass #3
Posted 26 May 2014 - 01:03 AM
–snip–
So here's a more proper way to check if it's on a disk


local function isOnDisk()
	local disks = {}
	for i, v in ipairs( rs.getSides() ) do
		if disk.isPresent( v ) then
			table.insert( disks, disk.getMountPath( v ) )
		end
	end

	local path = shell.resolve( shell.getRunningProgram() )
	for i, v in ipairs( disks ) do
		if path:find( v ) then
			return true
		end
	end
	return false
end

if isOnDisk() then
	print( "It's running on a disk" )
else
	print( "It's not running on a disk" )
end

Thanks, I was trying to use fs.getDir(fs.getDir(shell.getRunningProgram())) to get /disk or / :P/> That, as you can imagine, did not work so well.
EDIT: Lyqyd has a better answer with no looping, just fs.getDrive which is apparently new.
Edited on 25 May 2014 - 11:36 PM
Lyqyd #4
Posted 26 May 2014 - 01:05 AM
Or maybe something like:


local function isOnDisk(path)
  local drive = fs.getDrive(path)
  if drive == "rom" or drive == "hdd" then
    return false
  else
    return true
  end
end

I believe fs.getDrive was added in 1.6, though.
TheOddByte #5
Posted 26 May 2014 - 09:12 AM
Or maybe something like:


local function isOnDisk(path)
  local drive = fs.getDrive(path)
  if drive == "rom" or drive == "hdd" then
	return false
  else
	return true
  end
end

I believe fs.getDrive was added in 1.6, though.
Oh.. I gotta start using CC again! I'm not used to the new functions that have been added :(/>
CometWolf #6
Posted 26 May 2014 - 10:44 AM
I suspect using shell.resolve on shell.getRunningProgram might cause some issues if the shell is set to a sub folder. Since getRunningProgram returns the absolut path already.