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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
|
/*
* Copyright (C) 2012-2013 Red Hat, Inc. All rights reserved.
*
* 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.1 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Authors: Jan Synacek <jsynacek@redhat.com>
*/
#include <konkret/konkret.h>
#include "LMI_DirectoryContainsFile.h"
#include "LMI_UnixDirectory.h"
#include "CIM_LogicalFile.h"
#include "CIM_Directory.h"
#include "file.h"
static const CMPIBroker* _cb;
/* XXX if a directory contains more than MAX_REFS files,
* broker will abort with an error */
/* const unsigned int MAX_REFS = 65536; */
const unsigned int MAX_REFS = 4096;
static CMPIStatus dir_file_objectpaths(
const CMPIContext* cc,
const CMPIResult* cr,
const char** properties,
const char *namespace,
const char *path,
CMPIObjectPath **ops,
unsigned int *count)
{
CMPIObjectPath *o;
CMPIStatus st;
unsigned int i = 0;
struct stat sb;
struct dirent *de;
DIR *dp;
dp = opendir(path);
while ((de = readdir(dp))) {
if (!strcmp(de->d_name, ".")) {
continue;
}
char rpath[BUFLEN];
char fileclass[BUFLEN];
char *aux = strdup(path);
if (!strcmp(de->d_name, "..")) {
strncpy(rpath, dirname(aux), BUFLEN);
free(aux);
} else {
snprintf(rpath, BUFLEN, "%s/%s",
(!strcmp(path, "/")) ? "" : path,
de->d_name);
}
if (lstat(rpath, &sb) < 0) {
char err[BUFLEN];
snprintf(err, BUFLEN, "Can't stat file: %s", rpath);
CMReturnWithChars(_cb, CMPI_RC_ERR_NOT_FOUND, err);
} else {
get_class_from_stat(&sb, fileclass);
}
CIM_LogicalFileRef cim_lfr;
CIM_LogicalFileRef_Init(&cim_lfr, _cb, namespace);
fill_logicalfile(CIM_LogicalFileRef, &cim_lfr, rpath, fileclass);
o = CIM_LogicalFileRef_ToObjectPath(&cim_lfr, &st);
CMSetClassName(o, fileclass);
ops[i++] = o;
}
*count = i;
CMReturn(CMPI_RC_OK);
}
static int check_valid_classes(const CMPIObjectPath *o)
{
const char *VALID_CLASSES[] = {
"LMI_DataFile",
"LMI_UnixDeviceFile",
"LMI_UnixDirectory",
"LMI_FIFOPipeFile",
"LMI_SymbolicLink",
"LMI_UnixSocket",
NULL
};
int found = 0;
for (int i = 0; VALID_CLASSES[i]; i++) {
if (CMClassPathIsA(_cb, o, VALID_CLASSES[i], NULL)) {
found++;
break;
}
}
return found;
}
static CMPIStatus associators(
CMPIAssociationMI* mi,
const CMPIContext* cc,
const CMPIResult* cr,
const CMPIObjectPath* cop,
const char* assocClass,
const char* resultClass,
const char* role,
const char* resultRole,
const char** properties,
int names)
{
CMPIObjectPath *o;
CMPIInstance *ci;
CMPIStatus st;
CMPIData cd, pathd;
const char *ns = KNameSpace(cop);
st = lmi_check_required(_cb, cop, LOGICALFILE);
if (st.rc != CMPI_RC_OK) {
return st;
}
/* allow only LMI_UnixFile and classes derived from CIM_LogicalFile */
if (!check_valid_classes(cop)) {
CMReturn(CMPI_RC_OK);
}
cd = CMGetKey(cop, "CreationClassName", &st);
const char *ccname = KChars(cd.value.string);
pathd = CMGetKey(cop, "Name", &st);
const char *path = KChars(pathd.value.string);
CMPIObjectPath *refs[MAX_REFS];
unsigned int count;
if (CMClassPathIsA(_cb, cop, LMI_UnixDirectory_ClassName, &st)) {
/* got UnixDirectory */
st = dir_file_objectpaths(cc, cr, properties, ns, path, refs, &count);
if (st.rc != CMPI_RC_OK) {
return st;
}
if (count > MAX_REFS) {
CMReturnWithChars(_cb, CMPI_RC_ERR_NOT_FOUND, "Too many files in a single directory...");
}
for (unsigned int i = 0; i < count; i++) {
if (names) {
CMReturnObjectPath(cr, refs[i]);
} else {
ci = _cb->bft->getInstance(_cb, cc, refs[i], properties, &st);
CMReturnInstance(cr, ci);
}
}
} else {
/* got LogicalFile */
CIM_DirectoryRef lmi_dr;
CIM_DirectoryRef_Init(&lmi_dr, _cb, ns);
char *aux = strdup(path);
fill_logicalfile(CIM_DirectoryRef, &lmi_dr, dirname(aux), LMI_UnixDirectory_ClassName);
o = CIM_DirectoryRef_ToObjectPath(&lmi_dr, &st);
CMSetClassName(o, LMI_UnixDirectory_ClassName);
if (names) {
CMReturnObjectPath(cr, o);
} else {
ci = _cb->bft->getInstance(_cb, cc, o, properties, &st);
CMReturnInstance(cr, ci);
}
free(aux);
}
CMReturn(CMPI_RC_OK);
}
static CMPIStatus references(
CMPIAssociationMI* mi,
const CMPIContext* cc,
const CMPIResult* cr,
const CMPIObjectPath* cop,
const char* assocClass,
const char* role,
const char** properties,
int names)
{
LMI_DirectoryContainsFile lmi_dcf;
CMPIStatus st;
CMPIData pathd;
CMPIData cd;
const char *ns = KNameSpace(cop);
/* GroupComponent */
CIM_DirectoryRef lmi_dr;
/* PartComponent */
CIM_LogicalFileRef lmi_lfr;
CMPIObjectPath *o;
CMPIInstance *ci;
/* allow only LMI_UnixFile and classes derived from CIM_LogicalFile */
if (!check_valid_classes(cop)) {
CMReturn(CMPI_RC_OK);
}
CIM_DirectoryRef_Init(&lmi_dr, _cb, ns);
CIM_LogicalFileRef_Init(&lmi_lfr, _cb, ns);
LMI_DirectoryContainsFile_Init(&lmi_dcf, _cb, ns);
st = lmi_check_required(_cb, cop, LOGICALFILE);
if (st.rc != CMPI_RC_OK) {
return st;
}
cd = CMGetKey(cop, "CreationClassName", &st);
const char *ccname = KChars(cd.value.string);
pathd = CMGetKey(cop, "Name", &st);
const char *path = KChars(pathd.value.string);
if (!strcmp(ccname, LMI_UnixDirectory_ClassName)) {
/* got GroupComponent - DirectoryRef */
fill_logicalfile(CIM_DirectoryRef, &lmi_dr, path, LMI_UnixDirectory_ClassName);
o = CIM_DirectoryRef_ToObjectPath(&lmi_dr, &st);
CMSetClassName(o, LMI_UnixDirectory_ClassName);
LMI_DirectoryContainsFile_SetObjectPath_GroupComponent(&lmi_dcf, o);
/* PartComponent */
CMPIObjectPath *refs[MAX_REFS];
unsigned int count;
st = dir_file_objectpaths(cc, cr, properties, ns, path, refs, &count);
if (st.rc != CMPI_RC_OK) {
return st;
}
if (count > MAX_REFS) {
CMReturnWithChars(_cb, CMPI_RC_ERR_NOT_FOUND, "Too many files in a single directory...");
}
for (unsigned int i = 0; i < count; i++) {
LMI_DirectoryContainsFile_SetObjectPath_PartComponent(&lmi_dcf, refs[i]);
o = LMI_DirectoryContainsFile_ToObjectPath(&lmi_dcf, &st);
if (names) {
CMReturnObjectPath(cr, o);
} else {
ci = LMI_DirectoryContainsFile_ToInstance(&lmi_dcf, &st);
CMReturnInstance(cr, ci);
}
}
} else {
/* got PartComponent - LogicalFileRef */
fill_logicalfile(CIM_LogicalFileRef, &lmi_lfr, path, ccname);
o = CIM_LogicalFileRef_ToObjectPath(&lmi_lfr, &st);
CMSetClassName(o, ccname);
LMI_DirectoryContainsFile_SetObjectPath_PartComponent(&lmi_dcf, o);
/* GroupComponent */
char *aux = strdup(path);
fill_logicalfile(CIM_DirectoryRef, &lmi_dr, dirname(aux), LMI_UnixDirectory_ClassName);
o = CIM_DirectoryRef_ToObjectPath(&lmi_dr, &st);
CMSetClassName(o, LMI_UnixDirectory_ClassName);
LMI_DirectoryContainsFile_SetObjectPath_GroupComponent(&lmi_dcf, o);
o = LMI_DirectoryContainsFile_ToObjectPath(&lmi_dcf, &st);
if (names) {
CMReturnObjectPath(cr, o);
} else {
ci = LMI_DirectoryContainsFile_ToInstance(&lmi_dcf, &st);
CMReturnInstance(cr, ci);
}
free(aux);
}
CMReturn(CMPI_RC_OK);
}
static void LMI_DirectoryContainsFileInitialize()
{
}
static CMPIStatus LMI_DirectoryContainsFileCleanup(
CMPIInstanceMI* mi,
const CMPIContext* cc,
CMPIBoolean term)
{
CMReturn(CMPI_RC_OK);
}
static CMPIStatus LMI_DirectoryContainsFileEnumInstanceNames(
CMPIInstanceMI* mi,
const CMPIContext* cc,
const CMPIResult* cr,
const CMPIObjectPath* cop)
{
CMReturn(CMPI_RC_ERR_NOT_SUPPORTED);
}
static CMPIStatus LMI_DirectoryContainsFileEnumInstances(
CMPIInstanceMI* mi,
const CMPIContext* cc,
const CMPIResult* cr,
const CMPIObjectPath* cop,
const char** properties)
{
CMReturn(CMPI_RC_ERR_NOT_SUPPORTED);
}
static CMPIStatus LMI_DirectoryContainsFileGetInstance(
CMPIInstanceMI* mi,
const CMPIContext* cc,
const CMPIResult* cr,
const CMPIObjectPath* cop,
const char** properties)
{
/* TODO TBI */
CMReturn(CMPI_RC_OK);
}
static CMPIStatus LMI_DirectoryContainsFileCreateInstance(
CMPIInstanceMI* mi,
const CMPIContext* cc,
const CMPIResult* cr,
const CMPIObjectPath* cop,
const CMPIInstance* ci)
{
CMReturn(CMPI_RC_ERR_NOT_SUPPORTED);
}
static CMPIStatus LMI_DirectoryContainsFileModifyInstance(
CMPIInstanceMI* mi,
const CMPIContext* cc,
const CMPIResult* cr,
const CMPIObjectPath* cop,
const CMPIInstance* ci,
const char**properties)
{
CMReturn(CMPI_RC_ERR_NOT_SUPPORTED);
}
static CMPIStatus LMI_DirectoryContainsFileDeleteInstance(
CMPIInstanceMI* mi,
const CMPIContext* cc,
const CMPIResult* cr,
const CMPIObjectPath* cop)
{
CMReturn(CMPI_RC_ERR_NOT_SUPPORTED);
}
static CMPIStatus LMI_DirectoryContainsFileExecQuery(
CMPIInstanceMI* mi,
const CMPIContext* cc,
const CMPIResult* cr,
const CMPIObjectPath* cop,
const char* lang,
const char* query)
{
CMReturn(CMPI_RC_ERR_NOT_SUPPORTED);
}
static CMPIStatus LMI_DirectoryContainsFileAssociationCleanup(
CMPIAssociationMI* mi,
const CMPIContext* cc,
CMPIBoolean term)
{
CMReturn(CMPI_RC_OK);
}
static CMPIStatus LMI_DirectoryContainsFileAssociators(
CMPIAssociationMI* mi,
const CMPIContext* cc,
const CMPIResult* cr,
const CMPIObjectPath* cop,
const char* assocClass,
const char* resultClass,
const char* role,
const char* resultRole,
const char** properties)
{
return associators(mi, cc, cr, cop, assocClass, resultClass, role, resultRole,
properties, 0);
}
static CMPIStatus LMI_DirectoryContainsFileAssociatorNames(
CMPIAssociationMI* mi,
const CMPIContext* cc,
const CMPIResult* cr,
const CMPIObjectPath* cop,
const char* assocClass,
const char* resultClass,
const char* role,
const char* resultRole)
{
return associators(mi, cc, cr, cop, assocClass, resultClass, role, resultRole,
NULL, 1);
}
static CMPIStatus LMI_DirectoryContainsFileReferences(
CMPIAssociationMI* mi,
const CMPIContext* cc,
const CMPIResult* cr,
const CMPIObjectPath* cop,
const char* assocClass,
const char* role,
const char** properties)
{
return references(mi, cc, cr, cop, assocClass, role, properties, 0);
}
static CMPIStatus LMI_DirectoryContainsFileReferenceNames(
CMPIAssociationMI* mi,
const CMPIContext* cc,
const CMPIResult* cr,
const CMPIObjectPath* cop,
const char* assocClass,
const char* role)
{
return references(mi, cc, cr, cop, assocClass, role, NULL, 1);
}
CMInstanceMIStub(
LMI_DirectoryContainsFile,
LMI_DirectoryContainsFile,
_cb,
LMI_DirectoryContainsFileInitialize())
CMAssociationMIStub(
LMI_DirectoryContainsFile,
LMI_DirectoryContainsFile,
_cb,
LMI_DirectoryContainsFileInitialize())
KONKRET_REGISTRATION(
"root/cimv2",
"LMI_DirectoryContainsFile",
"LMI_DirectoryContainsFile",
"instance association")
/* vi: set et: */
|