~ubuntu-branches/ubuntu/dapper/eiciel/dapper

« back to all changes in this revision

Viewing changes to src/gestor_xattr.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michael Biebl
  • Date: 2005-12-27 01:45:06 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20051227014506-nhcty9r1kink5hlz
Tags: 0.9-1
* New upstream release.
* Updated description as eiciel now also allows to manipulate extended user
  attributes.

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
                        std::string prefix =  nomAttr.substr(0, 5);
 
100
                        std::string postfix = nomAttr.substr(5);
 
101
 
 
102
                        if (prefix == "user.")
 
103
                        {
 
104
                                resultat.push_back(postfix);
 
105
                        }
 
106
                        
 
107
                        inici = actual + 1;
 
108
                }
 
109
        }
 
110
        
 
111
        delete[] buffer;
 
112
 
 
113
        return resultat;
 
114
}
 
115
 
 
116
std::string GestorXAttr::recuperarValorAtribut(const std::string& nomAttr) throw (GestorXAttrException)
 
117
{
 
118
        int mida = 30;
 
119
        char* buffer = new char[mida];
 
120
 
 
121
        std::string nomAttrQualif = "user." + nomAttr;
 
122
 
 
123
        int longitudBuffer = getxattr (nomFitxer.c_str(), nomAttrQualif.c_str(),
 
124
                        buffer, mida);
 
125
 
 
126
        while ((longitudBuffer == -1) && (errno == ERANGE))
 
127
        {
 
128
                delete[] buffer;
 
129
                mida = mida*2;
 
130
                buffer = new char[mida];
 
131
 
 
132
                longitudBuffer = getxattr (nomFitxer.c_str(), nomAttrQualif.c_str(),
 
133
                                buffer, mida);
 
134
        }
 
135
 
 
136
        if (longitudBuffer == -1)
 
137
        {
 
138
                delete[] buffer;
 
139
                throw GestorXAttrException(Glib::locale_to_utf8(strerror(errno)));
 
140
        }
 
141
 
 
142
        char* nouBuffer = new char[longitudBuffer + 1];
 
143
        nouBuffer[longitudBuffer] = '\0';
 
144
 
 
145
        for (int i = 0; i < longitudBuffer; i++)
 
146
        {
 
147
                nouBuffer[i] = buffer[i];
 
148
        }
 
149
 
 
150
        std::string valorAttr(nouBuffer);
 
151
 
 
152
        delete[] nouBuffer;
 
153
        delete[] buffer;
 
154
 
 
155
        return valorAttr;
 
156
}
 
157
 
 
158
GestorXAttr::atributs_t GestorXAttr::donarLlistaAtributs()
 
159
{
 
160
        std::vector<std::string> atributs;
 
161
        std::vector<std::string>::iterator it;
 
162
 
 
163
        atributs = obtenirLlistaXAttr();
 
164
 
 
165
        atributs_t resultat;
 
166
 
 
167
        for (it = atributs.begin(); it != atributs.end(); it++)
 
168
        {
 
169
                std::string valorAttr = recuperarValorAtribut(*it);
 
170
 
 
171
                resultat[*it] = valorAttr;
 
172
        }
 
173
 
 
174
        return resultat;
 
175
}
 
176
 
 
177
void GestorXAttr::eliminarAtribut(std::string nomAtr)
 
178
{
 
179
        std::string nomQualif = "user." + nomAtr;
 
180
        int result = removexattr (nomFitxer.c_str(), nomQualif.c_str());
 
181
 
 
182
        if (result != 0)
 
183
        {
 
184
                throw GestorXAttrException(Glib::locale_to_utf8(strerror(errno)));
 
185
        }
 
186
}
 
187
 
 
188
void GestorXAttr::afegirAtribut(std::string nomAtr, std::string valor)
 
189
{
 
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);
 
193
 
 
194
        if (resultat != 0)
 
195
        {
 
196
                throw GestorXAttrException(Glib::locale_to_utf8(strerror(errno)));
 
197
        }
 
198
}
 
199
 
 
200
void GestorXAttr::canviarNomAtribut(std::string anticNomAttr, std::string nouNomAttr)
 
201
{
 
202
        std::string valorAtribut = recuperarValorAtribut(anticNomAttr);
 
203
        afegirAtribut(nouNomAttr, valorAtribut);
 
204
        eliminarAtribut(anticNomAttr);
 
205
}