| 1 |
/* |
| 2 |
* Python bindings for the libmount library. |
| 3 |
* |
| 4 |
* Copyright (C) 2013, Red Hat, Inc. All rights reserved. |
| 5 |
* Written by Ondrej Oprala and Karel Zak |
| 6 |
* |
| 7 |
* This file is free software; you can redistribute it and/or |
| 8 |
* modify it under the terms of the GNU Lesser General Public |
| 9 |
* License as published by the Free Software Foundation; either |
| 10 |
* version 3 of the License, or (at your option) any later version. |
| 11 |
* |
| 12 |
* This file is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 |
* Lesser General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this program. If not, see <https://gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
#include "pylibmount.h" |
| 21 |
|
| 22 |
static PyMemberDef Table_members[] = { |
| 23 |
{ NULL } |
| 24 |
}; |
| 25 |
|
| 26 |
static int Table_set_parser_errcb(TableObject *self, PyObject *func, |
| 27 |
void *closure __attribute__((unused))) |
| 28 |
{ |
| 29 |
PyObject *tmp; |
| 30 |
|
| 31 |
if (!func) { |
| 32 |
PyErr_SetString(PyExc_TypeError, NODEL_ATTR); |
| 33 |
return -1; |
| 34 |
} |
| 35 |
|
| 36 |
if (!PyCallable_Check(func)) |
| 37 |
return -1; |
| 38 |
|
| 39 |
tmp = self->errcb; |
| 40 |
Py_INCREF(func); |
| 41 |
self->errcb = func; |
| 42 |
Py_XDECREF(tmp); |
| 43 |
return 0; |
| 44 |
} |
| 45 |
|
| 46 |
static PyObject *Table_get_intro_comment(TableObject *self, |
| 47 |
void *closure __attribute__((unused))) |
| 48 |
{ |
| 49 |
return PyObjectResultStr(mnt_table_get_intro_comment(self->tab)); |
| 50 |
} |
| 51 |
|
| 52 |
static int Table_set_intro_comment(TableObject *self, PyObject *value, |
| 53 |
void *closure __attribute__((unused))) |
| 54 |
{ |
| 55 |
char *comment = NULL; |
| 56 |
int rc = 0; |
| 57 |
|
| 58 |
if (!value) { |
| 59 |
PyErr_SetString(PyExc_TypeError, NODEL_ATTR); |
| 60 |
return -1; |
| 61 |
} |
| 62 |
if (!(comment = pystos(value))) |
| 63 |
return -1; |
| 64 |
|
| 65 |
if ((rc = mnt_table_set_intro_comment(self->tab, comment))) { |
| 66 |
UL_RaiseExc(-rc); |
| 67 |
return -1; |
| 68 |
} |
| 69 |
return 0; |
| 70 |
} |
| 71 |
|
| 72 |
static PyObject *Table_get_trailing_comment(TableObject *self, |
| 73 |
void *closure __attribute__((unused))) |
| 74 |
{ |
| 75 |
return PyObjectResultStr(mnt_table_get_trailing_comment(self->tab)); |
| 76 |
} |
| 77 |
|
| 78 |
static int Table_set_trailing_comment(TableObject *self, PyObject *value, |
| 79 |
void *closure __attribute__((unused))) |
| 80 |
{ |
| 81 |
char *comment = NULL; |
| 82 |
int rc = 0; |
| 83 |
|
| 84 |
if (!value) { |
| 85 |
PyErr_SetString(PyExc_TypeError, NODEL_ATTR); |
| 86 |
return -1; |
| 87 |
} |
| 88 |
if (!(comment = pystos(value))) |
| 89 |
return -1; |
| 90 |
|
| 91 |
if ((rc = mnt_table_set_trailing_comment(self->tab, comment))) { |
| 92 |
UL_RaiseExc(-rc); |
| 93 |
return -1; |
| 94 |
} |
| 95 |
return 0; |
| 96 |
} |
| 97 |
|
| 98 |
#define Table_enable_comments_HELP "enable_comments(enable)\n\n" \ |
| 99 |
"Enables parsing of comments.\n\n" \ |
| 100 |
"The initial (intro) file comment is accessible by\n" \ |
| 101 |
"Tab.intro_comment. The intro and the comment of the first fstab" \ |
| 102 |
"entry has to be separated by blank line. The filesystem comments are\n" \ |
| 103 |
"accessible by Fs.comment. The trailing fstab comment is accessible\n" \ |
| 104 |
"by Tab.trailing_comment.\n" \ |
| 105 |
"\n" \ |
| 106 |
"<informalexample>\n" \ |
| 107 |
"<programlisting>\n" \ |
| 108 |
"#\n" \ |
| 109 |
"# Intro comment\n" \ |
| 110 |
"#\n" \ |
| 111 |
"\n" \ |
| 112 |
"# this comments belongs to the first fs\n" \ |
| 113 |
"LABEL=foo /mnt/foo auto defaults 1 2\n" \ |
| 114 |
"# this comments belongs to the second fs\n" \ |
| 115 |
"LABEL=bar /mnt/bar auto defaults 1 2 \n" \ |
| 116 |
"# trailing comment\n" \ |
| 117 |
"</programlisting>\n" \ |
| 118 |
"</informalexample>" |
| 119 |
static PyObject *Table_enable_comments(TableObject *self, PyObject *args, |
| 120 |
PyObject *kwds) |
| 121 |
{ |
| 122 |
int enable = 0; |
| 123 |
char *kwlist[] = {"enable", NULL}; |
| 124 |
|
| 125 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "i", kwlist, &enable)) { |
| 126 |
PyErr_SetString(PyExc_TypeError, ARG_ERR); |
| 127 |
return NULL; |
| 128 |
} |
| 129 |
mnt_table_enable_comments(self->tab, enable); |
| 130 |
Py_INCREF(self); |
| 131 |
return (PyObject *)self; |
| 132 |
} |
| 133 |
|
| 134 |
#define Table_replace_file_HELP "replace_file(filename)\n\n" \ |
| 135 |
"This function replaces filename with the new content from TableObject." |
| 136 |
static PyObject *Table_replace_file(TableObject *self, PyObject *args, PyObject *kwds) |
| 137 |
{ |
| 138 |
int rc; |
| 139 |
char *filename = NULL; |
| 140 |
char *kwlist[] = {"filename", NULL}; |
| 141 |
|
| 142 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "s", kwlist, &filename)) { |
| 143 |
PyErr_SetString(PyExc_TypeError, ARG_ERR); |
| 144 |
return NULL; |
| 145 |
} |
| 146 |
rc = mnt_table_replace_file(self->tab, filename); |
| 147 |
return rc ? UL_RaiseExc(-rc) : UL_IncRef(self); |
| 148 |
} |
| 149 |
|
| 150 |
#define Table_write_file_HELP "write_file(path)\n\n" \ |
| 151 |
"This function writes tab to file(stream)" |
| 152 |
static PyObject *Table_write_file(TableObject *self, PyObject *args, PyObject *kwds) |
| 153 |
{ |
| 154 |
int rc; |
| 155 |
//PyObject *stream = NULL; |
| 156 |
FILE *f = NULL; |
| 157 |
char *path = NULL; |
| 158 |
char *kwlist[] = {"path", NULL}; |
| 159 |
|
| 160 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "s", kwlist, |
| 161 |
&path)) { |
| 162 |
PyErr_SetString(PyExc_TypeError, ARG_ERR); |
| 163 |
return NULL; |
| 164 |
} |
| 165 |
if (!(f = fopen(path, "w"))) |
| 166 |
return UL_RaiseExc(errno); |
| 167 |
rc = mnt_table_write_file(self->tab, f); |
| 168 |
fclose(f); |
| 169 |
return rc ? UL_RaiseExc(-rc) : UL_IncRef(self); |
| 170 |
} |
| 171 |
|
| 172 |
#define Table_find_devno_HELP "find_devno(devno, [direction])\n\n" \ |
| 173 |
"Note that zero could be valid device number for root pseudo " \ |
| 174 |
"filesystem (e.g. tmpfs)\n" \ |
| 175 |
"Returns a tab entry or None" |
| 176 |
static PyObject *Table_find_devno(TableObject *self, PyObject *args, PyObject *kwds) |
| 177 |
{ |
| 178 |
dev_t devno; |
| 179 |
int direction = MNT_ITER_BACKWARD; |
| 180 |
char *kwlist[] = {"devno", "direction", NULL}; |
| 181 |
|
| 182 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "I|i", kwlist, &devno, &direction)) { |
| 183 |
PyErr_SetString(PyExc_TypeError, ARG_ERR); |
| 184 |
return NULL; |
| 185 |
} |
| 186 |
return PyObjectResultFs(mnt_table_find_devno(self->tab, devno, direction)); |
| 187 |
} |
| 188 |
|
| 189 |
#define Table_find_mountpoint_HELP "find_mountpoint(path, [direction])\n\n" \ |
| 190 |
"Returns a tab entry or None." |
| 191 |
static PyObject *Table_find_mountpoint(TableObject *self, PyObject *args, PyObject *kwds) |
| 192 |
{ |
| 193 |
char *path; |
| 194 |
int direction = MNT_ITER_BACKWARD; |
| 195 |
char *kwlist[] = {"path", "direction", NULL}; |
| 196 |
|
| 197 |
if (! PyArg_ParseTupleAndKeywords(args, kwds, "s|i", kwlist, &path, &direction)) { |
| 198 |
PyErr_SetString(PyExc_TypeError, ARG_ERR); |
| 199 |
return NULL; |
| 200 |
} |
| 201 |
return PyObjectResultFs(mnt_table_find_mountpoint(self->tab, path, direction)); |
| 202 |
} |
| 203 |
|
| 204 |
#define Table_find_pair_HELP "find_pair(source, target, [direction])\n\n" \ |
| 205 |
"Returns a tab entry or None." |
| 206 |
static PyObject *Table_find_pair(TableObject *self, PyObject *args, PyObject *kwds) |
| 207 |
{ |
| 208 |
char *kwlist[] = {"source", "target", "direction", NULL}; |
| 209 |
char *source; |
| 210 |
char *target; |
| 211 |
int direction = MNT_ITER_BACKWARD; |
| 212 |
|
| 213 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "ss|i", kwlist, &source, &target, &direction)) { |
| 214 |
PyErr_SetString(PyExc_TypeError, ARG_ERR); |
| 215 |
return NULL; |
| 216 |
} |
| 217 |
return PyObjectResultFs(mnt_table_find_pair(self->tab, source, target, direction)); |
| 218 |
} |
| 219 |
|
| 220 |
#define Table_find_source_HELP "find_source(source, [direction])\n\n" \ |
| 221 |
"Returns a tab entry or None." |
| 222 |
static PyObject *Table_find_source(TableObject *self, PyObject *args, PyObject *kwds) |
| 223 |
{ |
| 224 |
char *kwlist[] = {"source", "direction", NULL}; |
| 225 |
char *source; |
| 226 |
int direction = MNT_ITER_BACKWARD; |
| 227 |
|
| 228 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|i", kwlist, &source, &direction)) { |
| 229 |
PyErr_SetString(PyExc_TypeError, ARG_ERR); |
| 230 |
return NULL; |
| 231 |
} |
| 232 |
return PyObjectResultFs(mnt_table_find_source(self->tab, source, direction)); |
| 233 |
} |
| 234 |
|
| 235 |
#define Table_find_target_HELP "find_target(target, [direction])\n\n" \ |
| 236 |
"Try to lookup an entry in given tab, possible are three iterations, first\n" \ |
| 237 |
"with path, second with realpath(path) and third with realpath(path)\n" \ |
| 238 |
"against realpath(fs->target). The 2nd and 3rd iterations are not performed\n" \ |
| 239 |
"when tb cache is not set (cache not implemented yet).\n" \ |
| 240 |
"\n" \ |
| 241 |
"Returns a tab entry or None." |
| 242 |
static PyObject *Table_find_target(TableObject *self, PyObject *args, PyObject *kwds) |
| 243 |
{ |
| 244 |
char *kwlist[] = {"target", "direction", NULL}; |
| 245 |
char *target; |
| 246 |
int direction = MNT_ITER_BACKWARD; |
| 247 |
|
| 248 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|i", kwlist, &target, &direction)) { |
| 249 |
PyErr_SetString(PyExc_TypeError, ARG_ERR); |
| 250 |
return NULL; |
| 251 |
} |
| 252 |
return PyObjectResultFs(mnt_table_find_target(self->tab, target, direction)); |
| 253 |
} |
| 254 |
|
| 255 |
#define Table_find_srcpath_HELP "find_srcpath(srcpath, [direction])\n\n" \ |
| 256 |
"Try to lookup an entry in given tab, possible are four iterations, first\n" \ |
| 257 |
"with path, second with realpath(path), third with tags (LABEL, UUID, ..)\n" \ |
| 258 |
"from path and fourth with realpath(path) against realpath(entry->srcpath).\n" \ |
| 259 |
"\n" \ |
| 260 |
"The 2nd, 3rd and 4th iterations are not performed when tb cache is not\n" \ |
| 261 |
"set (not implemented yet).\n" \ |
| 262 |
"\n" \ |
| 263 |
"Note that None is a valid source path; it will be replaced with \"none\". The\n" \ |
| 264 |
"\"none\" is used in /proc/{mounts,self/mountinfo} for pseudo filesystems.\n" \ |
| 265 |
"\n" \ |
| 266 |
"Returns a tab entry or None." |
| 267 |
static PyObject *Table_find_srcpath(TableObject *self, PyObject *args, PyObject *kwds) |
| 268 |
{ |
| 269 |
char *kwlist[] = {"srcpath", "direction", NULL}; |
| 270 |
char *srcpath; |
| 271 |
int direction = MNT_ITER_BACKWARD; |
| 272 |
|
| 273 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "s|i", kwlist, &srcpath, &direction)) { |
| 274 |
PyErr_SetString(PyExc_TypeError, ARG_ERR); |
| 275 |
return NULL; |
| 276 |
} |
| 277 |
return PyObjectResultFs(mnt_table_find_srcpath(self->tab, srcpath, direction)); |
| 278 |
} |
| 279 |
|
| 280 |
#define Table_find_tag_HELP "find_tag(tag, val, [direction])\n\n" \ |
| 281 |
"Try to lookup an entry in given tab, first attempt is lookup by tag and\n" \ |
| 282 |
"val, for the second attempt the tag is evaluated (converted to the device\n" \ |
| 283 |
"name) and Tab.find_srcpath() is performed. The second attempt is not\n" \ |
| 284 |
"performed when tb cache is not set (not implemented yet).\n" \ |
| 285 |
"\n" \ |
| 286 |
"Returns a tab entry or NULL." |
| 287 |
static PyObject *Table_find_tag(TableObject *self, PyObject *args, PyObject *kwds) |
| 288 |
{ |
| 289 |
char *kwlist[] = {"tag", "val", "direction", NULL}; |
| 290 |
char *tag; |
| 291 |
char *val; |
| 292 |
int direction = MNT_ITER_BACKWARD; |
| 293 |
|
| 294 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "ss|i", kwlist, &tag, &val, &direction)) { |
| 295 |
PyErr_SetString(PyExc_TypeError, ARG_ERR); |
| 296 |
return NULL; |
| 297 |
} |
| 298 |
return PyObjectResultFs(mnt_table_find_tag(self->tab, tag, val, direction)); |
| 299 |
} |
| 300 |
|
| 301 |
static PyObject *Table_get_nents(TableObject *self) |
| 302 |
{ |
| 303 |
return PyObjectResultInt(mnt_table_get_nents(self->tab)); |
| 304 |
} |
| 305 |
|
| 306 |
#define Table_is_fs_mounted_HELP "is_fs_mounted(fstab_fs)\n\n" \ |
| 307 |
"Checks if the fstab_fs entry is already in the tb table. The \"swap\" is\n" \ |
| 308 |
"ignored. This function explicitly compares source, target and root of the\n" \ |
| 309 |
"filesystems.\n" \ |
| 310 |
"\n" \ |
| 311 |
"Note that source and target are canonicalized only if a cache for tb is\n" \ |
| 312 |
"defined (not implemented yet). The target canonicalization may\n" \ |
| 313 |
"trigger automount on autofs mountpoints!\n" \ |
| 314 |
"\n" \ |
| 315 |
"Don't use it if you want to know if a device is mounted, just use\n" \ |
| 316 |
"Tab.find_source() for the device.\n" \ |
| 317 |
"\n" \ |
| 318 |
"This function is designed mostly for \"mount -a\".\n" \ |
| 319 |
"\n" \ |
| 320 |
"Returns a boolean value." |
| 321 |
static PyObject *Table_is_fs_mounted(TableObject *self, PyObject *args, PyObject *kwds) |
| 322 |
{ |
| 323 |
FsObject *fs; |
| 324 |
char *kwlist[] = {"fstab_fs", NULL}; |
| 325 |
|
| 326 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!", kwlist, &FsType, &fs)) { |
| 327 |
PyErr_SetString(PyExc_TypeError, ARG_ERR); |
| 328 |
return NULL; |
| 329 |
} |
| 330 |
return PyBool_FromLong(mnt_table_is_fs_mounted(self->tab, fs->fs)); |
| 331 |
} |
| 332 |
|
| 333 |
#define Table_parse_file_HELP "parse_file(file)\n\n" \ |
| 334 |
"Parses whole table (e.g. /etc/mtab) and appends new records to the tab.\n" \ |
| 335 |
"\n" \ |
| 336 |
"The libmount parser ignores broken (syntax error) lines, these lines are\n" \ |
| 337 |
"reported to caller by errcb() function (see Tab.parser_errcb).\n" \ |
| 338 |
"\n" \ |
| 339 |
"Returns self or raises an exception in case of an error." |
| 340 |
static PyObject *Table_parse_file(TableObject *self, PyObject* args, PyObject *kwds) |
| 341 |
{ |
| 342 |
int rc; |
| 343 |
char *file = NULL; |
| 344 |
char *kwlist[] = {"file", NULL}; |
| 345 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "s", kwlist, &file)) { |
| 346 |
PyErr_SetString(PyExc_TypeError, ARG_ERR); |
| 347 |
return NULL; |
| 348 |
} |
| 349 |
rc = mnt_table_parse_file(self->tab, file); |
| 350 |
return rc ? UL_RaiseExc(-rc) : UL_IncRef(self); |
| 351 |
} |
| 352 |
|
| 353 |
#define Table_parse_fstab_HELP "parse_fstab([fstab])\n\n" \ |
| 354 |
"This function parses /etc/fstab and appends new lines to the tab. If the\n" \ |
| 355 |
"filename is a directory then Tab.parse_dir() is called.\n" \ |
| 356 |
"\n" \ |
| 357 |
"See also Tab.parser_errcb.\n" \ |
| 358 |
"\n" \ |
| 359 |
"Returns self or raises an exception in case of an error." |
| 360 |
static PyObject *Table_parse_fstab(TableObject *self, PyObject* args, PyObject *kwds) |
| 361 |
{ |
| 362 |
int rc; |
| 363 |
char *fstab = NULL; |
| 364 |
char *kwlist[] = {"fstab", NULL}; |
| 365 |
|
| 366 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|s", kwlist, &fstab)) { |
| 367 |
PyErr_SetString(PyExc_TypeError, ARG_ERR); |
| 368 |
return NULL; |
| 369 |
} |
| 370 |
rc = mnt_table_parse_fstab(self->tab, fstab); |
| 371 |
return rc ? UL_RaiseExc(-rc) : UL_IncRef(self); |
| 372 |
} |
| 373 |
|
| 374 |
#define Table_parse_mtab_HELP "parse_mtab([mtab])\n\n" \ |
| 375 |
"This function parses /etc/mtab or /proc/self/mountinfo\n" \ |
| 376 |
"/run/mount/utabs or /proc/mounts.\n" \ |
| 377 |
"\n" \ |
| 378 |
"See also Tab.parser_errcb().\n" \ |
| 379 |
"\n" \ |
| 380 |
"Returns self or raises an exception in case of an error." |
| 381 |
static PyObject *Table_parse_mtab(TableObject *self, PyObject* args, PyObject *kwds) |
| 382 |
{ |
| 383 |
int rc; |
| 384 |
char *mtab = NULL; |
| 385 |
char *kwlist[] = {"mtab", NULL}; |
| 386 |
|
| 387 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|s", kwlist, &mtab)) { |
| 388 |
PyErr_SetString(PyExc_TypeError, ARG_ERR); |
| 389 |
return NULL; |
| 390 |
} |
| 391 |
rc = mnt_table_parse_mtab(self->tab, mtab); |
| 392 |
return rc ? UL_RaiseExc(-rc) : UL_IncRef(self); |
| 393 |
} |
| 394 |
|
| 395 |
#define Table_parse_dir_HELP "parse_dir(dir)\n\n" \ |
| 396 |
"The directory:\n" \ |
| 397 |
"- files are sorted by strverscmp(3)\n" \ |
| 398 |
"- files that start with \".\" are ignored (e.g. \".10foo.fstab\")\n" \ |
| 399 |
"- files without the \".fstab\" extension are ignored\n" \ |
| 400 |
"\n" \ |
| 401 |
"Returns self or raises an exception in case of an error." |
| 402 |
static PyObject *Table_parse_dir(TableObject *self, PyObject* args, PyObject *kwds) |
| 403 |
{ |
| 404 |
int rc; |
| 405 |
char *dir = NULL; |
| 406 |
char *kwlist[] = {"dir", NULL}; |
| 407 |
|
| 408 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "s", kwlist, &dir)) { |
| 409 |
PyErr_SetString(PyExc_TypeError, ARG_ERR); |
| 410 |
return NULL; |
| 411 |
} |
| 412 |
rc = mnt_table_parse_dir(self->tab, dir); |
| 413 |
return rc ? UL_RaiseExc(-rc) : UL_IncRef(self); |
| 414 |
} |
| 415 |
|
| 416 |
#define Table_parse_swaps_HELP "parse_swaps(swaps)\n\n" \ |
| 417 |
"This function parses /proc/swaps and appends new lines to the tab" |
| 418 |
static PyObject *Table_parse_swaps(TableObject *self, PyObject* args, PyObject *kwds) |
| 419 |
{ |
| 420 |
int rc; |
| 421 |
char *swaps = NULL; |
| 422 |
char *kwlist[] = {"swaps", NULL}; |
| 423 |
|
| 424 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "s", kwlist, &swaps)) { |
| 425 |
PyErr_SetString(PyExc_TypeError, ARG_ERR); |
| 426 |
return NULL; |
| 427 |
} |
| 428 |
rc = mnt_table_parse_swaps(self->tab, swaps); |
| 429 |
return rc ? UL_RaiseExc(-rc) : UL_IncRef(self); |
| 430 |
} |
| 431 |
|
| 432 |
#define Table_add_fs_HELP "add_fs(fs)\n\nAdds a new entry to tab.\n" \ |
| 433 |
"Returns self or raises an exception in case of an error." |
| 434 |
|
| 435 |
static PyObject *Table_add_fs(TableObject *self, PyObject* args, PyObject *kwds) |
| 436 |
{ |
| 437 |
int rc; |
| 438 |
FsObject *fs = NULL; |
| 439 |
char *kwlist[] = {"fs", NULL}; |
| 440 |
|
| 441 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!", kwlist, &FsType, &fs)) { |
| 442 |
PyErr_SetString(PyExc_TypeError, ARG_ERR); |
| 443 |
return NULL; |
| 444 |
} |
| 445 |
Py_INCREF(fs); |
| 446 |
rc = mnt_table_add_fs(self->tab, fs->fs); |
| 447 |
return rc ? UL_RaiseExc(-rc) : UL_IncRef(self); |
| 448 |
} |
| 449 |
|
| 450 |
#define Table_remove_fs_HELP "remove_fs(fs)\n\n" \ |
| 451 |
"Returns self or raises an exception in case of an error." |
| 452 |
static PyObject *Table_remove_fs(TableObject *self, PyObject* args, PyObject *kwds) |
| 453 |
{ |
| 454 |
int rc; |
| 455 |
FsObject *fs = NULL; |
| 456 |
char *kwlist[] = {"fs", NULL}; |
| 457 |
|
| 458 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!", kwlist, &FsType, &fs)) { |
| 459 |
PyErr_SetString(PyExc_TypeError, ARG_ERR); |
| 460 |
return NULL; |
| 461 |
} |
| 462 |
rc = mnt_table_remove_fs(self->tab, fs->fs); |
| 463 |
Py_DECREF(fs); |
| 464 |
return rc ? UL_RaiseExc(-rc) : UL_IncRef(self); |
| 465 |
} |
| 466 |
|
| 467 |
#define Table_next_fs_HELP "next_fs()\n\n" \ |
| 468 |
"Returns the next Fs on success, raises an exception in case " \ |
| 469 |
"of an error and None at end of list.\n" \ |
| 470 |
"\n" \ |
| 471 |
"Example:\n" \ |
| 472 |
"<informalexample>\n" \ |
| 473 |
"<programlisting>\n" \ |
| 474 |
"import libmount\n" \ |
| 475 |
"import functools\n" \ |
| 476 |
"for fs in iter(functools.partial(tb.next_fs), None):\n" \ |
| 477 |
" dir = Fs.target\n" \ |
| 478 |
" print \"mount point: {:s}\".format(dir)\n" \ |
| 479 |
"\n" \ |
| 480 |
"</programlisting>\n" \ |
| 481 |
"</informalexample>\n" \ |
| 482 |
"\n" \ |
| 483 |
"lists all mountpoints from fstab in backward order." |
| 484 |
static PyObject *Table_next_fs(TableObject *self) |
| 485 |
{ |
| 486 |
struct libmnt_fs *fs; |
| 487 |
int rc; |
| 488 |
|
| 489 |
/* Reset the builtin iterator after reaching the end of the list */ |
| 490 |
rc = mnt_table_next_fs(self->tab, self->iter, &fs); |
| 491 |
if (rc == 1) { |
| 492 |
mnt_reset_iter(self->iter, MNT_ITER_FORWARD); |
| 493 |
Py_RETURN_NONE; |
| 494 |
} |
| 495 |
|
| 496 |
if (rc) |
| 497 |
return UL_RaiseExc(-rc); |
| 498 |
|
| 499 |
return PyObjectResultFs(fs); |
| 500 |
} |
| 501 |
|
| 502 |
static PyMethodDef Table_methods[] = { |
| 503 |
{"enable_comments", (PyCFunction)Table_enable_comments, METH_VARARGS|METH_KEYWORDS, Table_enable_comments_HELP}, |
| 504 |
{"find_pair", (PyCFunction)Table_find_pair, METH_VARARGS|METH_KEYWORDS, Table_find_pair_HELP}, |
| 505 |
{"find_source", (PyCFunction)Table_find_source, METH_VARARGS|METH_KEYWORDS, Table_find_source_HELP}, |
| 506 |
{"find_srcpath", (PyCFunction)Table_find_srcpath, METH_VARARGS|METH_KEYWORDS, Table_find_srcpath_HELP}, |
| 507 |
{"find_tag", (PyCFunction)Table_find_tag, METH_VARARGS|METH_KEYWORDS, Table_find_tag_HELP}, |
| 508 |
{"find_target", (PyCFunction)Table_find_target, METH_VARARGS|METH_KEYWORDS, Table_find_target_HELP}, |
| 509 |
{"find_devno", (PyCFunction)Table_find_devno, METH_VARARGS|METH_KEYWORDS, Table_find_devno_HELP}, |
| 510 |
{"find_mountpoint", (PyCFunction)Table_find_mountpoint, METH_VARARGS|METH_KEYWORDS, Table_find_mountpoint_HELP}, |
| 511 |
{"parse_file", (PyCFunction)Table_parse_file, METH_VARARGS|METH_KEYWORDS, Table_parse_file_HELP}, |
| 512 |
{"parse_fstab", (PyCFunction)Table_parse_fstab, METH_VARARGS|METH_KEYWORDS, Table_parse_fstab_HELP}, |
| 513 |
{"parse_mtab", (PyCFunction)Table_parse_mtab, METH_VARARGS|METH_KEYWORDS, Table_parse_mtab_HELP}, |
| 514 |
{"parse_dir", (PyCFunction)Table_parse_dir, METH_VARARGS|METH_KEYWORDS, Table_parse_dir_HELP}, |
| 515 |
{"parse_swaps", (PyCFunction)Table_parse_swaps, METH_VARARGS|METH_KEYWORDS, Table_parse_swaps_HELP}, |
| 516 |
{"is_fs_mounted", (PyCFunction)Table_is_fs_mounted, METH_VARARGS|METH_KEYWORDS, Table_is_fs_mounted_HELP}, |
| 517 |
{"add_fs", (PyCFunction)Table_add_fs, METH_VARARGS|METH_KEYWORDS, Table_add_fs_HELP}, |
| 518 |
{"remove_fs", (PyCFunction)Table_remove_fs, METH_VARARGS|METH_KEYWORDS, Table_remove_fs_HELP}, |
| 519 |
{"next_fs", (PyCFunction)Table_next_fs, METH_NOARGS, Table_next_fs_HELP}, |
| 520 |
{"write_file", (PyCFunction)Table_write_file, METH_VARARGS|METH_KEYWORDS, Table_write_file_HELP}, |
| 521 |
{"replace_file", (PyCFunction)Table_replace_file, METH_VARARGS|METH_KEYWORDS, Table_replace_file_HELP}, |
| 522 |
{NULL} |
| 523 |
}; |
| 524 |
|
| 525 |
/* mnt_free_tab() with a few necessary additions */ |
| 526 |
void Table_unref(struct libmnt_table *tab) |
| 527 |
{ |
| 528 |
struct libmnt_fs *fs; |
| 529 |
struct libmnt_iter *iter; |
| 530 |
|
| 531 |
if (!tab) |
| 532 |
return; |
| 533 |
|
| 534 |
DBG(TAB, pymnt_debug_h(tab, "un-referencing filesystems")); |
| 535 |
|
| 536 |
iter = mnt_new_iter(MNT_ITER_BACKWARD); |
| 537 |
|
| 538 |
/* remove pylibmount specific references to the entries */ |
| 539 |
while (mnt_table_next_fs(tab, iter, &fs) == 0) |
| 540 |
Py_XDECREF(mnt_fs_get_userdata(fs)); |
| 541 |
|
| 542 |
DBG(TAB, pymnt_debug_h(tab, "un-referencing table")); |
| 543 |
|
| 544 |
mnt_unref_table(tab); |
| 545 |
mnt_free_iter(iter); |
| 546 |
} |
| 547 |
|
| 548 |
static void Table_destructor(TableObject *self) |
| 549 |
{ |
| 550 |
DBG(TAB, pymnt_debug_h(self->tab, "destructor py-obj: %p, py-refcnt=%d", |
| 551 |
self, (int) Py_REFCNT((PyObject *) self))); |
| 552 |
Table_unref(self->tab); |
| 553 |
self->tab = NULL; |
| 554 |
|
| 555 |
mnt_free_iter(self->iter); |
| 556 |
Py_XDECREF(self->errcb); |
| 557 |
PyFree(self); |
| 558 |
} |
| 559 |
|
| 560 |
static PyObject *Table_new(PyTypeObject *type, |
| 561 |
PyObject *args __attribute__((unused)), |
| 562 |
PyObject *kwds __attribute__((unused))) |
| 563 |
{ |
| 564 |
TableObject *self = (TableObject*)type->tp_alloc(type, 0); |
| 565 |
|
| 566 |
if (self) { |
| 567 |
DBG(TAB, pymnt_debug_h(self, "new")); |
| 568 |
|
| 569 |
self->tab = NULL; |
| 570 |
self->iter = NULL; |
| 571 |
self->errcb = NULL; |
| 572 |
} |
| 573 |
return (PyObject *)self; |
| 574 |
} |
| 575 |
|
| 576 |
/* explicit tab.__init__() serves as mnt_reset_table(tab) would in C |
| 577 |
* and as mnt_new_table{,_from_dir,_from_file}() with proper arguments */ |
| 578 |
#define Table_HELP "Table(path=None, errcb=None)" |
| 579 |
static int Table_init(TableObject *self, PyObject *args, PyObject *kwds) |
| 580 |
{ |
| 581 |
struct libmnt_cache *cache; |
| 582 |
char *path = NULL; |
| 583 |
char *kwlist[] = {"path", "errcb", NULL}; |
| 584 |
PyObject *errcb = NULL; |
| 585 |
struct stat buf; |
| 586 |
|
| 587 |
memset (&buf, 0, sizeof(struct stat)); |
| 588 |
|
| 589 |
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|sO", |
| 590 |
kwlist, &path, &errcb)) |
| 591 |
return -1; |
| 592 |
|
| 593 |
DBG(TAB, pymnt_debug_h(self, "init")); |
| 594 |
|
| 595 |
Table_unref(self->tab); |
| 596 |
self->tab = NULL; |
| 597 |
|
| 598 |
if (self->iter) |
| 599 |
mnt_reset_iter(self->iter, MNT_ITER_FORWARD); |
| 600 |
else |
| 601 |
self->iter = mnt_new_iter(MNT_ITER_FORWARD); |
| 602 |
|
| 603 |
if (errcb) { |
| 604 |
PyObject *tmp; |
| 605 |
if (!PyCallable_Check(errcb)) |
| 606 |
return -1; |
| 607 |
tmp = self->errcb; |
| 608 |
Py_INCREF(errcb); |
| 609 |
self->errcb = errcb; |
| 610 |
Py_XDECREF(tmp); |
| 611 |
} else { |
| 612 |
Py_XDECREF(self->errcb); |
| 613 |
self->errcb = NULL; |
| 614 |
} |
| 615 |
|
| 616 |
if (path) { |
| 617 |
DBG(TAB, pymnt_debug_h(self, "init: path defined (%s)", path)); |
| 618 |
|
| 619 |
if (stat(path, &buf)) { |
| 620 |
/* TODO: weird */ |
| 621 |
PyErr_SetFromErrno(PyExc_RuntimeError); |
| 622 |
return -1; |
| 623 |
} |
| 624 |
if (S_ISREG(buf.st_mode)) |
| 625 |
self->tab = mnt_new_table_from_file(path); |
| 626 |
else if (S_ISDIR(buf.st_mode)) |
| 627 |
self->tab = mnt_new_table_from_dir(path); |
| 628 |
} else { |
| 629 |
DBG(TAB, pymnt_debug_h(self, "init: allocate empty table")); |
| 630 |
self->tab = mnt_new_table(); |
| 631 |
} |
| 632 |
|
| 633 |
/* Always set custom handler when using libmount from python */ |
| 634 |
mnt_table_set_parser_errcb(self->tab, pymnt_table_parser_errcb); |
| 635 |
mnt_table_set_userdata(self->tab, self); |
| 636 |
|
| 637 |
cache = mnt_new_cache(); /* TODO: make it optional? */ |
| 638 |
if (!cache) |
| 639 |
return -1; |
| 640 |
mnt_table_set_cache(self->tab, cache); |
| 641 |
mnt_unref_cache(cache); |
| 642 |
|
| 643 |
return 0; |
| 644 |
} |
| 645 |
|
| 646 |
/* Handler for the tab->errcb callback */ |
| 647 |
int pymnt_table_parser_errcb(struct libmnt_table *tb, const char *filename, int line) |
| 648 |
{ |
| 649 |
int rc = 0; |
| 650 |
PyObject *obj; |
| 651 |
|
| 652 |
obj = mnt_table_get_userdata(tb); |
| 653 |
if (obj && ((TableObject*) obj)->errcb) { |
| 654 |
PyObject *arglist, *result; |
| 655 |
|
| 656 |
arglist = Py_BuildValue("(Osi)", obj, filename, line); |
| 657 |
if (!arglist) |
| 658 |
return -ENOMEM; |
| 659 |
|
| 660 |
/* A python callback was set, so tb is definitely encapsulated in an object */ |
| 661 |
result = PyObject_Call(((TableObject *)obj)->errcb, arglist, NULL); |
| 662 |
Py_DECREF(arglist); |
| 663 |
|
| 664 |
if (!result) |
| 665 |
return -EINVAL; |
| 666 |
if (!PyArg_Parse(result, "i", &rc)) |
| 667 |
rc = -EINVAL; |
| 668 |
Py_DECREF(result); |
| 669 |
} |
| 670 |
return rc; |
| 671 |
} |
| 672 |
|
| 673 |
PyObject *PyObjectResultTab(struct libmnt_table *tab) |
| 674 |
{ |
| 675 |
TableObject *result; |
| 676 |
|
| 677 |
if (!tab) { |
| 678 |
PyErr_SetString(LibmountError, "internal exception"); |
| 679 |
return NULL; |
| 680 |
} |
| 681 |
|
| 682 |
result = mnt_table_get_userdata(tab); |
| 683 |
if (result) { |
| 684 |
Py_INCREF(result); |
| 685 |
DBG(TAB, pymnt_debug_h(tab, "result py-obj %p: already exists, py-refcnt=%d", |
| 686 |
result, (int) Py_REFCNT((PyObject *) result))); |
| 687 |
return (PyObject *) result; |
| 688 |
} |
| 689 |
|
| 690 |
/* Creating an encapsulating object: increment the refcount, so that |
| 691 |
* code such as: cxt.get_fstab() doesn't call the destructor, which |
| 692 |
* would free our tab struct as well |
| 693 |
*/ |
| 694 |
result = PyObject_New(TableObject, &TableType); |
| 695 |
if (!result) { |
| 696 |
UL_RaiseExc(ENOMEM); |
| 697 |
return NULL; |
| 698 |
} |
| 699 |
|
| 700 |
Py_INCREF(result); |
| 701 |
mnt_ref_table(tab); |
| 702 |
|
| 703 |
DBG(TAB, pymnt_debug_h(tab, "result py-obj %p new, py-refcnt=%d", |
| 704 |
result, (int) Py_REFCNT((PyObject *) result))); |
| 705 |
result->tab = tab; |
| 706 |
result->iter = mnt_new_iter(MNT_ITER_FORWARD); |
| 707 |
mnt_table_set_userdata(result->tab, result); |
| 708 |
result->errcb = NULL; |
| 709 |
return (PyObject *) result; |
| 710 |
} |
| 711 |
|
| 712 |
static PyGetSetDef Table_getseters[] = { |
| 713 |
{"nents", (getter)Table_get_nents, NULL, "number of valid entries in tab", NULL}, |
| 714 |
{"intro_comment", (getter)Table_get_intro_comment, (setter)Table_set_intro_comment, "fstab intro comment", NULL}, |
| 715 |
{"trailing_comment", (getter)Table_get_trailing_comment, (setter)Table_set_trailing_comment, "fstab trailing comment", NULL}, |
| 716 |
{"errcb", NULL, (setter)Table_set_parser_errcb, "parser error callback", NULL}, |
| 717 |
{NULL} |
| 718 |
}; |
| 719 |
|
| 720 |
|
| 721 |
static PyObject *Table_repr(TableObject *self) |
| 722 |
{ |
| 723 |
return PyUnicode_FromFormat( |
| 724 |
"<libmount.Table object at %p, entries=%d, comments_enabled=%s, errcb=%s>", |
| 725 |
self, |
| 726 |
mnt_table_get_nents(self->tab), |
| 727 |
mnt_table_with_comments(self->tab) ? "True" : "False", |
| 728 |
self->errcb ? pystos(PyObject_Repr(self->errcb)) : "None"); |
| 729 |
} |
| 730 |
|
| 731 |
PyTypeObject TableType = { |
| 732 |
PyVarObject_HEAD_INIT(NULL, 0) |
| 733 |
.tp_name = "libmount.Table", |
| 734 |
.tp_basicsize = sizeof(TableObject), |
| 735 |
.tp_dealloc = (destructor)Table_destructor, |
| 736 |
.tp_repr = (reprfunc) Table_repr, |
| 737 |
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, |
| 738 |
.tp_doc = Table_HELP, |
| 739 |
.tp_methods = Table_methods, |
| 740 |
.tp_members = Table_members, |
| 741 |
.tp_getset = Table_getseters, |
| 742 |
.tp_init = (initproc)Table_init, |
| 743 |
.tp_new = Table_new, |
| 744 |
}; |
| 745 |
|
| 746 |
void Table_AddModuleObject(PyObject *mod) |
| 747 |
{ |
| 748 |
if (PyType_Ready(&TableType) < 0) |
| 749 |
return; |
| 750 |
|
| 751 |
DBG(TAB, pymnt_debug("add to module")); |
| 752 |
|
| 753 |
Py_INCREF(&TableType); |
| 754 |
PyModule_AddObject(mod, "Table", (PyObject *)&TableType); |
| 755 |
} |
| 756 |
|