Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/olpc/datastore/metadatareader.c
blob: ce6d38e934cb02175fa2b916c736c3ea5b762385 (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
#include "Python.h"

#include <dirent.h>

// TODO: put it in a place where python can use it when writing metadata
#define MAX_PROPERTY_LENGTH 500 * 1024

static PyObject *byte_array_type = NULL;

static PyObject *
metadatareader_retrieve(PyObject *unused, PyObject *args)
{
    PyObject *dict = NULL;
    PyObject *properties = NULL;
    const char *dir_path = NULL;
    char *metadata_path = NULL;
	DIR *dir_stream = NULL;
	struct dirent *dir_entry = NULL;
    char *file_path = NULL;
    FILE *file = NULL;
	char *value_buf = NULL;

    if (!PyArg_ParseTuple(args, "sO:retrieve", &dir_path, &properties))
        return NULL;

    // Build path to the metadata directory
    int metadata_path_size = strlen(dir_path) + 10;
    metadata_path = PyMem_Malloc(metadata_path_size);
	if (metadata_path == NULL) {
        PyErr_NoMemory();
        goto cleanup;
	}
    snprintf (metadata_path, metadata_path_size, "%s/%s", dir_path, "metadata");

    dir_stream = opendir (metadata_path);
	if (dir_stream == NULL) {
	    char buf[256];
	    snprintf(buf, sizeof(buf), "Couldn't open metadata directory %s",
	             metadata_path);
        PyErr_SetString(PyExc_IOError, buf);
        goto cleanup;
	}

    dict = PyDict_New();

	dir_entry = readdir(dir_stream);
    while (dir_entry != NULL) {
        long file_size;
        int file_path_size;
        PyObject *value = NULL;

        // Skip . and ..
        if (dir_entry->d_name[0] == '.' &&
            (strlen(dir_entry->d_name) == 1 || 
                (dir_entry->d_name[1] == '.' &&
                    strlen(dir_entry->d_name) == 2)))
			goto next_property;

        // Check if the property is in the properties list
        if ((properties != Py_None) && (PyList_Size(properties) > 0)) {
            int found = 0;
            int i;
            for (i = 0; i < PyList_Size(properties); i++) {
                PyObject *property = PyList_GetItem(properties, i);
                if (!strcmp (dir_entry->d_name, PyString_AsString (property))) {
                    found = 1;
                }
            }
            if (!found) {
                goto next_property;
            }
        }

        // Build path of the property file
        file_path_size = strlen(metadata_path) + 1 + strlen(dir_entry->d_name) +
                         1;
        file_path = PyMem_Malloc(file_path_size);
    	if (file_path == NULL) {
            PyErr_NoMemory();
            goto cleanup;
    	}
        snprintf (file_path, file_path_size, "%s/%s", metadata_path,
                  dir_entry->d_name);

        file = fopen(file_path, "r");
        if (file == NULL) {
    	    char buf[256];
    	    snprintf(buf, sizeof(buf), "Cannot open property file %s: %s",
    	             file_path, strerror(errno));
            PyErr_SetString(PyExc_IOError, buf);
            goto cleanup;
        }

        // Get file size
        fseek (file, 0, SEEK_END);
        file_size = ftell (file);
        rewind (file);

        if (file_size == 0) {
            // Empty property
            value = PyString_FromString("");
            if (value == NULL) {
                PyErr_SetString(PyExc_ValueError,
                                "Failed to convert value to python string");
                goto cleanup;
            }
        } else {
            if (file_size > MAX_PROPERTY_LENGTH) {
                PyErr_SetString(PyExc_ValueError, "Property file too big");
                goto cleanup;
            }

            // Read the whole file
        	value_buf = PyMem_Malloc(file_size);
        	if (value_buf == NULL) {
                PyErr_NoMemory();
                goto cleanup;
        	}
            long read_size = fread(value_buf, 1, file_size, file);
            if (read_size < file_size) {
        	    char buf[256];
        	    snprintf(buf, sizeof(buf),
        	             "Error while reading property file %s", file_path);
                PyErr_SetString(PyExc_IOError, buf);
                goto cleanup;
            }

            // Convert value to dbus.ByteArray
            PyObject *args = Py_BuildValue("(s#)", value_buf, file_size);
            value = PyObject_CallObject(byte_array_type, args);
            if (value == NULL) {
                PyErr_SetString(PyExc_ValueError,
                                "Failed to convert value to dbus.ByteArray");
                goto cleanup;
            }
        }

        // Add property to the metadata dict
        if (PyDict_SetItemString(dict, dir_entry->d_name, value) == -1) {
            PyErr_SetString(PyExc_ValueError,
                            "Failed to add property to dictionary");
            goto cleanup;
        }

    next_property:
        if (file_path) {
            PyMem_Free(file_path);
            file_path = NULL;
        }
        if (file) {
            fclose(file);
            file = NULL;
        }
        if (value_buf) {
            PyMem_Free(value_buf);
            value_buf = NULL;
        }

    	dir_entry = readdir(dir_stream);
    }

	closedir(dir_stream);

    return dict;

cleanup:
    if (file_path) {
        PyMem_Free(file_path);
    }
    if (value_buf) {
        PyMem_Free(value_buf);
    }
    if (dict) {
        Py_DECREF(dict);
    }
    if (file) {
        fclose(file);
    }
    if (dir_stream) {
    	closedir(dir_stream);
	}
    return NULL;        
}

static PyMethodDef metadatareader_functions[] = {
    {"retrieve", metadatareader_retrieve, METH_VARARGS, PyDoc_STR("Read a dictionary from a file")},
    {NULL, NULL, 0, NULL}
};

PyMODINIT_FUNC
initmetadatareader(void)
{
  PyObject* mod;
  mod = Py_InitModule("metadatareader", metadatareader_functions);
  
  PyObject *dbus_module = PyImport_ImportModule("dbus");
  byte_array_type = PyObject_GetAttrString(dbus_module, "ByteArray");
}