summaryrefslogtreecommitdiffstats
path: root/src/logicalfile/file.c
blob: 64b7d52e44e0c94d5f09f56383ef160364f3d08d (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
/*
 * 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 "file.h"

static const char *LOGICALFILE_REQUIRED_NAMES[] = {
    "CSCreationClassName",
    "CSName",
    "CreationClassName",
    "FSCreationClassName",
    "FSName",
    "Name",
    NULL
};

static const char *UNIXFILE_REQUIRED_NAMES[] = {
    "CSCreationClassName",
    "CSName",
    "LFCreationClassName",
    "FSCreationClassName",
    "FSName",
    "LFName",
    NULL
};

CMPIStatus lmi_check_required(
    const CMPIBroker *b,
    const CMPIObjectPath *o,
    const enum RequiredNames rn)
{
    const char **names;

    switch (rn) {
    case LOGICALFILE:
        names = LOGICALFILE_REQUIRED_NAMES;
        break;
    case UNIXFILE:
        names = UNIXFILE_REQUIRED_NAMES;
        break;
    default:
        /* not possible! */
        assert(0);
        break;
    }

    for (int i = 0; names[i]; i++) {
        if (CMIsNullValue(CMGetKey(o, names[i], NULL))) {
            char errmsg[BUFLEN];
            snprintf(errmsg, BUFLEN, "No '%s' specified", names[i]);
            CMReturnWithChars(b, CMPI_RC_ERR_FAILED, errmsg);
        }
    }
    CMReturn(CMPI_RC_OK);
}

void get_class_from_stat(const struct stat *sb, char *fileclass) {
    (S_ISREG(sb->st_mode)) ? strcpy(fileclass, "LMI_DataFile") :
    (S_ISDIR(sb->st_mode)) ? strcpy(fileclass, "LMI_UnixDirectory") :
    (S_ISCHR(sb->st_mode)) ? strcpy(fileclass, "LMI_UnixDeviceFile") :
    (S_ISBLK(sb->st_mode)) ? strcpy(fileclass, "LMI_UnixDeviceFile") :
    (S_ISLNK(sb->st_mode)) ? strcpy(fileclass, "LMI_SymbolicLink") :
    (S_ISFIFO(sb->st_mode)) ? strcpy(fileclass, "LMI_FIFOPipeFile") :
    (S_ISSOCK(sb->st_mode)) ? strcpy(fileclass, "LMI_UnixSocket") :
    strcpy(fileclass, "Unknown");
    assert(strcmp(fileclass, "Unknown") != 0);
}

int get_class_from_path(const char *path, char *fileclass)
{
    int rc = 0;
    struct stat sb;

    if (lstat(path, &sb) < 0) {
        rc = 1;
    } else {
        get_class_from_stat(&sb, fileclass);
    }

    return rc;
}

int get_fsname_from_stat(const struct stat *sb, char **fname)
{
    static struct udev *udev_ctx = NULL;
    struct udev_device *udev_dev;
    const char *dev_name;
    int rc = 0;

    if (!udev_ctx) {
        udev_ctx = udev_new();
        if (!udev_ctx) {
            rc = -1;
            goto err;
        }
    }

    char dev_id[16];
    snprintf(dev_id, 16, "b%u:%u", major(sb->st_dev), minor(sb->st_dev));

    udev_dev = udev_device_new_from_device_id(udev_ctx, dev_id);
    if ((dev_name = udev_device_get_property_value(udev_dev, "ID_FS_UUID_ENC"))) {
        rc = asprintf(fname, "UUID=%s", dev_name);
    } else if ((dev_name = udev_device_get_property_value(udev_dev, "DEVNAME"))) {
        rc = asprintf(fname, "DEVICE=%s", dev_name);
    } else {
        rc = asprintf(fname, "Unknown");
    }
err:
    return rc;
}

int get_fsname_from_path(const char *path, char **fsname)
{
    struct stat sb;
    int rc;

    rc = lstat(path, &sb);
    if (rc == 0) {
        rc = get_fsname_from_stat(&sb, fsname);
    }
    return rc;
}

void _dump_objectpath(const CMPIObjectPath *o)
{
    printf("OP: %s\n", CMGetCharsPtr(o->ft->toString(o, NULL), NULL));
}
/* vi: set et: */