afero icon indicating copy to clipboard operation
afero copied to clipboard

GetTempDir() not compatible with MemMapFS

Open jcrsilva opened this issue 3 years ago • 1 comments

I ran into this one while debugging a few tests.

I was under the assumption that the method GetTempDir would return a valid temporary diretory for use in any FS, as it receives the fs as an argument:

func GetTempDir(fs Fs, subPath string) string

However it seems that in the implementation the os package is always called:

dir := addSlash(os.TempDir())

Which results in the default temp directory for the system being returned, even when the filesystem in use is MemMapFS.

In my case (MacOS) this results in something like "/var/folders/hb/_83nhz590h95ss_ygd_lpyq00000gn/T/" being returned, which then fails when you call:

exists, err := afero.Exists(fs, path)

As this directory only exists on the host and not in the memory-backed FS.

My suggestions to fix this are:

  1. Accept this is the default behavior and document it;
  2. Return err when this is called on MemMapFS;
  3. When a new MemMapFS is initialized, also create a default, known temporary directory and return it in the method.

Happy to help implementing any of these.

jcrsilva avatar Nov 26 '20 11:11 jcrsilva

This should be abstracted to make it possible to add different behaviors based on the kind of storage being used.

e200 avatar Jun 07 '23 12:06 e200