Adding SugarExt for setting hidden attribute on FAT files

This extension is used by Sugar to set the HIDDEN attribute for
.Sugar-Metadata. By creating an extension, we avoid the need to add an
additional dependency (fatattr) to Sugar. The code is modeled after
fatattr, using an IOCTL call get and set FAT file attributes.

There is a corresponding patch for model.py in sugar/src/jarabe/journal
This commit is contained in:
Walter Bender 2013-06-08 07:47:37 -04:00 committed by Daniel Narvaez
parent 9396b09ee4
commit 3e5c37f3f8
3 changed files with 129 additions and 0 deletions

View File

@ -70,6 +70,8 @@ libsugarext_la_SOURCES = \
gsm-session.h \
gsm-xsmp.c \
gsm-xsmp.h \
sugar-fatattr.c \
sugar-fatattr.h \
sugar-grid.c \
sugar-grid.h \
sugar-cursor-tracker.c \
@ -121,6 +123,8 @@ SugarExt_1_0_gir_FILES = \
gsm-session.h \
gsm-xsmp.c \
gsm-xsmp.h \
sugar-fatattr.c \
sugar-fatattr.h \
sugar-cursor-tracker.c \
sugar-cursor-tracker.h \
sugar-gesture-grabber.c \

View File

@ -0,0 +1,96 @@
/*
* Copyright (C) 2013, Walter Bender
* Copyright (C) 2009 Aaron Stone <aaron@serendipity.cx>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
// Gives us O_NOATIME
#define _GNU_SOURCE
#include <sys/types.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <linux/msdos_fs.h>
#include <glib.h>
#include <errno.h>
#include <stdio.h>
// This function is wrapped by getattrs/setattrs
static int _ioctl_attrs(char *file, __u32 *attrs, int ioctlnum, char *verb)
{
int fd;
// Interesting, we don't need a read-write handle to call the SET ioctl.
fd = open(file, O_RDONLY | O_NOATIME);
if (fd < 0) {
fprintf(stderr, "Error opening '%s': %s\n", file, strerror(errno));
goto err;
}
if (ioctl(fd, ioctlnum, attrs) != 0) {
fprintf(stderr, "Error %s attributes: %s\n", verb, strerror(errno));
goto err;
}
close (fd);
return 0;
err:
close (fd);
return -1;
}
static int getattrs(char *file, __u32 *attrs)
{
return _ioctl_attrs(file, attrs, FAT_IOCTL_GET_ATTRIBUTES, "reading");
}
static int setattrs(char *file, __u32 *attrs)
{
return _ioctl_attrs(file, attrs, FAT_IOCTL_SET_ATTRIBUTES, "writing");
}
static int set_hidden_attrib(char *pathname)
{
__u32 attrs = 0;
char *file = NULL;
file = pathname;
if (getattrs(file, &attrs) == 0) {
attrs |= ATTR_HIDDEN;
setattrs(file, &attrs);
return 0;
}
return -1;
}
/**
* Set the FAT HIDDEN attribute.
*
* sugar_fat_set_hidden_attrib:
* @const char: (file utf8)
*/
gboolean sugar_fat_set_hidden_attrib(const char *file)
{
if (set_hidden_attrib(file) == 0) {
return TRUE;
}
return FALSE;
}

View File

@ -0,0 +1,29 @@
/*
* Copyright (C) 2013, Walter Bender
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifndef __SUGAR_FATATTR_H__
#define __SUGAR_FATATTR_H__
G_BEGIN_DECLS
gboolean sugar_fat_set_hidden_attrib(const char *file);
G_END_DECLS
#endif /* __SUGAR_FATATTR_H__ */