summaryrefslogtreecommitdiffstats
path: root/src/logicalfile/LMI_DirectoryContainsFileProvider.c
blob: 06bc7b911f26e015c7b8f92ab4ec5fe576a2d058 (plain)
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
/*
 * 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* resultClass,
    int group,
    int rgroup,
    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, ".") == 0) {
            continue;
        }

        char rpath[BUFLEN + 1]; /* \0 */
        char fileclass[BUFLEN];
        char *fsname;

        if (strcmp(de->d_name, "..") == 0) {
            /* to get the parent directory, if either role or result role is
             * set, role must be GroupComponent, or result role must be
             * PartComponent */
            if (group == 1 || rgroup == 0) {
                continue;
            }
            char *aux = strdup(path);
            strncpy(rpath, dirname(aux), BUFLEN);
            free(aux);
        } else {
            /* to get the content of a directory, if either role or result role
             * is set, role must be PartComponent, or result role must be
             * GroupComponent */
            if (group == 0 || rgroup == 1) {
                continue;
            }
            snprintf(rpath, BUFLEN, "%s/%s",
                     (strcmp(path, "/") == 0) ? "" : path,
                     de->d_name);
        }

        if (lstat(rpath, &sb) < 0) {
            closedir(dp);
            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);
            st = check_assoc_class(_cb, namespace, resultClass, fileclass);
            check_class_check_status(st);
            if (st.rc != CMPI_RC_OK) {
                closedir(dp);
                return st;
            }
            st = get_fsname_from_stat(_cb, &sb, &fsname);
            if (st.rc != CMPI_RC_OK) {
                closedir(dp);
                return st;
            }
        }

        CIM_LogicalFileRef cim_lfr;
        CIM_LogicalFileRef_Init(&cim_lfr, _cb, namespace);
        fill_logicalfile(CIM_LogicalFileRef, &cim_lfr, rpath, fsname, fileclass);
        o = CIM_LogicalFileRef_ToObjectPath(&cim_lfr, &st);
        CMSetClassName(o, fileclass);

        ops[i++] = o;
        free(fsname);
    }
    *count = i;
    closedir(dp);
    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;
    const char *ns = KNameSpace(cop);
    int group = -1;
    int rgroup = -1;

    st = lmi_check_required(_cb, cop);
    if (st.rc != CMPI_RC_OK) {
        return st;
    }
    st = check_assoc_class(_cb, ns, assocClass, LMI_DirectoryContainsFile_ClassName);
    check_class_check_status(st);
    /* allow only LMI_UnixFile and classes derived from CIM_LogicalFile */
    if (!check_valid_classes(cop)) {
        CMReturn(CMPI_RC_OK);
    }
    /* role && resultRole checks */
    if (role) {
        if (strcmp(role, GROUP_COMPONENT) != 0 && strcmp(role, PART_COMPONENT) != 0) {
            CMReturn(CMPI_RC_OK);
        } else if (strcmp(role, GROUP_COMPONENT) == 0) {
            group = 1;
        } else if (strcmp(role, PART_COMPONENT) == 0) {
            group = 0;
        }
    }
    if (resultRole) {
        if (strcmp(resultRole, GROUP_COMPONENT) != 0 && strcmp(resultRole, PART_COMPONENT) != 0) {
            CMReturn(CMPI_RC_OK);
        } else if (strcmp(resultRole, GROUP_COMPONENT) == 0) {
            rgroup = 1;
        } else if (strcmp(resultRole, PART_COMPONENT) == 0) {
            rgroup = 0;
        }
    }
    /* if role and resultRole are the same, there is nothing left to return */
    if ((group == 1 && rgroup == 1) ||
        (group == 0 && rgroup == 0)) {
        CMReturn(CMPI_RC_OK);
    }

    const char *path = get_string_property_from_op(cop, "Name");

    CMPIObjectPath *refs[MAX_REFS];
    unsigned int count = 0;

    if (CMClassPathIsA(_cb, cop, LMI_UnixDirectory_ClassName, &st)) {
        /* got UnixDirectory - GroupComponent */
        st = dir_file_objectpaths(cc, cr, resultClass, group, rgroup, 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...");
        }

        /* TODO: directories are walked in two passes
         * rewrite using only one pass */
        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 - PartComponent */
        if (group == 1 || rgroup == 0) {
            CMReturn(CMPI_RC_OK);
        }
        st = check_assoc_class(_cb, ns, resultClass, LMI_UnixDirectory_ClassName);
        check_class_check_status(st);

        CIM_DirectoryRef lmi_dr;
        CIM_DirectoryRef_Init(&lmi_dr, _cb, ns);
        char *aux = strdup(path);
        char *dir = dirname(aux);
        char *fsname;

        st = get_fsname_from_path(_cb, path, &fsname);
        if (st.rc != CMPI_RC_OK) {
            free(aux);
            return st;
        }

        fill_logicalfile(CIM_DirectoryRef, &lmi_dr, dir, fsname, 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);
        free(fsname);
    }
    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;
    const char *ns = KNameSpace(cop);

    /* GroupComponent */
    CIM_DirectoryRef lmi_dr;
    /* PartComponent */
    CIM_LogicalFileRef lmi_lfr;
    CMPIObjectPath *o;
    CMPIInstance *ci;
    int group = -1;

    st = check_assoc_class(_cb, ns, assocClass, LMI_DirectoryContainsFile_ClassName);
    check_class_check_status(st);
    /* allow only LMI_UnixFile and classes derived from CIM_LogicalFile */
    if (!check_valid_classes(cop)) {
        CMReturn(CMPI_RC_OK);
    }
    if (role) {
        if (strcmp(role, GROUP_COMPONENT) != 0 && strcmp(role, PART_COMPONENT) != 0) {
            CMReturn(CMPI_RC_OK);
        } else if (strcmp(role, GROUP_COMPONENT) == 0) {
            group = 1;
        } else if (strcmp(role, PART_COMPONENT) == 0) {
            group = 0;
        }
    }
    st = lmi_check_required(_cb, cop);
    check_status(st);

    CIM_DirectoryRef_Init(&lmi_dr, _cb, ns);
    CIM_LogicalFileRef_Init(&lmi_lfr, _cb, ns);
    LMI_DirectoryContainsFile_Init(&lmi_dcf, _cb, ns);

    const char *ccname = get_string_property_from_op(cop, "CreationClassName");
    const char *path = get_string_property_from_op(cop, "Name");
    char *fsname;
    st = get_fsname_from_path(_cb, path, &fsname);
    check_status(st);

    if (strcmp(ccname, LMI_UnixDirectory_ClassName) == 0) {
        /* got GroupComponent - DirectoryRef */
        fill_logicalfile(CIM_DirectoryRef, &lmi_dr, path, fsname, 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, NULL, group, -1, properties, ns, path, refs, &count);
        check_status(st);
        if (count > MAX_REFS) {
            CMReturnWithChars(_cb, CMPI_RC_ERR_NOT_FOUND, "Too many files in a single directory...");
        }
        /* TODO: directories are walked in two passes
         * rewrite using only one pass */
        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 */
        if (group == 1) {
            CMReturn(CMPI_RC_OK);
        }

        fill_logicalfile(CIM_LogicalFileRef, &lmi_lfr, path, fsname, ccname);
        o = CIM_LogicalFileRef_ToObjectPath(&lmi_lfr, &st);
        CMSetClassName(o, ccname);
        LMI_DirectoryContainsFile_SetObjectPath_PartComponent(&lmi_dcf, o);

        /* GroupComponent */
        char *aux = strdup(path);
        char *dir = dirname(aux);
        char *fsname;
        st = get_fsname_from_path(_cb, dir, &fsname);
        if (st.rc != CMPI_RC_OK) {
            free(aux);
            return st;
        }

        fill_logicalfile(CIM_DirectoryRef, &lmi_dr, dir, fsname, 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);
        free(fsname);
    }
    free(fsname);
    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: */
/* Local Variables: */
/* indent-tabs-mode: nil */
/* End: */