Fixed part of bug 556 (nautilus_file_can_rename returns

TRUE for /root, but shouldn't). Only remaining issue is
	the no-nautilus-file-for-/ problem, which is already written
	up separately.

	Fixed bug 546 (Folders/files without read permissions should
	not be viewable).

	* libnautilus-extensions/nautilus-file.h,
	* libnautilus-extensions/nautilus-file.c:
	(nautilus_file_denies_access_permission): New private
	function that checks permissions, including checking
	whether the current user is the owner or in the group
	of the queried file. If permissions aren't valid, returns
	FALSE.
	(nautilus_file_can_read), (nautilus_file_can_write),
	(nautilus_file_can_execute): New public functions that
	use nautilus_file_denies_access_permission to determine
	which access permissions the current user has. They return
	TRUE if permissions aren't valid, so callers still have to
	be prepared to handle failures.
	(nautilus_file_can_rename): Now calls nautilus_file_can_write
	on the parent directory. Formerly it was just checking the
	permission flags directly, ignoring whether the current user
	was the owner or not. This fixes part of bug 556.

	* src/ntl-window-msgs.c:
	(handle_unreadable_location): New private function, puts up
	an alert if the user tries to switch to an unreadable location.
	(nautilus_window_request_location_change): Try
	handle_unreadable_location before starting up the state transition.
	This fixes bug 546.

	* src/file-manager/fm-error-reporting.c:
	(fm_report_error_renaming_file): Add missing _()'s around messages.
This commit is contained in:
John Sullivan 2000-04-25 18:11:57 +00:00
parent 52bba6353b
commit c9f5361b75
8 changed files with 387 additions and 33 deletions

View file

@ -1,3 +1,41 @@
2000-04-25 John Sullivan <sullivan@eazel.com>
Fixed part of bug 556 (nautilus_file_can_rename returns
TRUE for /root, but shouldn't). Only remaining issue is
the no-nautilus-file-for-/ problem, which is already written
up separately.
Fixed bug 546 (Folders/files without read permissions should
not be viewable).
* libnautilus-extensions/nautilus-file.h,
* libnautilus-extensions/nautilus-file.c:
(nautilus_file_denies_access_permission): New private
function that checks permissions, including checking
whether the current user is the owner or in the group
of the queried file. If permissions aren't valid, returns
FALSE.
(nautilus_file_can_read), (nautilus_file_can_write),
(nautilus_file_can_execute): New public functions that
use nautilus_file_denies_access_permission to determine
which access permissions the current user has. They return
TRUE if permissions aren't valid, so callers still have to
be prepared to handle failures.
(nautilus_file_can_rename): Now calls nautilus_file_can_write
on the parent directory. Formerly it was just checking the
permission flags directly, ignoring whether the current user
was the owner or not. This fixes part of bug 556.
* src/ntl-window-msgs.c:
(handle_unreadable_location): New private function, puts up
an alert if the user tries to switch to an unreadable location.
(nautilus_window_request_location_change): Try
handle_unreadable_location before starting up the state transition.
This fixes bug 546.
* src/file-manager/fm-error-reporting.c:
(fm_report_error_renaming_file): Add missing _()'s around messages.
2000-04-25 Darin Adler <darin@eazel.com>
* libnautilus-extensions/nautilus-directory-async.c

View file

@ -27,6 +27,7 @@
#include <grp.h>
#include <pwd.h>
#include <unistd.h>
#include <gtk/gtksignal.h>
@ -279,6 +280,127 @@ get_file_for_parent_directory (NautilusFile *file)
return result;
}
/**
* nautilus_file_denies_access_permission:
*
* Check whether the current file does not have a given permission
* for the current user. The sense is negative because the function
* returns FALSE if permissions cannot be determined.
*
* @file: The file to check.
* @owner_permission: The USER version of the permission (e.g. GNOME_VFS_PERM_USER_READ).
* @group_permission: The GROUP version of the permission (e.g. GNOME_VFS_PERM_GROUP_READ).
* @other_permission: The OTHER version of the permission (e.g. GNOME_VFS_PERM_OTHER_READ).
*
* Return value: TRUE if the current user definitely does not have
* the specified permission. FALSE if the current user does have
* permission, or if the permissions themselves are not queryable.
*/
static gboolean
nautilus_file_denies_access_permission (NautilusFile *file,
GnomeVFSFilePermissions owner_permission,
GnomeVFSFilePermissions group_permission,
GnomeVFSFilePermissions other_permission)
{
g_assert (NAUTILUS_IS_FILE (file));
if ((file->details->info->valid_fields & GNOME_VFS_FILE_INFO_FIELDS_PERMISSIONS) == 0) {
/*
* File's permissions field is not valid.
* Can't access specific permissions, so return FALSE.
*/
return FALSE;
}
/* Check whether anyone at all has permission. */
if (file->details->info->permissions & other_permission) {
return FALSE;
}
/* Check whether user's ID matches file's. */
if ((file->details->info->permissions & owner_permission)
&& getuid() == file->details->info->uid) {
return FALSE;
}
/* Check whether user's group ID matches file's. */
if ((file->details->info->permissions & group_permission)
&& getpwuid (getuid())->pw_gid == file->details->info->gid) {
return FALSE;
}
return TRUE;
}
/**
* nautilus_file_can_read:
*
* Check whether the user is allowed to read the contents of this file.
*
* @file: The file to check.
*
* Return value: FALSE if the user is definitely not allowed to read
* the contents of the file. If the user has read permission, or
* the code can't tell whether the user has read permission,
* returns TRUE (so failures must always be handled).
*/
gboolean
nautilus_file_can_read (NautilusFile *file)
{
g_return_val_if_fail (NAUTILUS_IS_FILE (file), FALSE);
return !nautilus_file_denies_access_permission (file,
GNOME_VFS_PERM_USER_READ,
GNOME_VFS_PERM_GROUP_READ,
GNOME_VFS_PERM_OTHER_READ);
}
/**
* nautilus_file_can_write:
*
* Check whether the user is allowed to write to this file.
*
* @file: The file to check.
*
* Return value: FALSE if the user is definitely not allowed to write
* to the file. If the user has write permission, or
* the code can't tell whether the user has write permission,
* returns TRUE (so failures must always be handled).
*/
gboolean
nautilus_file_can_write (NautilusFile *file)
{
g_return_val_if_fail (NAUTILUS_IS_FILE (file), FALSE);
return !nautilus_file_denies_access_permission (file,
GNOME_VFS_PERM_USER_WRITE,
GNOME_VFS_PERM_GROUP_WRITE,
GNOME_VFS_PERM_OTHER_WRITE);
}
/**
* nautilus_file_can_execute:
*
* Check whether the user is allowed to execute this file.
*
* @file: The file to check.
*
* Return value: FALSE if the user is definitely not allowed to execute
* the file. If the user has execute permission, or
* the code can't tell whether the user has execute permission,
* returns TRUE (so failures must always be handled).
*/
gboolean
nautilus_file_can_execute (NautilusFile *file)
{
g_return_val_if_fail (NAUTILUS_IS_FILE (file), FALSE);
return !nautilus_file_denies_access_permission (file,
GNOME_VFS_PERM_USER_EXEC,
GNOME_VFS_PERM_GROUP_EXEC,
GNOME_VFS_PERM_OTHER_EXEC);
}
/**
* nautilus_file_can_rename:
*
@ -295,29 +417,26 @@ gboolean
nautilus_file_can_rename (NautilusFile *file)
{
NautilusFile *parent;
gboolean result;
g_return_val_if_fail (NAUTILUS_IS_FILE (file), FALSE);
/* User must have write permissions for the parent directory. */
parent = get_file_for_parent_directory (file);
/*
* No parent directory for some reason (at root level?).
* Can't tell whether this file is renameable, so return TRUE.
*/
if (parent == NULL) {
/*
* No parent directory for some reason (at root level?).
* Can't tell whether this file is renameable, so return TRUE.
*/
return TRUE;
}
if ((parent->details->info->valid_fields & GNOME_VFS_FILE_INFO_FIELDS_PERMISSIONS) == 0){
/*
* Parent's permissions field is not valid.
* Can't tell whether this file is renameable, so return TRUE.
*/
return TRUE;
}
result = nautilus_file_can_write (parent);
/* Trust the write permissions of the parent directory. */
return (file->details->info->permissions & GNOME_VFS_PERM_USER_WRITE) != 0;
nautilus_file_unref (parent);
return result;
}
GnomeVFSResult

View file

@ -113,6 +113,10 @@ void nautilus_file_set_keywords (NautilusFile
GList * nautilus_file_get_emblem_names (NautilusFile *file);
/* Basic operations for file objects. */
gboolean nautilus_file_can_read (NautilusFile *file);
gboolean nautilus_file_can_write (NautilusFile *file);
gboolean nautilus_file_can_execute (NautilusFile *file);
gboolean nautilus_file_can_rename (NautilusFile *file);
GnomeVFSResult nautilus_file_rename (NautilusFile *file,
const char *new_name);

View file

@ -27,6 +27,7 @@
#include <grp.h>
#include <pwd.h>
#include <unistd.h>
#include <gtk/gtksignal.h>
@ -279,6 +280,127 @@ get_file_for_parent_directory (NautilusFile *file)
return result;
}
/**
* nautilus_file_denies_access_permission:
*
* Check whether the current file does not have a given permission
* for the current user. The sense is negative because the function
* returns FALSE if permissions cannot be determined.
*
* @file: The file to check.
* @owner_permission: The USER version of the permission (e.g. GNOME_VFS_PERM_USER_READ).
* @group_permission: The GROUP version of the permission (e.g. GNOME_VFS_PERM_GROUP_READ).
* @other_permission: The OTHER version of the permission (e.g. GNOME_VFS_PERM_OTHER_READ).
*
* Return value: TRUE if the current user definitely does not have
* the specified permission. FALSE if the current user does have
* permission, or if the permissions themselves are not queryable.
*/
static gboolean
nautilus_file_denies_access_permission (NautilusFile *file,
GnomeVFSFilePermissions owner_permission,
GnomeVFSFilePermissions group_permission,
GnomeVFSFilePermissions other_permission)
{
g_assert (NAUTILUS_IS_FILE (file));
if ((file->details->info->valid_fields & GNOME_VFS_FILE_INFO_FIELDS_PERMISSIONS) == 0) {
/*
* File's permissions field is not valid.
* Can't access specific permissions, so return FALSE.
*/
return FALSE;
}
/* Check whether anyone at all has permission. */
if (file->details->info->permissions & other_permission) {
return FALSE;
}
/* Check whether user's ID matches file's. */
if ((file->details->info->permissions & owner_permission)
&& getuid() == file->details->info->uid) {
return FALSE;
}
/* Check whether user's group ID matches file's. */
if ((file->details->info->permissions & group_permission)
&& getpwuid (getuid())->pw_gid == file->details->info->gid) {
return FALSE;
}
return TRUE;
}
/**
* nautilus_file_can_read:
*
* Check whether the user is allowed to read the contents of this file.
*
* @file: The file to check.
*
* Return value: FALSE if the user is definitely not allowed to read
* the contents of the file. If the user has read permission, or
* the code can't tell whether the user has read permission,
* returns TRUE (so failures must always be handled).
*/
gboolean
nautilus_file_can_read (NautilusFile *file)
{
g_return_val_if_fail (NAUTILUS_IS_FILE (file), FALSE);
return !nautilus_file_denies_access_permission (file,
GNOME_VFS_PERM_USER_READ,
GNOME_VFS_PERM_GROUP_READ,
GNOME_VFS_PERM_OTHER_READ);
}
/**
* nautilus_file_can_write:
*
* Check whether the user is allowed to write to this file.
*
* @file: The file to check.
*
* Return value: FALSE if the user is definitely not allowed to write
* to the file. If the user has write permission, or
* the code can't tell whether the user has write permission,
* returns TRUE (so failures must always be handled).
*/
gboolean
nautilus_file_can_write (NautilusFile *file)
{
g_return_val_if_fail (NAUTILUS_IS_FILE (file), FALSE);
return !nautilus_file_denies_access_permission (file,
GNOME_VFS_PERM_USER_WRITE,
GNOME_VFS_PERM_GROUP_WRITE,
GNOME_VFS_PERM_OTHER_WRITE);
}
/**
* nautilus_file_can_execute:
*
* Check whether the user is allowed to execute this file.
*
* @file: The file to check.
*
* Return value: FALSE if the user is definitely not allowed to execute
* the file. If the user has execute permission, or
* the code can't tell whether the user has execute permission,
* returns TRUE (so failures must always be handled).
*/
gboolean
nautilus_file_can_execute (NautilusFile *file)
{
g_return_val_if_fail (NAUTILUS_IS_FILE (file), FALSE);
return !nautilus_file_denies_access_permission (file,
GNOME_VFS_PERM_USER_EXEC,
GNOME_VFS_PERM_GROUP_EXEC,
GNOME_VFS_PERM_OTHER_EXEC);
}
/**
* nautilus_file_can_rename:
*
@ -295,29 +417,26 @@ gboolean
nautilus_file_can_rename (NautilusFile *file)
{
NautilusFile *parent;
gboolean result;
g_return_val_if_fail (NAUTILUS_IS_FILE (file), FALSE);
/* User must have write permissions for the parent directory. */
parent = get_file_for_parent_directory (file);
/*
* No parent directory for some reason (at root level?).
* Can't tell whether this file is renameable, so return TRUE.
*/
if (parent == NULL) {
/*
* No parent directory for some reason (at root level?).
* Can't tell whether this file is renameable, so return TRUE.
*/
return TRUE;
}
if ((parent->details->info->valid_fields & GNOME_VFS_FILE_INFO_FIELDS_PERMISSIONS) == 0){
/*
* Parent's permissions field is not valid.
* Can't tell whether this file is renameable, so return TRUE.
*/
return TRUE;
}
result = nautilus_file_can_write (parent);
/* Trust the write permissions of the parent directory. */
return (file->details->info->permissions & GNOME_VFS_PERM_USER_WRITE) != 0;
nautilus_file_unref (parent);
return result;
}
GnomeVFSResult

View file

@ -113,6 +113,10 @@ void nautilus_file_set_keywords (NautilusFile
GList * nautilus_file_get_emblem_names (NautilusFile *file);
/* Basic operations for file objects. */
gboolean nautilus_file_can_read (NautilusFile *file);
gboolean nautilus_file_can_write (NautilusFile *file);
gboolean nautilus_file_can_execute (NautilusFile *file);
gboolean nautilus_file_can_rename (NautilusFile *file);
GnomeVFSResult nautilus_file_rename (NautilusFile *file,
const char *new_name);

View file

@ -26,6 +26,8 @@
#include <config.h>
#include "fm-error-reporting.h"
#include <libgnome/gnome-defs.h>
#include <libgnome/gnome-i18n.h>
#include <libgnomeui/gnome-messagebox.h>
#include <libgnomeui/gnome-stock.h>
@ -41,19 +43,17 @@ fm_report_error_renaming_file (const char *original_name,
switch (error) {
case GNOME_VFS_ERROR_FILEEXISTS:
message = g_strdup_printf ("The name \"%s\" is already used in this directory.\nPlease use a different name.",
message = g_strdup_printf (_("The name \"%s\" is already used in this directory.\nPlease use a different name."),
new_name);
break;
case GNOME_VFS_ERROR_ACCESSDENIED:
message = g_strdup_printf ("You do not have the right permissions to rename\"%s.\"", original_name);
message = g_strdup_printf (_("You do not have the right permissions to rename \"%s.\""), original_name);
break;
default:
/*
* We should invent decent error messages for every case we actually experience.
* If you hit this assert, please tell John Sullivan (sullivan@eazel.com).
*/
/* We should invent decent error messages for every case we actually experience. */
g_warning ("Hit unhandled case %d in fm_report_error_renaming_file, tell sullivan@eazel.com", error);
message = g_strdup_printf ("Sorry, couldn't rename \"%s\" to \"%s\".", original_name, new_name);
message = g_strdup_printf (_("Sorry, couldn't rename \"%s\" to \"%s\"."), original_name, new_name);
}
message_box = gnome_message_box_new (message,

View file

@ -598,12 +598,47 @@ nautilus_window_load_meta_view(NautilusWindow *window,
return meta_view;
}
static gboolean
handle_unreadable_location (NautilusWindow *window, const char *uri) {
NautilusFile *file;
gboolean unreadable;
char *file_name;
char *message;
file = nautilus_file_get (uri);
/* Can't make file object; can't check permissions; can't determine
* whether file is readable so return FALSE.
*/
if (file == NULL) {
return FALSE;
}
unreadable = !nautilus_file_can_read (file);
nautilus_file_unref (file);
if (unreadable) {
file_name = nautilus_file_get_name (file);
message = g_strdup_printf (_("You do not have the right permissions to view \"%s.\""), file_name);
gtk_widget_show (gnome_error_dialog_parented (message,
GTK_WINDOW (window)));
g_free (file_name);
g_free (message);
}
return unreadable;
}
void
nautilus_window_request_location_change (NautilusWindow *window,
Nautilus_NavigationRequestInfo *loc,
NautilusView *requesting_view)
{
NautilusWindow *new_window;
if (handle_unreadable_location (window, loc->requested_uri)) {
return;
}
if (loc->new_window_requested) {
new_window = nautilus_app_create_window (NAUTILUS_APP(window->app));

View file

@ -598,12 +598,47 @@ nautilus_window_load_meta_view(NautilusWindow *window,
return meta_view;
}
static gboolean
handle_unreadable_location (NautilusWindow *window, const char *uri) {
NautilusFile *file;
gboolean unreadable;
char *file_name;
char *message;
file = nautilus_file_get (uri);
/* Can't make file object; can't check permissions; can't determine
* whether file is readable so return FALSE.
*/
if (file == NULL) {
return FALSE;
}
unreadable = !nautilus_file_can_read (file);
nautilus_file_unref (file);
if (unreadable) {
file_name = nautilus_file_get_name (file);
message = g_strdup_printf (_("You do not have the right permissions to view \"%s.\""), file_name);
gtk_widget_show (gnome_error_dialog_parented (message,
GTK_WINDOW (window)));
g_free (file_name);
g_free (message);
}
return unreadable;
}
void
nautilus_window_request_location_change (NautilusWindow *window,
Nautilus_NavigationRequestInfo *loc,
NautilusView *requesting_view)
{
NautilusWindow *new_window;
if (handle_unreadable_location (window, loc->requested_uri)) {
return;
}
if (loc->new_window_requested) {
new_window = nautilus_app_create_window (NAUTILUS_APP(window->app));