zen-fs / core

A filesystem, anywhere
https://zen-fs.github.io/core/
MIT License
103 stars 14 forks source link

Temporary directory and recursive removing #99

Closed matteo-cristino closed 3 weeks ago

matteo-cristino commented 3 weeks ago

Hi there, I'm new to zenFS and I'm testing it for the moment with node, but found out some interesting stuff that may be also of your interest.

This is the code I'm using

import { promises as fs } from "@zenfs/core";
import path from "path-browserify";

// if this is line commented out then fs.mkdtemp will return error:
// ErrnoError: ENOENT: No such file or directory, '/tmp'
// should this be created directly by fs.mkdtemp if /tmp is not found?
await fs.mkdir("/tmp");

const tmpdir = await fs.mkdtemp("random-folder-");
await fs.mkdir(path.join(tmpdir, "foo"));
await fs.writeFile(path.join(tmpdir, "foo", "test.txt"), "hello world!");

// This fails with
// ErrnoError: ENOTEMPTY: Directory is not empty, '/tmp/random-folder-1724408696549-5pjlwuyb46e/foo'
// seems that recursive options is not forwarded in the recursrive calls
await fs.rm(tmpdir, { recursive: true });

with @zenfs/core@0.16.3.

As written in the comment:

mho22 commented 3 weeks ago

I encountered the same recursivity problem with a different code. I also tried to implement a method to make this recursivity work :

function removeDirectoryRecursively( directory )
{
    if( fs.existsSync( directory ) )
    {
        let files = fs.readdirSync( directory, { withFileTypes : true } );

        files.forEach( file =>
        {
            const path = `${directory}/${file.path}`;

            const stat = fs.statSync( path );

            if( stat.isFile() )
            {
                fs.rmSync( path );
            }

            if( stat.isDirectory() )
            {
                removeDirectoryRecursively( path );
            }
        } );

        files = fs.readdirSync( directory, { withFileTypes : true } );

        if( files.length == 0 )
        {
            fs.rmdirSync( directory );
        }
    }
}

But even this is returning Directory is not empty for every directories to remove.

matteo-cristino commented 3 weeks ago

Opened PR #100 that should address the recursively removal

james-pre commented 3 weeks ago

Fixed in #100. Thanks a lot!