Advice for copy/move file extension
Posted: 09 Nov 2019 18:38
I'm trying to make a vlc lua extension that can delete, move or copy the current played file. Now I'm somewhat stuck and was wondering if someone can give advice.
I have developed the delete using vlc.io.unlink. For the copy I use vlc.io.open with option wb and write the file. This works all very well.
However there are some downsides.
1. vlc.io.unlink doesn't use the recycle bin.
2. When copying a file using vlc.io.open, the created and modified timestamps are new.
3. When moving a file, a copy + delete of original works, but takes for larger files more time than a 'real move' in Windows Explorer when moved on the same disk.
Now for the solutions for these (on Windows only) I have:
1. I can live with that.
2. Using os.execute and xcopy Windows commands.
3. Using os.execute and move Windows command. Here some convoluted logic is needed to make it work for filenames with special characters, but it is possible.
However now I face another problem. Apparently, vlc crashes when os.execute takes more than about 5 seconds. For copying larger files, this problem can occur. Now I have following solutions:
i) I can start a new command prompt in os.execute or use io.popen instead so the call returns immediately. This solves the problem. But a new problem arises: I need to wait for the result of the copy to know if it is successful. Now I can
* When using io.popen, I can read the output. But this has the same problem that vlc crashes if the copy takes more than about 5 seconds.
* Output the result of the copy to a temporary file. However then I need a polling mechanism to wait for the temporary file (result). With vlc lua extensions, this seems only possible using a busy-wait or opening command prompts with e.g. a ping that takes one second until the copy is finished. These solutions are not very good.
ii) Another solution is maybe trying robocopy instead of xcopy. I didn't do this yet, but I think because robocopy outputs a progress during the copying, if I use io.popen and then in some way read the progress while it happens, this should maybe not be vulnerable to the vlc crash problem and avoid the busy-wait problem. The problem however is that robocopy has no possibility to rename the copied file. Since I also want to be able to automatically rename the copied file in case the file already exists, I cannot use robocopy. Xcopy can rename a copied file, but doesn't report a progress during the copy. I could use robocopy to copy it to some temporary subfolder first, and than use 'rename' and 'move' Windows commands to move it to the correct directory, which should be fast enough to not cause the crash problem. But that solution seems extremely convoluted, and I'm not sure at this point if using robocopy is actually a solution.
So I'm wondering
1. Is there anyone who knows a non-convoluted solution to counter the mentioned problems on top? Best involving not using os.execute or io.popen I think. Or if not possible to avoid this, a good solution to avoid a busy-wait during the copy.
2. Is it a bug that vlc crashes on os.execute or some other call (like file:read('*all') where file is a handle from io.popen) if this takes more than 5 seconds?
I have developed the delete using vlc.io.unlink. For the copy I use vlc.io.open with option wb and write the file. This works all very well.
However there are some downsides.
1. vlc.io.unlink doesn't use the recycle bin.
2. When copying a file using vlc.io.open, the created and modified timestamps are new.
3. When moving a file, a copy + delete of original works, but takes for larger files more time than a 'real move' in Windows Explorer when moved on the same disk.
Now for the solutions for these (on Windows only) I have:
1. I can live with that.
2. Using os.execute and xcopy Windows commands.
3. Using os.execute and move Windows command. Here some convoluted logic is needed to make it work for filenames with special characters, but it is possible.
However now I face another problem. Apparently, vlc crashes when os.execute takes more than about 5 seconds. For copying larger files, this problem can occur. Now I have following solutions:
i) I can start a new command prompt in os.execute or use io.popen instead so the call returns immediately. This solves the problem. But a new problem arises: I need to wait for the result of the copy to know if it is successful. Now I can
* When using io.popen, I can read the output. But this has the same problem that vlc crashes if the copy takes more than about 5 seconds.
* Output the result of the copy to a temporary file. However then I need a polling mechanism to wait for the temporary file (result). With vlc lua extensions, this seems only possible using a busy-wait or opening command prompts with e.g. a ping that takes one second until the copy is finished. These solutions are not very good.
ii) Another solution is maybe trying robocopy instead of xcopy. I didn't do this yet, but I think because robocopy outputs a progress during the copying, if I use io.popen and then in some way read the progress while it happens, this should maybe not be vulnerable to the vlc crash problem and avoid the busy-wait problem. The problem however is that robocopy has no possibility to rename the copied file. Since I also want to be able to automatically rename the copied file in case the file already exists, I cannot use robocopy. Xcopy can rename a copied file, but doesn't report a progress during the copy. I could use robocopy to copy it to some temporary subfolder first, and than use 'rename' and 'move' Windows commands to move it to the correct directory, which should be fast enough to not cause the crash problem. But that solution seems extremely convoluted, and I'm not sure at this point if using robocopy is actually a solution.
So I'm wondering
1. Is there anyone who knows a non-convoluted solution to counter the mentioned problems on top? Best involving not using os.execute or io.popen I think. Or if not possible to avoid this, a good solution to avoid a busy-wait during the copy.
2. Is it a bug that vlc crashes on os.execute or some other call (like file:read('*all') where file is a handle from io.popen) if this takes more than 5 seconds?