GNOME Bugzilla – Bug 581368
SMB doesn't set G_FILE_ATTRIBUTE_ACCESS_ file attributes
Last modified: 2018-09-21 16:44:28 UTC
Please describe the problem: I was trying to see if an smb uri was readable using the g_file_info_get_attribute_boolean method, works fine for sftp and local files, but fails for smb mounts Steps to reproduce: 1. See example code below 2. 3. Actual results: Returns false for file opened off of an smb share Expected results: should return true, works ok in nautilus Does this happen every time? yes, and on other system levels Other information: Example code with workaround file = g_file_new_for_uri(uri); if (file != NULL) { info = g_file_query_info(file, "*", G_FILE_QUERY_INFO_NONE, NULL, NULL); if (info != NULL) { // SMB filesystems seem to give incorrect access answers over GIO, // so if the file has a filesize > 0 we think it is not streaming if (g_ascii_strncasecmp(uri, "smb://", strlen("smb://")) == 0) { ret = (g_file_info_get_size(info) > 0) ? FALSE : TRUE; } else { ret = !g_file_info_get_attribute_boolean(info, G_FILE_ATTRIBUTE_ACCESS_CAN_READ); } g_object_unref(info); } else { ret = !g_file_test(uri, G_FILE_TEST_EXISTS); } } g_object_unref(file);
I bet this is because the smb backend doesn't support permissions. You'll probably see the same problem on the ftp backend. Try using g_file_info_has_attribute to make sure the attribute actually exists before reading the value.
From a quick look at the smb backend it's clear that the G_FILE_ATTRIBUTE_ACCESS_CAN_READ attribute is just not set, so a call to _get_attribute_boolean will naturally return FALSE *every time*. As Andreas already pointed out you need to check for if the attribute is present before using it. If the attribute is not set then this indicates (in most cases) that we just don't know better. So in case it is not set the best you can do is the best we would do: assume its readable until proven otherwise.
Although samba on linux supports POSIX permissions, I don't see a way how to retrieve them using libsmbclient call. The smbc_stat_fn() returns struct stat in some way but the st_mode field contains different information. To get real G_FILE_ATTRIBUTE_ACCESS_CAN_READ attribute, we would have to try opening each file and this is certainly nothing we want in the query_info() call. The other possibility would be to look at smbc_getxattr_fn() and smbc_listxattr_fn() to see if we can get any useful information.
See setup_stat() code, where st_mode is filled: https://github.com/samba-team/samba/blob/82801f9ec895deb9536a2b0a4e0ce4b3d5853220/source3/libsmb/libsmb_stat.c#L54 S_IRUSR is always set regardless of actual permissions. It seems to me that only S_IWUSR is reliable (for files only) as per the code and per my testing. See for details about DOS attributes: https://en.wikipedia.org/wiki/File_attribute#Types
I've changed the summary, because there is a problem with other G_FILE_ATTRIBUTE_ACCESS_ attribtues as well...
-- GitLab Migration Automatic Message -- This bug has been migrated to GNOME's GitLab instance and has been closed from further activity. You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.gnome.org/GNOME/gvfs/issues/93.