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

Serialization problems

Question

So, I have been working on an API which allows me to assign additional addresses to computers. And No I have a moment of confusion:

function internal.dns.loadHosts()
  local HOST_FILE_LOC = os.getenv("HOST_FILE_LOC")
  print(HOST_FILE_LOC) --Prints /usr/misc/hosts.txt
  assert(HOST_FILE_LOC, "Missing environment variable: HOST_FILE_LOC pointing to a file used for storing dns mamppings ")
  if (filesystem.exists(HOST_FILE_LOC)) then
    local hostFile = io.open(HOST_FILE_LOC)
    print(serialization.unserialize(hostFile:read("*all"))) --prints a table
    local hosts
    hosts = serialization.unserialize(hostFile:read("*all"))
    print(hosts) -- prints nil
    hostFile:close()
    return hosts --Return a table of hosts, if successful
  else
    return nil --Return nil if hostfile doesnt exist
  end
end

Why is it that these two seemingly same calls return different values. one is table other is nil?

Share this post


Link to post
Share on other sites

2 answers to this question

Recommended Posts

  • 0

I haven't tested but I believe the file descriptor has a file position. After the call to :read("*all") you're at the end of the file and thus nil is returned.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

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