Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/sugar3/sugar-fatattr.c
blob: 40a6ee8da403c3c583e61917a7813d64872723b2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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;
}