2
Eiciel - GNOME editor of ACL file permissions.
3
Copyright (C) 2004-2005 Roger Ferrer Ib��ez
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.
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.
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
19
#include "gestor_xattr.hpp"
21
GestorXAttr::GestorXAttr(const Glib::ustring& nomF) throw (GestorXAttrException)
24
// Comprovem que es un fitxer normal o directori
26
if (stat(nomFitxer.c_str(), &buffer) == -1)
28
throw GestorXAttrException(Glib::locale_to_utf8(strerror(errno)));
31
// Comprovem si es un fitxer regular o un directori
32
if (!S_ISREG(buffer.st_mode) && !S_ISDIR(buffer.st_mode))
34
throw GestorXAttrException(_("Only regular files or directories supported")); // De moment...
37
this->propietari = buffer.st_uid;
39
// Aixo es pq salti una excepcio si cal
43
void GestorXAttr::testLectura() throw (GestorXAttrException)
45
Glib::ustring nomAttrQualif = "user.prova";
48
char* buffer = new char[mida];
50
longitudBuffer = getxattr (nomFitxer.c_str(), nomAttrQualif.c_str(),
53
if (longitudBuffer == -1 &&
58
throw GestorXAttrException(Glib::locale_to_utf8(strerror(errno)));
64
std::vector<std::string> GestorXAttr::obtenirLlistaXAttr() throw (GestorXAttrException)
66
std::vector<std::string> resultat;
68
int mida = listxattr(nomFitxer.c_str(), NULL, 0);
70
// Calculem que la longitud de cada element ser� d'uns 30 car�cters com a molt
72
char* buffer = new char[mida];
75
numElements = listxattr(nomFitxer.c_str(), buffer, mida);
77
while ((numElements == -1) && (errno == ERANGE))
81
buffer = new char[mida];
82
numElements = listxattr(nomFitxer.c_str(), buffer, mida);
85
// numElements == -1 && errno != ERANGE
86
if (numElements == -1)
89
throw GestorXAttrException(Glib::locale_to_utf8(strerror(errno)));
92
for (int inici = 0, actual = 0; actual < numElements; actual++)
94
if (buffer[actual] == '\0')
96
// Ara recuperem cada valor
97
std::string nomAttr(&buffer[inici]);
99
// Alguns FS retornen un nom d'atribut massa petit
100
if (nomAttr.size() > 5)
102
std::string prefix = nomAttr.substr(0, 5);
103
std::string postfix = nomAttr.substr(5);
105
if (prefix == "user.")
107
// Comprovem que el FS retorna alguna cosa ...
112
std::string valorAtribut = recuperarValorAtribut(postfix);
114
catch (GestorXAttrException e)
121
resultat.push_back(postfix);
135
std::string GestorXAttr::recuperarValorAtribut(const std::string& nomAttr) throw (GestorXAttrException)
138
char* buffer = new char[mida];
140
std::string nomAttrQualif = "user." + nomAttr;
142
int longitudBuffer = getxattr (nomFitxer.c_str(), nomAttrQualif.c_str(),
145
while ((longitudBuffer == -1) && (errno == ERANGE))
149
buffer = new char[mida];
151
longitudBuffer = getxattr (nomFitxer.c_str(), nomAttrQualif.c_str(),
155
if (longitudBuffer == -1)
158
throw GestorXAttrException(Glib::locale_to_utf8(strerror(errno)));
161
char* nouBuffer = new char[longitudBuffer + 1];
162
nouBuffer[longitudBuffer] = '\0';
164
for (int i = 0; i < longitudBuffer; i++)
166
nouBuffer[i] = buffer[i];
169
std::string valorAttr(nouBuffer);
177
GestorXAttr::atributs_t GestorXAttr::donarLlistaAtributs()
179
std::vector<std::string> atributs;
180
std::vector<std::string>::iterator it;
182
atributs = obtenirLlistaXAttr();
186
for (it = atributs.begin(); it != atributs.end(); it++)
188
std::string valorAttr = recuperarValorAtribut(*it);
190
resultat[*it] = valorAttr;
196
void GestorXAttr::eliminarAtribut(std::string nomAtr)
198
std::string nomQualif = "user." + nomAtr;
199
int result = removexattr (nomFitxer.c_str(), nomQualif.c_str());
203
throw GestorXAttrException(Glib::locale_to_utf8(strerror(errno)));
207
void GestorXAttr::afegirAtribut(std::string nomAtr, std::string valor)
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);
215
throw GestorXAttrException(Glib::locale_to_utf8(strerror(errno)));
219
void GestorXAttr::canviarNomAtribut(std::string anticNomAttr, std::string nouNomAttr)
221
std::string valorAtribut = recuperarValorAtribut(anticNomAttr);
222
afegirAtribut(nouNomAttr, valorAtribut);
223
eliminarAtribut(anticNomAttr);