lib: Move generic file-handling code to lib/