~ubuntu-branches/ubuntu/trusty/eiciel/trusty-proposed

« back to all changes in this revision

Viewing changes to src/gestor_xattr.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michael Biebl
  • Date: 2010-06-04 02:38:02 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20100604023802-jp2v6c9uviiioc5z
* New upstream release.
* debian/patches/02-gio.patch
  - Removed, merged upstream.
* debian/patches/03-no-libgnomeui.patch
  - Removed, merged upstream.
* debian/patches/01-fix-gettext-translations.patch
  - Fix gettext translations by using dgettext and specifying the correct
    domainname.
* debian/patches/02-de-po.patch
  - Update and complete German translation.
* debian/rules
  - Remove de.gmo on clean and rebuild it on build.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
    Eiciel - GNOME editor of ACL file permissions.
3
 
    Copyright (C) 2004-2005 Roger Ferrer Ib��ez
4
 
 
5
 
    This program is free software; you can redistribute it and/or modify
6
 
    it under the terms of the GNU General Public License as published by
7
 
    the Free Software Foundation; either version 2 of the License, or
8
 
    (at your option) any later version.
9
 
 
10
 
    This program is distributed in the hope that it will be useful,
11
 
    but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
    GNU General Public License for more details.
14
 
 
15
 
    You should have received a copy of the GNU General Public License
16
 
    along with this program; if not, write to the Free Software
17
 
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
 
*/
19
 
#include "gestor_xattr.hpp"
20
 
 
21
 
GestorXAttr::GestorXAttr(const Glib::ustring& nomF) throw (GestorXAttrException)
22
 
        : nomFitxer(nomF)
23
 
{
24
 
        // Comprovem que es un fitxer normal o directori
25
 
        struct stat buffer;
26
 
        if (stat(nomFitxer.c_str(), &buffer) == -1)
27
 
        {
28
 
                throw GestorXAttrException(Glib::locale_to_utf8(strerror(errno)));
29
 
        }
30
 
 
31
 
        // Comprovem si es un fitxer regular o un directori
32
 
        if (!S_ISREG(buffer.st_mode) && !S_ISDIR(buffer.st_mode))
33
 
        {
34
 
                throw GestorXAttrException(_("Only regular files or directories supported")); // De moment...
35
 
        }
36
 
 
37
 
        this->propietari = buffer.st_uid;
38
 
        
39
 
        // Aixo es pq salti una excepcio si cal
40
 
        testLectura();
41
 
}
42
 
 
43
 
void GestorXAttr::testLectura() throw (GestorXAttrException)
44
 
{
45
 
        Glib::ustring nomAttrQualif = "user.prova";
46
 
        int longitudBuffer;
47
 
        int mida = 30;
48
 
        char* buffer = new char[mida];
49
 
 
50
 
        longitudBuffer = getxattr (nomFitxer.c_str(), nomAttrQualif.c_str(),
51
 
                        buffer, mida);
52
 
 
53
 
        if (longitudBuffer == -1 && 
54
 
                        errno != ENOATTR && 
55
 
                        errno != ERANGE)
56
 
        {
57
 
                delete[] buffer;
58
 
                throw GestorXAttrException(Glib::locale_to_utf8(strerror(errno)));
59
 
        }
60
 
 
61
 
        delete[] buffer;
62
 
}
63
 
 
64
 
std::vector<std::string> GestorXAttr::obtenirLlistaXAttr() throw (GestorXAttrException)
65
 
{
66
 
        std::vector<std::string> resultat;
67
 
 
68
 
    int mida = listxattr(nomFitxer.c_str(), NULL, 0);
69
 
 
70
 
        // Calculem que la longitud de cada element ser� d'uns 30 car�cters com a molt
71
 
        mida = mida*30;
72
 
        char* buffer = new char[mida];
73
 
 
74
 
        int numElements;
75
 
        numElements = listxattr(nomFitxer.c_str(), buffer, mida);
76
 
 
77
 
        while ((numElements == -1) && (errno == ERANGE))
78
 
        {
79
 
                delete[] buffer;
80
 
                mida = mida*2;
81
 
                buffer = new char[mida];
82
 
                numElements = listxattr(nomFitxer.c_str(), buffer, mida);
83
 
        }
84
 
 
85
 
        // numElements == -1 && errno != ERANGE
86
 
        if (numElements == -1)
87
 
        {
88
 
                delete[] buffer;
89
 
                throw GestorXAttrException(Glib::locale_to_utf8(strerror(errno)));
90
 
        }
91
 
 
92
 
        for (int inici = 0, actual = 0; actual < numElements; actual++)
93
 
        {
94
 
                if (buffer[actual] == '\0')
95
 
                {
96
 
                        // Ara recuperem cada valor
97
 
                        std::string nomAttr(&buffer[inici]);
98
 
 
99
 
            // Alguns FS retornen un nom d'atribut massa petit
100
 
            if (nomAttr.size() > 5)
101
 
            {
102
 
                std::string prefix =  nomAttr.substr(0, 5);
103
 
                std::string postfix = nomAttr.substr(5);
104
 
 
105
 
                if (prefix == "user.")
106
 
                {
107
 
                    // Comprovem que el FS retorna alguna cosa ...
108
 
                    bool esBuit = false;
109
 
 
110
 
                    try
111
 
                    {
112
 
                        std::string valorAtribut = recuperarValorAtribut(postfix);
113
 
                    }
114
 
                    catch (GestorXAttrException e)
115
 
                    {
116
 
                        esBuit = true;
117
 
                    }
118
 
 
119
 
                    if (!esBuit)
120
 
                    {
121
 
                        resultat.push_back(postfix);
122
 
                    }
123
 
                }
124
 
            }
125
 
                        
126
 
                        inici = actual + 1;
127
 
                }
128
 
        }
129
 
        
130
 
        delete[] buffer;
131
 
 
132
 
        return resultat;
133
 
}
134
 
 
135
 
std::string GestorXAttr::recuperarValorAtribut(const std::string& nomAttr) throw (GestorXAttrException)
136
 
{
137
 
        int mida = 30;
138
 
        char* buffer = new char[mida];
139
 
 
140
 
        std::string nomAttrQualif = "user." + nomAttr;
141
 
 
142
 
        int longitudBuffer = getxattr (nomFitxer.c_str(), nomAttrQualif.c_str(),
143
 
                        buffer, mida);
144
 
 
145
 
        while ((longitudBuffer == -1) && (errno == ERANGE))
146
 
        {
147
 
                delete[] buffer;
148
 
                mida = mida*2;
149
 
                buffer = new char[mida];
150
 
 
151
 
                longitudBuffer = getxattr (nomFitxer.c_str(), nomAttrQualif.c_str(),
152
 
                                buffer, mida);
153
 
        }
154
 
 
155
 
        if (longitudBuffer == -1)
156
 
        {
157
 
                delete[] buffer;
158
 
                throw GestorXAttrException(Glib::locale_to_utf8(strerror(errno)));
159
 
        }
160
 
 
161
 
        char* nouBuffer = new char[longitudBuffer + 1];
162
 
        nouBuffer[longitudBuffer] = '\0';
163
 
 
164
 
        for (int i = 0; i < longitudBuffer; i++)
165
 
        {
166
 
                nouBuffer[i] = buffer[i];
167
 
        }
168
 
 
169
 
        std::string valorAttr(nouBuffer);
170
 
 
171
 
        delete[] nouBuffer;
172
 
        delete[] buffer;
173
 
 
174
 
        return valorAttr;
175
 
}
176
 
 
177
 
GestorXAttr::atributs_t GestorXAttr::donarLlistaAtributs()
178
 
{
179
 
        std::vector<std::string> atributs;
180
 
        std::vector<std::string>::iterator it;
181
 
 
182
 
        atributs = obtenirLlistaXAttr();
183
 
 
184
 
        atributs_t resultat;
185
 
 
186
 
        for (it = atributs.begin(); it != atributs.end(); it++)
187
 
        {
188
 
                std::string valorAttr = recuperarValorAtribut(*it);
189
 
 
190
 
                resultat[*it] = valorAttr;
191
 
        }
192
 
 
193
 
        return resultat;
194
 
}
195
 
 
196
 
void GestorXAttr::eliminarAtribut(std::string nomAtr)
197
 
{
198
 
        std::string nomQualif = "user." + nomAtr;
199
 
        int result = removexattr (nomFitxer.c_str(), nomQualif.c_str());
200
 
 
201
 
        if (result != 0)
202
 
        {
203
 
                throw GestorXAttrException(Glib::locale_to_utf8(strerror(errno)));
204
 
        }
205
 
}
206
 
 
207
 
void GestorXAttr::afegirAtribut(std::string nomAtr, std::string valor)
208
 
{
209
 
        std::string nomQualif = "user." + nomAtr;
210
 
        int resultat = setxattr (nomFitxer.c_str(), nomQualif.c_str(),
211
 
                        valor.c_str(), strlen(valor.c_str()), 0);
212
 
 
213
 
        if (resultat != 0)
214
 
        {
215
 
                throw GestorXAttrException(Glib::locale_to_utf8(strerror(errno)));
216
 
        }
217
 
}
218
 
 
219
 
void GestorXAttr::canviarNomAtribut(std::string anticNomAttr, std::string nouNomAttr)
220
 
{
221
 
        std::string valorAtribut = recuperarValorAtribut(anticNomAttr);
222
 
        afegirAtribut(nouNomAttr, valorAtribut);
223
 
        eliminarAtribut(anticNomAttr);
224
 
}