summaryrefslogtreecommitdiffstats
path: root/src/logicalfile/LMI_DirectoryContainsFileProvider.c
diff options
context:
space:
mode:
authorJan Synacek <jsynacek@redhat.com>2014-01-08 15:01:37 +0100
committerJan Synacek <jsynacek@redhat.com>2014-01-09 15:26:52 +0100
commit67d5db8908afbd40bf290ad9e771453cc099abc5 (patch)
tree0757f19adb8c8d2c87bf1d50f2b2c132b2a11be6 /src/logicalfile/LMI_DirectoryContainsFileProvider.c
parent787f13127b8679e1ef52646cab0940d370d5145c (diff)
downloadopenlmi-providers-67d5db8908afbd40bf290ad9e771453cc099abc5.tar.gz
openlmi-providers-67d5db8908afbd40bf290ad9e771453cc099abc5.tar.xz
openlmi-providers-67d5db8908afbd40bf290ad9e771453cc099abc5.zip
logicalfile: replace thread-unsafe functions
Some functions, such as readdir() and dirname(), are not thread-safe. Use equivalent glib functions. trac: #173
Diffstat (limited to 'src/logicalfile/LMI_DirectoryContainsFileProvider.c')
-rw-r--r--src/logicalfile/LMI_DirectoryContainsFileProvider.c171
1 files changed, 92 insertions, 79 deletions
diff --git a/src/logicalfile/LMI_DirectoryContainsFileProvider.c b/src/logicalfile/LMI_DirectoryContainsFileProvider.c
index 6e1ddc5..06810b7 100644
--- a/src/logicalfile/LMI_DirectoryContainsFileProvider.c
+++ b/src/logicalfile/LMI_DirectoryContainsFileProvider.c
@@ -18,6 +18,7 @@
* Authors: Jan Synacek <jsynacek@redhat.com>
*/
#include <konkret/konkret.h>
+#include <glib.h>
#include "LMI_DirectoryContainsFile.h"
#include "LMI_UnixDirectory.h"
#include "CIM_LogicalFile.h"
@@ -31,6 +32,47 @@ static const CMPIBroker* _cb;
/* const unsigned int MAX_REFS = 65536; */
const unsigned int MAX_REFS = 4096;
+static CMPIStatus logicalfile_objectpath_from_path(
+ const char *abspath,
+ const char *resultClass,
+ const char *namespace,
+ CMPIObjectPath **o)
+{
+ struct stat sb;
+ CMPIStatus st = {.rc = CMPI_RC_OK};
+
+ char fileclass[BUFLEN];
+ char *fsname;
+ char *fsclassname;
+
+ if (lstat(abspath, &sb) < 0) {
+ char err[BUFLEN];
+ snprintf(err, BUFLEN, "Can't stat file: %s", abspath);
+ CMReturnWithChars(_cb, CMPI_RC_ERR_NOT_FOUND, err);
+ } else {
+ get_class_from_stat(&sb, fileclass);
+ st = check_assoc_class(_cb, namespace, resultClass, fileclass);
+ if (st.rc == RC_ERR_CLASS_CHECK_FAILED) {
+ st.rc = CMPI_RC_OK;
+ *o = NULL;
+ return st;
+ }
+ /* check status again for other possible errors */
+ check_status(st);
+
+ st = get_fsinfo_from_stat(_cb, &sb, abspath, &fsclassname, &fsname);
+ check_status(st);
+ }
+
+ CIM_LogicalFileRef cim_lfr;
+ CIM_LogicalFileRef_Init(&cim_lfr, _cb, namespace);
+ fill_logicalfile(CIM_LogicalFileRef, &cim_lfr, abspath, fsclassname, fsname, fileclass);
+ *o = CIM_LogicalFileRef_ToObjectPath(&cim_lfr, &st);
+ CMSetClassName(*o, fileclass);
+
+ return st;
+}
+
static CMPIStatus dir_file_objectpaths(
const CMPIContext* cc,
const CMPIResult* cr,
@@ -43,82 +85,56 @@ static CMPIStatus dir_file_objectpaths(
CMPIObjectPath **ops,
unsigned int *count)
{
- CMPIObjectPath *o;
+ CMPIObjectPath *o = NULL;
CMPIStatus st = {.rc = CMPI_RC_OK};
unsigned int i = 0;
- struct stat sb;
- struct dirent *de;
- DIR *dp;
- dp = opendir(path);
- if (dp == NULL) {
- st.rc = CMPI_RC_ERR_NOT_FOUND;
- return st;
- }
- while ((de = readdir(dp))) {
- if (strcmp(de->d_name, ".") == 0) {
- continue;
- }
+ GError *gerror = NULL;
+ GDir *gdir;
+ const gchar *gde;
- char rpath[BUFLEN + 1]; /* \0 */
- char fileclass[BUFLEN];
- char *fsname;
- char *fsclassname;
+ gdir = g_dir_open(path, 0, &gerror);
+ if (gerror) {
+ st.rc = CMPI_RC_ERR_NOT_FOUND;
+ return st;
+ }
- 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);
+ /* since g_dir_read_name() doesn't go over '.' and '..', get the parent first */
+ /* 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)) {
+ gchar *dirname;
+ dirname = g_path_get_dirname(path);
+ st = logicalfile_objectpath_from_path(dirname, resultClass, namespace, &o);
+ g_free(dirname);
+ check_status(st);
+ if (o) {
+ ops[i++] = o;
}
+ }
- 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);
- if (st.rc == RC_ERR_CLASS_CHECK_FAILED) {
- st.rc = CMPI_RC_OK;
- continue;
- } else if (st.rc != CMPI_RC_OK) {
- goto done;
- }
- st = get_fsinfo_from_stat(_cb, &sb, rpath, &fsclassname, &fsname);
- if (st.rc != CMPI_RC_OK) {
- goto done;
- }
+ while ((gde = g_dir_read_name(gdir))) {
+ /* 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;
}
- CIM_LogicalFileRef cim_lfr;
- CIM_LogicalFileRef_Init(&cim_lfr, _cb, namespace);
- fill_logicalfile(CIM_LogicalFileRef, &cim_lfr, rpath, fsclassname, fsname, fileclass);
- o = CIM_LogicalFileRef_ToObjectPath(&cim_lfr, &st);
- CMSetClassName(o, fileclass);
+ gchar *rpath = g_build_filename(path, gde, NULL);
+ st = logicalfile_objectpath_from_path(rpath, resultClass, namespace, &o);
+ g_free(rpath);
- ops[i++] = o;
- free(fsname);
+ if (st.rc != CMPI_RC_OK) {
+ break;
+ }
+ if (o) {
+ ops[i++] = o;
+ }
}
*count = i;
-done:
- closedir(dp);
+
+ g_dir_close(gdir);
return st;
}
@@ -215,18 +231,16 @@ static CMPIStatus associators(
CIM_DirectoryRef lmi_dr;
CIM_DirectoryRef_Init(&lmi_dr, _cb, ns);
- char *aux = strdup(path);
- char *dir = dirname(aux);
+
char *fsname;
char *fsclassname;
st = get_fsinfo_from_path(_cb, path, &fsclassname, &fsname);
- if (st.rc != CMPI_RC_OK) {
- free(aux);
- return st;
- }
+ check_status(st);
- fill_logicalfile(CIM_DirectoryRef, &lmi_dr, dir, fsclassname, fsname, LMI_UnixDirectory_ClassName);
+ gchar *dirname = g_path_get_dirname(path);
+ fill_logicalfile(CIM_DirectoryRef, &lmi_dr, dirname, fsclassname, fsname, LMI_UnixDirectory_ClassName);
+ g_free(dirname);
o = CIM_DirectoryRef_ToObjectPath(&lmi_dr, &st);
CMSetClassName(o, LMI_UnixDirectory_ClassName);
@@ -236,7 +250,6 @@ static CMPIStatus associators(
ci = _cb->bft->getInstance(_cb, cc, o, properties, &st);
CMReturnInstance(cr, ci);
}
- free(aux);
free(fsname);
} else {
/* this association does not associate with given 'cop' class */
@@ -304,7 +317,7 @@ static CMPIStatus references(
/* PartComponent */
CMPIObjectPath *refs[MAX_REFS];
- unsigned int count;
+ unsigned int count = 0;
st = dir_file_objectpaths(cc, cr, NULL, group, -1, properties, ns, path, refs, &count);
check_status(st);
if (count > MAX_REFS) {
@@ -342,15 +355,14 @@ static CMPIStatus references(
LMI_DirectoryContainsFile_SetObjectPath_PartComponent(&lmi_dcf, o);
/* GroupComponent */
- char *aux = strdup(path);
- char *dir = dirname(aux);
- st = get_fsinfo_from_path(_cb, dir, &fsclassname, &fsname);
+ gchar *dirname = g_path_get_dirname(path);
+ st = get_fsinfo_from_path(_cb, dirname, &fsclassname, &fsname);
if (st.rc != CMPI_RC_OK) {
- free(aux);
+ g_free(dirname);
return st;
}
- fill_logicalfile(CIM_DirectoryRef, &lmi_dr, dir, fsclassname, fsname, LMI_UnixDirectory_ClassName);
+ fill_logicalfile(CIM_DirectoryRef, &lmi_dr, dirname, fsclassname, fsname, LMI_UnixDirectory_ClassName);
o = CIM_DirectoryRef_ToObjectPath(&lmi_dr, &st);
CMSetClassName(o, LMI_UnixDirectory_ClassName);
LMI_DirectoryContainsFile_SetObjectPath_GroupComponent(&lmi_dcf, o);
@@ -361,7 +373,7 @@ static CMPIStatus references(
ci = LMI_DirectoryContainsFile_ToInstance(&lmi_dcf, &st);
CMReturnInstance(cr, ci);
}
- free(aux);
+ g_free(dirname);
} else {
/* this association does not associate with given 'cop' class */
CMReturn(CMPI_RC_OK);
@@ -536,4 +548,5 @@ KONKRET_REGISTRATION(
/* vi: set et: */
/* Local Variables: */
/* indent-tabs-mode: nil */
+/* c-basic-offset: 4 */
/* End: */