summaryrefslogtreecommitdiffstats
path: root/src/logicalfile/file.c
diff options
context:
space:
mode:
authorJan Synacek <jsynacek@redhat.com>2013-09-18 08:33:03 +0200
committerJan Synacek <jsynacek@redhat.com>2013-09-18 15:19:30 +0200
commitcd13c983a49ce3685a3813e66001bd3ea8f796c8 (patch)
treec79386678dbfdbfbdbbccb48d690f7cd3f475c50 /src/logicalfile/file.c
parent2746832524f5e5d022b13ffa0890f895ab82355c (diff)
downloadopenlmi-providers-cd13c983a49ce3685a3813e66001bd3ea8f796c8.tar.gz
openlmi-providers-cd13c983a49ce3685a3813e66001bd3ea8f796c8.tar.xz
openlmi-providers-cd13c983a49ce3685a3813e66001bd3ea8f796c8.zip
logicalfile: code cleanup and rewrite
GetInstance() calls were rewritten using cleaner code. Also, the macros that were used before were either rewritten to functions, or cleaned up so they wouldn't return or do anything unexpected. Helper functions now use CMPIStatus more consistently. Some memory leaks were fixed.
Diffstat (limited to 'src/logicalfile/file.c')
-rw-r--r--src/logicalfile/file.c131
1 files changed, 110 insertions, 21 deletions
diff --git a/src/logicalfile/file.c b/src/logicalfile/file.c
index e768ea0..7a3cfac 100644
--- a/src/logicalfile/file.c
+++ b/src/logicalfile/file.c
@@ -24,7 +24,6 @@ CMPIStatus lmi_check_required(
const CMPIObjectPath *o)
{
const char *prop;
- const char *errmsg = NULL;
/* check computer system creation class name */
if (CMIsNullValue(CMGetKey(o, "CSCreationClassName", NULL))) {
@@ -73,20 +72,16 @@ int get_class_from_path(const char *path, char *fileclass)
return rc;
}
-int get_fsname_from_stat(const struct stat *sb, char **fname)
+CMPIStatus get_fsname_from_stat(const CMPIBroker *b, const struct stat *sb, char **fname)
{
- static struct udev *udev_ctx = NULL;
+ struct udev *udev_ctx;
struct udev_device *udev_dev;
const char *dev_name;
- int rc = 0;
+ CMPIStatus st = {.rc = CMPI_RC_OK};
+ udev_ctx = udev_new();
if (!udev_ctx) {
- udev_ctx = udev_new();
- if (!udev_ctx) {
- lmi_warn("Could not create udev context");
- rc = -1;
- goto err;
- }
+ return_with_status(b, &st, ERR_FAILED, "Could not create udev context");
}
char dev_id[16];
@@ -94,26 +89,34 @@ int get_fsname_from_stat(const struct stat *sb, char **fname)
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);
+ if (asprintf(fname, "UUID=%s", dev_name) < 0) {
+ return_with_status(b, &st, ERR_FAILED, "asprintf failed");
+ }
} else if ((dev_name = udev_device_get_property_value(udev_dev, "DEVNAME"))) {
- rc = asprintf(fname, "DEVICE=%s", dev_name);
+ if (asprintf(fname, "DEVICE=%s", dev_name) < 0) {
+ return_with_status(b, &st, ERR_FAILED, "asprintf failed");
+ }
} else {
- rc = asprintf(fname, "Unknown");
+ if (asprintf(fname, "Unknown") < 0) {
+ return_with_status(b, &st, ERR_FAILED, "asprintf failed");
+ }
}
-err:
- return rc;
+ udev_device_unref(udev_dev);
+ udev_unref(udev_ctx);
+
+ return st;
}
-int get_fsname_from_path(const char *path, char **fsname)
+CMPIStatus get_fsname_from_path(const CMPIBroker *b, const char *path, char **fsname)
{
+ CMPIStatus st = {.rc = CMPI_RC_OK};
struct stat sb;
- int rc;
- rc = lstat(path, &sb);
- if (rc == 0) {
- rc = get_fsname_from_stat(&sb, fsname);
+ if (lstat(path, &sb) < 0) {
+ return_with_status(b, &st, ERR_FAILED, "lstat(2) failed");
}
- return rc;
+
+ return get_fsname_from_stat(b, &sb, fsname);
}
const char *get_string_property_from_op(const CMPIObjectPath *o, const char *prop)
@@ -123,6 +126,92 @@ const char *get_string_property_from_op(const CMPIObjectPath *o, const char *pro
return KChars(d.value.string);
}
+CMPIStatus check_assoc_class(
+ const CMPIBroker *cb,
+ const char *namespace,
+ const char *assocClass,
+ const char *class)
+{
+ CMPIObjectPath *o;
+ CMPIStatus st = {.rc = CMPI_RC_OK};
+
+ o = CMNewObjectPath(cb, namespace, class, &st);
+ if (!o || st.rc) {
+ CMRelease(o);
+ return st;
+ }
+ if (assocClass && !CMClassPathIsA(cb, o, assocClass, &st)) {
+ CMRelease(o);
+ st.rc = RC_ERR_CLASS_CHECK_FAILED;
+ return st;
+ }
+ CMRelease(o);
+ return st;
+}
+
+CMPIStatus stat_logicalfile_and_fill(
+ const CMPIBroker *b,
+ logicalfile_t *lf,
+ mode_t mode,
+ const char *errmsg)
+{
+ struct stat sb;
+ char buf[BUFLEN];
+ char *fsname = NULL;
+ const char *path = KChars(lf->lf.datafile.Name.value);
+ CMPIStatus st = {.rc = CMPI_RC_OK};
+
+ if (lstat(path, &sb) < 0 || !(sb.st_mode & S_IFMT & mode)) {
+ snprintf(buf, BUFLEN, errmsg, path);
+ CMReturnWithChars(b, CMPI_RC_ERR_NOT_FOUND, buf);
+ }
+
+ get_class_from_stat(&sb, buf);
+
+ st = get_fsname_from_stat(b, &sb, &fsname);
+ check_status(st);
+
+ switch(mode) {
+ case S_IFREG:
+ fill_basic(b, DataFile, &lf->lf.datafile, buf, fsname, sb);
+ break;
+ case S_IFCHR:
+ /* FALLTHROUGH */
+ case S_IFBLK:
+ fill_basic(b, UnixDeviceFile, &lf->lf.unixdevicefile, buf, fsname, sb);
+ /* device-specific stuff */
+ char tmp[16];
+ sprintf(tmp, "%lu", sb.st_rdev);
+ LMI_UnixDeviceFile_Set_DeviceId(&lf->lf.unixdevicefile, tmp);
+ sprintf(tmp, "%u", major(sb.st_rdev));
+ LMI_UnixDeviceFile_Set_DeviceMajor(&lf->lf.unixdevicefile, tmp);
+ sprintf(tmp, "%u", minor(sb.st_rdev));
+ LMI_UnixDeviceFile_Set_DeviceMinor(&lf->lf.unixdevicefile, tmp);
+ break;
+ case S_IFDIR:
+ fill_basic(b, UnixDirectory, &lf->lf.unixdirectory, buf, fsname, sb);
+ break;
+ case S_IFIFO:
+ fill_basic(b, FIFOPipeFile, &lf->lf.fifopipefile, buf, fsname, sb);
+ break;
+ case S_IFLNK:
+ fill_basic(b, SymbolicLink, &lf->lf.symboliclink, buf, fsname, sb);
+ /* symlink-specific stuff */
+ char rpath[PATH_MAX];
+ const char *path;
+ path = KChars(lf->lf.symboliclink.Name.value);
+ realpath(path, rpath);
+ LMI_SymbolicLink_Set_TargetFile(&lf->lf.symboliclink, rpath);
+ break;
+ default:
+ /* impossible */
+ assert(0);
+ }
+
+ free(fsname);
+ return st;
+}
+
void _dump_objectpath(const CMPIObjectPath *o)
{
printf("OP: %s\n", CMGetCharsPtr(o->ft->toString(o, NULL), NULL));