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
std::string prefix = nomAttr.substr(0, 5);
100
std::string postfix = nomAttr.substr(5);
102
if (prefix == "user.")
104
resultat.push_back(postfix);
116
std::string GestorXAttr::recuperarValorAtribut(const std::string& nomAttr) throw (GestorXAttrException)
119
char* buffer = new char[mida];
121
std::string nomAttrQualif = "user." + nomAttr;
123
int longitudBuffer = getxattr (nomFitxer.c_str(), nomAttrQualif.c_str(),
126
while ((longitudBuffer == -1) && (errno == ERANGE))
130
buffer = new char[mida];
132
longitudBuffer = getxattr (nomFitxer.c_str(), nomAttrQualif.c_str(),
136
if (longitudBuffer == -1)
139
throw GestorXAttrException(Glib::locale_to_utf8(strerror(errno)));
142
char* nouBuffer = new char[longitudBuffer + 1];
143
nouBuffer[longitudBuffer] = '\0';
145
for (int i = 0; i < longitudBuffer; i++)
147
nouBuffer[i] = buffer[i];
150
std::string valorAttr(nouBuffer);
158
GestorXAttr::atributs_t GestorXAttr::donarLlistaAtributs()
160
std::vector<std::string> atributs;
161
std::vector<std::string>::iterator it;
163
atributs = obtenirLlistaXAttr();
167
for (it = atributs.begin(); it != atributs.end(); it++)
169
std::string valorAttr = recuperarValorAtribut(*it);
171
resultat[*it] = valorAttr;
177
void GestorXAttr::eliminarAtribut(std::string nomAtr)
179
std::string nomQualif = "user." + nomAtr;
180
int result = removexattr (nomFitxer.c_str(), nomQualif.c_str());
184
throw GestorXAttrException(Glib::locale_to_utf8(strerror(errno)));
188
void GestorXAttr::afegirAtribut(std::string nomAtr, std::string valor)
190
std::string nomQualif = "user." + nomAtr;
191
int resultat = setxattr (nomFitxer.c_str(), nomQualif.c_str(),
192
valor.c_str(), strlen(valor.c_str()), 0);
196
throw GestorXAttrException(Glib::locale_to_utf8(strerror(errno)));
200
void GestorXAttr::canviarNomAtribut(std::string anticNomAttr, std::string nouNomAttr)
202
std::string valorAtribut = recuperarValorAtribut(anticNomAttr);
203
afegirAtribut(nouNomAttr, valorAtribut);
204
eliminarAtribut(anticNomAttr);