kannibalox / pyrosimple

An overhauled fork of the pyrocore tools for rTorrent
https://kannibalox.github.io/pyrosimple/
GNU General Public License v3.0
48 stars 5 forks source link

--cull deletes file on disc with error and not in rtorrent #39

Closed codedesperate closed 1 year ago

codedesperate commented 1 year ago

Hi, I am running the following rtcontrol command:

/home/user/.local/bin/rtcontrol 'message=/(.*torrent not registered with this tracker.*|.*Unregistered torrent.*)/' --select 1 --cull --quiet --yes

rtcontrol then gives me this error message:

2023-07-04 13:51:56 cull         torrent_name_here [tag_here]
Traceback (most recent call last):
  File "/home/user/.local/bin/rtcontrol", line 8, in <module>
    sys.exit(run())
  File "/home/user/.local/lib/python3.10/site-packages/pyrosimple/scripts/rtcontrol.py", line 973, in run
    RtorrentControl().run()
  File "/home/user/.local/lib/python3.10/site-packages/pyrosimple/scripts/base.py", line 177, in run
    self.mainloop()
  File "/home/user/.local/lib/python3.10/site-packages/pyrosimple/scripts/rtcontrol.py", line 870, in mainloop
    getattr(item, action["method"])(*args)
  File "/home/user/.local/lib/python3.10/site-packages/pyrosimple/torrent/rtorrent.py", line 566, in cull
    directory.rmdir()
  File "/usr/lib/python3.10/pathlib.py", line 1215, in rmdir
    self._accessor.rmdir(self)
PermissionError: [Errno 13] Permission denied: '/home/user/rtorrent/download/rtorrent_sonarr2'

I tried giving permissions 777, but I still got the error.

I then discovered that the file on disk in the path '/home/user/rtorrent/download/rtorrent_sonarr2/torrent_to_be_deleted' actually did get deleted, but it just did not get deleted in rtorrent.

The user running the rtcontrol command is also the user running the rtorrent process and is the owner of all the paths.

I don't understand why I get the permissions denied error when it does have the permissions, and it also deleted the torrent_file in path, but not in rtorrent which is run by the same user.

I experienced this on v.2.8.0. I just updated to v.2.9.0 and still facing this issue.

kannibalox commented 1 year ago

Unfortunately there's not a lot that rtcontrol can do if the OS decides that you're not allowed to delete the directory. However, keep in mind that when deleting a directory, you actually need write permissions on the parent directory, so if you can't rmdir /home/user/rtorrent/download/rtorrent_sonarr2/, check the permissions on /home/user/rtorrent/download/.

codedesperate commented 1 year ago

Hi @kannibalox , I tried 777 that path too and it didn't work.

But I really don't think it is a permission problem. Because rtcontrol did access the path, and it did delete the file /home/user/rtorrent/download/rtorrent_sonarr2/downloaded_file So I don't even understand why it is giving a permission error when it successfully deleted the file in that folder. The only thing it did not do, was to remove the torrent file from rtorrent (So it still shows up in rutorrent, but if I click force filecheck, it will also report the file missing as it did delete the file.

kannibalox commented 1 year ago

So /home/user/rtorrent/download/rtorrent_sonarr2/downloaded_file was cleaned up fine, but now rtcontrol is trying to clean up /home/user/rtorrent/download/rtorrent_sonarr2/ (the base directory), where it's getting the error. Can you successfully run rmdir /home/user/rtorrent/download/rtorrent_sonarr2/ as the same user as rtcontrol? That's all rtcontrol is trying to do, and it won't continue on to clean the item out of rtorrent until it doesn't encounter any exceptions.

As a silly workaround, rtcontrol will skip over any directories that aren't empty, so if you create an empty file, e.g. (touch /home/user/rtorrent/download/rtorrent_sonarr2/.keep), it should just continue past it.

codedesperate commented 1 year ago

Yes, the rmdir command works!

user@user:~/rtorrent/download$ rmdir /home/user/rtorrent/download/rtorrent_sonarr2/
rmdir: failed to remove '/home/user/rtorrent/download/rtorrent_sonarr2/': Directory not empty
user@user:~/rtorrent/download$

So then it's not permission issues right? And it is the same user running that rmdir command as the rtcontrol command. Really everything on this server runs as "user".

kannibalox commented 1 year ago

The Directory not empty error will be raised before the permissions check, would it be possible to try and rmdir that directory while it is empty? That should expose if there's still a permission problem.

Where I'm getting stuck on is that the core error isn't anything pyrosimple-specific, you should be able to replicate it directly with rmdir or the following command:

python -c "import pathlib; pathlib.Path('/home/user/rtorrent/download/rtorrent_sonarr2').rmdir()"
codedesperate commented 1 year ago

I see, the core error seems strange indeed. And I appreciate any help you are willing to give!

Running: python -c "import pathlib; pathlib.Path('/home/user/rtorrent/download/rtorrent_sonarr2').rmdir()" gave the following error:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
ImportError: No module named pathlib

I then tried emptying the library to run rmdir /home/user/rtorrent/download/rtorrent_sonarr2/on. I do indeed get rmdir: failed to remove 'rtorrent_sonarr2': Permission deniedand I have absolutely no idea why.

even with the 777 perms on /home/user/rtorrent/download/ and on /home/user/rtorrent/download/rtorrent_sonarr2 I get permission denied.

Just guessing now, but I'm not sure if it has any impact that /home/user/rtorrent/download/is a mergerfs filesystem. Though rtcontrol works just fine on the library /home/user/rtorrent/download/rtorrent_radarr.

codedesperate commented 1 year ago

Thank you for the help once again @kannibalox I figured it out :D

It was indeed mergerfs that tricked me. All path's was owned by correct user, with the exception of one of the SSD's in my mergerfs where the base folder /mnt/ssd2 the disk was mounted on was root:root...

Thank you making me realise this :)