Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Ability to delete non-empty folder. #8197

Closed
wants to merge 5 commits into from

Conversation

n1tr0xs
Copy link
Contributor

@n1tr0xs n1tr0xs commented Aug 16, 2024

Fixes #1823 .

Description of the problem being solved:

Added the ability to delete a folder, and all builds stored within the folder.

Steps taken to verify a working solution:

  • Create folder
  • Create build in folder
  • Delete folder

Before screenshot:

image

After screenshot:

image

@Paliak Paliak added the user-interface Changes that only affect the UI label Aug 16, 2024
@Paliak
Copy link
Contributor

Paliak commented Aug 16, 2024

Not sure i'm a fan of using os.execute. Especially with user input.

If you want to implement this i'd say recursively delete files and directories with RemoveDir, NewFileSearch and os.remove Something like (not tested):

function deleteDirectory(dir)
    local function removeContents(path)
        local search = NewFileSearch(path .. "/*")
        while search:NextFile() do
            local item = path .. "/" .. search:GetFileName()
            if search:IsDirectory() then
                if search:GetFileName() ~= "." and search:GetFileName() ~= ".." then
                    removeContents(item)
                    RemoveDir(item)
                end
            else
                os.remove(item)
            end
        end
    end

    removeContents(dir)
    RemoveDir(dir)
end

@zao
Copy link
Contributor

zao commented Aug 16, 2024

I'm also not super keen on leveraging os.system as the command lines are both unportable and how it's non-trivial to robustly escape arguments in the command line for arbitrary filenames.
The iteration solution is better, but could lead to surprises as entries are removed as we sweep.

A possible alternative if we really want such a powerful primitive is that we could expose std::filesystem::remove_all in SimpleGraphic, either as an option to RemoveDir or as a separate function.

@n1tr0xs
Copy link
Contributor Author

n1tr0xs commented Aug 16, 2024

Could you check it out in your free time ?

@n1tr0xs
Copy link
Contributor Author

n1tr0xs commented Aug 16, 2024

And i have an idea, but i'm not sure about this practice in LUA.
To avoid repetative lines we can use this code:

function deleteDirectory(dir)
    local funcs = {}
    funcs[false] = os.remove -- delete file
    funcs[true] = deleteDirectory -- delete directory

    for k, v in pairs(funcs) do
        local search = NewFileSearch(dir..'/*', k)
        if search then
            funcs[k](dir..'/'..search:GetFileName())
            while search:NextFile() do
                funcs[k](dir..'/'..search:GetFileName())
            end
        end
    end

    RemoveDir(dir)
end

What do you think about this ?

@Paliak
Copy link
Contributor

Paliak commented Aug 16, 2024

I think adding support for recursive folder deletion from the SG side would be the best way to tackle this feature.

The method you implemented suffers from the issue that zao mentioned. You're changing the environment from underneath an iterator which is generally not a good idea.

https://en.cppreference.com/w/cpp/filesystem/directory_iterator

If a file or a directory is deleted or added to the directory tree after the directory iterator has been created, it is unspecified whether the change would be observed through the iterator.

Imo if you want to stick to the lua side you'd need to first make a list of all the elements to be deleted and then delete them in the right order. You'd probably be looking at a DFS kinda approach.

@n1tr0xs
Copy link
Contributor Author

n1tr0xs commented Aug 18, 2024

Yeah... It seems that things are much more complicated on the LUA side.

@n1tr0xs n1tr0xs closed this Aug 18, 2024
Paliak added a commit to Paliak/PathOfBuilding-SimpleGraphic that referenced this pull request Aug 18, 2024
Current implementation of RemoveDir does not allow for removal of
direcoties with content. This commit adds a recursive boolean parameter
to RemoveDir allowing for recursive removal of directories and their
contents.

PathOfBuildingCommunity/PathOfBuilding#8197
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
user-interface Changes that only affect the UI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Ability to delete Occupied Folders
3 participants