Return directory as Data type?

Hello!

OSX, Swift5

I would like to be able to return a directory as a Data object and would like to avoid re-inventing the wheel. FileManager has a method contents() which returns a Data type, but contentsOfDirectory returns a list of urls. (which is kind of inconsistent)

It would be pretty great if I could return a Data object for a directory rather than getting a list of urls or having it fail when attempting to use .contents() on a directory. Anyone know of a way to do this?

What, exactly, do you want the Data object to contain?

The .contents() method of the FileManager returns the contents of the file. At it's most general, a file is just a bag of bytes, so, returning a Data object makes a lot of sense. A file directory is a collection of file names, so, returning a list of URLs makes most sense. Do you want the system to provide a "bag of bytes" that you have to parse to find the URL names? Seems to me that having the system provide the parsing would be preferable.

You always have access to the "C" file management systems, even from Swift (you will have to use unmanaged pointers for the directory pointers, etc.).

Thanks for the info. I am more used to windows and It seemed strange to me that a directory was different from a file. I want a bag of bytes that represents the directory so I can verify the integrity of the directory. If I am getting a list of directory names I need to then manually target and check/hash/compare each file instead of just the overall directory which would be more ideal for me. I am inexperienced in file management with apple and swift. Thanks!

And what if your directory has directories, and they have directories, etc.?

There is a way of doing that in Unix (Linux, FreeBSD, MacOSX, iOS, etc.) using the system library primitives, but, it's not fun.

1 Like

At the Unix i-node level, files and directories are the same. But, you have to go through the chain of i-nodes to do what you want to do. That's essentially what the directory contents that is returned in the list of URLs is doing at a high level.

If you're looking for an object that can represent a directory hierarchy, including the contents of all files, it sounds like you want FileWrapper.

You can initialize a FileWrapper with the URL of the top-level directory, then as for its serializedRepresentation to get a Data that is a snapshot of the entire directory hierarchy.

2 Likes

Yeah, that seems like what I am looking for. I will look into it and get back to you. Thanks for reading my intent : )

Alright, Seems like what I need to do is...

let fileWrapperInstance = FileWrapper(url: directory_url)
// I am assuming ReadingOptions: is optional

my_data = fileWrapperInstance.serializedRepresentation
// note it returns an optional which I will have to sort out.

When I get back to work I will check if this does the job. This solves my issue by allowing the 'wrapping' of both single files and directories if it works as I imagine it should and does so in two lines of code.

I appreciate the knowledge. At some point I should pick up a book on the different file systems out there.
Actually, you inspired me to begin writing up a google doc on all the different file systems and how they work for my own knowledge.