Directory listing fails if name contains the "&" character
| Project: | Fileshare |
| Version: | HEAD |
| Component: | Module |
| Category: | bug report |
| Priority: | normal |
| Assigned: | Unassigned |
| Status: | patch (code needs review) |
It looks like the opendir() function used to list the contents of a directory cannot deal with directory names containing the "&" sign.
In my case, we have a filestore that people can access via WebDAV or via the fileshare module. By setting the environment on the server to UTF-8, I have special characters working fine in DAV and Drupal. The only character that seems to cause problems is the "&" sign in directory names.
I get the following errors in this case:
* warning: opendir(/var/www/fileshare/ORG_Filestore1/Other NGOs ): failed to open dir: No such file or directory in /var/www/htdocs/intranet.hidden.org.uk/modules/fileshare/fileshare.module on line 650.
* warning: readdir(): supplied argument is not a valid Directory resource in /var/www/htdocs/intranet.hidden.org.uk/modules/fileshare/fileshare.module on line 651.
* warning: sort() expects parameter 1 to be array, null given in /var/www/htdocs/intranet.hidden.org.uk/modules/fileshare/fileshare.module on line 654.
* warning: closedir(): supplied argument is not a valid Directory resource in /var/www/htdocs/intranet.hidden.org.uk/modules/fileshare/fileshare.module on line 708.
The directory is question is actually named "Other NGOs & Institutions".
While I know it is not good practice to have the "&" sign in a filename on *NIX platforms, it is quite common is other environments (i.e. Windows), so I think it would be good to fix this one. It looks to me like some escaping needs to be done on the directory name on line 650 of the module, but I am not quite sure what the function should be. If someone could point me in the right direction, I'll try to fix it and submit a patch.

#1
I also ran into this problem. I set up a small website for an elementary school where teachers could shrare files and post information. It appears that one teacher did something (I don't know what) and now I get these errors a over and over on the site. In fact, he said he was adding files and then he got the errors and then it deleted all his files and directories he had created via the web interface.
fileshare.module,v 1.12.2.12
I'm going to get the latest code and update the module. But in the meantime, the errors still pop up randomly...sometimes they are there and sometimes they are not.
#2
Sorry, one more thing. It appears that the actual directory on the file server was deleted as well, so the fileshare node was referencing a directory that no longer existed.
#3
I had this same problem last night on HEAD. We just got PHOP safe mode turned off by our Hosting provider. I had gotten the directory structure put together for the post and uploaded a bunch of pdf filed to the various sub directories. suddenly I started getting the dir error. The post was still there but it was showing no directories in it. I ftped in to my site and the directories for the hole fileshare post were gone with all the PDF files. I had to recreate the whole thing. the only thing I could find that happened at the same time (Could be coincidence), the site sent out its post update email to every one (I would say with in the same minute as it happened probably a lot less)
Justin
#4
I tried adding something like this to the opendir function, but it didn't seem to work... Ideas?
<?phpfunction _escape_unix($str) {
$special_chars = array("'","\"","*","?","[","-","]","{","}","~","$","!","&",";","(",")","<",">","|","#","@");
$escaped = array("\\'","\\\"","\\*","\\?","\\[","\\-","\\]","\\{","\\}","\\~","\\$","\\!","\\&","\\;","\\(","\\)","\\<","\\>","\\|","\\#","\\@");
return str_replace($special_chars,$escaped,$str);
}
?>
#5
Appears to be a missing urlencode in 'fileshare.module'. Appears up to HEAD. Trivial patch attached for HEAD.
The patch allows viewing the contents of directories with ampersand ('&') characters in them.
Only tested under ubuntu edgy / apache 2.0.55 / php 5.1.6 with DRUPAL-5-1 and HEAD.
Successfully tested file download, subdirectory views, file uploads, subdirectory creation, and deletion of files/subdirectories under a directory with an ampersand in the name.
I do feel that the true fix is to add full (and optional) support for all valid characters supported by the current host OS for files and directories.
~Damian
#6
Go figure I'd upload the wrong patch :) This one is nearly identical.. just also includes the "$dir . '/' ." in the urlencode.
#7
Update: The above patch dealing with ampersand ('&') characters (and possibly others) has been committed to HEAD.
Thank you Jamie!
~Damian
#8
Correction: that is DRUPAL-5, not HEAD.
( I will learn to pay attention... I will learn to pay attention... )
#9
Where else does this need to be applied?
#10
I'm not sure if this is relevant, but I noticed that a file name containing a single quote (') can't be deleted because it causes a javascript error.
#11
I found that if the folder also contains a '&' then this causes an error.
I tried to look through the module code to see where i could apply the urlencode function call but it wasn't apparent.
Any idea where i might find that?
#12
Hi,
Thanks for the patch, Damian. I just applied it to version 5.x-1.0 and it has fixed the problems with directories that contain the '&' character. It should probably solve problems with other special characters as well. I'm not sure about how to solve the problem in general, as you say the best thing would be to support all characters that are supported by the underlying file system.
-Ian
#13
Hi,
I've downloaded the dev version of the module, which I believe includes this patch, and it's working well when your main fileshare directory has an ampersand in the name; however, I'm still having this problem with subdirectories. When I click them, they do not give me a file list. I'm quite sure there's somewhere else you need to add urlencode(), but I can't seem to find it.
Thanks!
#14
It looks as though the character escaping is not totally working.
A user put this as a folder name: Research_and_Evidence_Task_Force_--"R&E"_--_Group_2
It caused the opendir() function error. The folder also could not be deleted. As a workaround I went in with an ftp program to rename the folder without the "R&E" and that allowed the folder to then be deleted.