Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
dgelessus

NamedMount, a simple mount-by-label script

Recommended Posts

This is a short boot/rc script that automatically mounts all new file systems under a mount point named after their label, in addition to the normal name assigned by the OS. Special characters in the label are replaced by an underscore and a conflicting name gets the start of its address appended, so the obvious tricky situations should not cause any problems.

 

I wouldn't be surprised if someone else had already written this kind of thing or if it was integrated in some of the non-default operating systems, but I couldn't find anything on the forums or OpenPrograms, so here it is.

(Yes, half of this is copied from OpenOS's automatic mounting code.)

-- NamedMount, a short boot script that gives labelled file systems named mount
-- points in /mnt. For example a floppy disk labelled "floppy" would get mounted
-- at /mnt/floppy in addition to the normal mount point assigned by the OS.
-- 
-- All non-alphanumeric characters except the underscore are replaced with
-- an underscore to ensure that the name is usable in a shell.
-- If a conflicting mount point already exists, the start of the address is
-- appended to the label to prevent overwriting other mount points.
--------------------------------------------------------------------------------

local component = require("component")
local event = require("event")
local fs = require("filesystem")

local function doNamedMount(name, address, componentType)
  if componentType == "filesystem" then
    local proxy = component.proxy(address)
    if proxy and proxy:getLabel() then
      local path = fs.concat("/mnt", (proxy:getLabel():gsub("[^%w_]+", "_")))
      if fs.exists(path) then
        -- Mount point exists already, start appending address
        local addrlen = 3
        local path = path .. "-" .. address:sub(1, addrlen)
        while fs.exists(path)
          and addrlen < address:len() -- just to be on the safe side
        do
          path = path .. address:sub(addrlen, addrlen)
        end
      end
      fs.mount(proxy, path)
    end
  end
end

function start(...)
  event.listen("component_added", doNamedMount)
end

-- Uncomment when placing this in /boot instead of using rc
-- start()

Edit: Added start function for use as a rc script.

Edited by dgelessus
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.