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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
|
/*
* Copyright (C) 2018 Facebook
*
* This file is part of libbtrfsutil.
*
* libbtrfsutil 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.
*
* libbtrfsutil 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 libbtrfsutil. If not, see <http://www.gnu.org/licenses/>.
*/
#include "btrfsutilpy.h"
PyObject *is_subvolume(PyObject *self, PyObject *args, PyObject *kwds)
{
static char *keywords[] = {"path", NULL};
struct path_arg path = {.allow_fd = true};
enum btrfs_util_error err;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&:is_subvolume",
keywords, &path_converter, &path))
return NULL;
if (path.path)
err = btrfs_util_is_subvolume(path.path);
else
err = btrfs_util_is_subvolume_fd(path.fd);
if (err == BTRFS_UTIL_OK) {
path_cleanup(&path);
Py_RETURN_TRUE;
} else if (err == BTRFS_UTIL_ERROR_NOT_BTRFS ||
err == BTRFS_UTIL_ERROR_NOT_SUBVOLUME) {
path_cleanup(&path);
Py_RETURN_FALSE;
} else {
SetFromBtrfsUtilErrorWithPath(err, &path);
path_cleanup(&path);
return NULL;
}
}
PyObject *subvolume_id(PyObject *self, PyObject *args, PyObject *kwds)
{
static char *keywords[] = {"path", NULL};
struct path_arg path = {.allow_fd = true};
enum btrfs_util_error err;
uint64_t id;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&:subvolume_id",
keywords, &path_converter, &path))
return NULL;
if (path.path)
err = btrfs_util_subvolume_id(path.path, &id);
else
err = btrfs_util_subvolume_id_fd(path.fd, &id);
if (err) {
SetFromBtrfsUtilErrorWithPath(err, &path);
path_cleanup(&path);
return NULL;
}
path_cleanup(&path);
return PyLong_FromUnsignedLongLong(id);
}
PyObject *subvolume_path(PyObject *self, PyObject *args, PyObject *kwds)
{
static char *keywords[] = {"path", "id", NULL};
struct path_arg path = {.allow_fd = true};
enum btrfs_util_error err;
uint64_t id = 0;
char *subvol_path;
PyObject *ret;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&|K:subvolume_path",
keywords, &path_converter, &path, &id))
return NULL;
if (path.path)
err = btrfs_util_subvolume_path(path.path, id, &subvol_path);
else
err = btrfs_util_subvolume_path_fd(path.fd, id, &subvol_path);
if (err) {
SetFromBtrfsUtilErrorWithPath(err, &path);
path_cleanup(&path);
return NULL;
}
path_cleanup(&path);
ret = PyUnicode_DecodeFSDefault(subvol_path);
free(subvol_path);
return ret;
}
static PyObject *subvolume_info_to_object(const struct btrfs_util_subvolume_info *subvol)
{
PyObject *ret, *tmp;
ret = PyStructSequence_New(&SubvolumeInfo_type);
if (ret == NULL)
return NULL;
#define SET_UINT64(i, field) \
tmp = PyLong_FromUnsignedLongLong(subvol->field); \
if (tmp == NULL) { \
Py_DECREF(ret); \
return ret; \
} \
PyStructSequence_SET_ITEM(ret, i, tmp);
#define SET_UUID(i, field) \
tmp = PyBytes_FromStringAndSize((char *)subvol->field, 16); \
if (tmp == NULL) { \
Py_DECREF(ret); \
return ret; \
} \
PyStructSequence_SET_ITEM(ret, i, tmp);
#define SET_TIME(i, field) \
tmp = PyFloat_FromDouble(subvol->field.tv_sec + \
subvol->field.tv_nsec / 1000000000); \
if (tmp == NULL) { \
Py_DECREF(ret); \
return ret; \
} \
PyStructSequence_SET_ITEM(ret, i, tmp);
SET_UINT64(0, id);
SET_UINT64(1, parent_id);
SET_UINT64(2, dir_id);
SET_UINT64(3, flags);
SET_UUID(4, uuid);
SET_UUID(5, parent_uuid);
SET_UUID(6, received_uuid);
SET_UINT64(7, generation);
SET_UINT64(8, ctransid);
SET_UINT64(9, otransid);
SET_UINT64(10, stransid);
SET_UINT64(11, rtransid);
SET_TIME(12, ctime);
SET_TIME(13, otime);
SET_TIME(14, stime);
SET_TIME(15, rtime);
#undef SET_TIME
#undef SET_UUID
#undef SET_UINT64
return ret;
}
PyObject *subvolume_info(PyObject *self, PyObject *args, PyObject *kwds)
{
static char *keywords[] = {"path", "id", NULL};
struct path_arg path = {.allow_fd = true};
struct btrfs_util_subvolume_info subvol;
enum btrfs_util_error err;
uint64_t id = 0;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&|K:subvolume_info",
keywords, &path_converter, &path, &id))
return NULL;
if (path.path)
err = btrfs_util_subvolume_info(path.path, id, &subvol);
else
err = btrfs_util_subvolume_info_fd(path.fd, id, &subvol);
if (err) {
SetFromBtrfsUtilErrorWithPath(err, &path);
path_cleanup(&path);
return NULL;
}
path_cleanup(&path);
return subvolume_info_to_object(&subvol);
}
static PyStructSequence_Field SubvolumeInfo_fields[] = {
{"id", "int ID of this subvolume"},
{"parent_id", "int ID of the subvolume containing this subvolume"},
{"dir_id", "int inode number of the directory containing this subvolume"},
{"flags", "int root item flags"},
{"uuid", "bytes UUID of this subvolume"},
{"parent_uuid", "bytes UUID of the subvolume this is a snapshot of"},
{"received_uuid", "bytes UUID of the subvolume this was received from"},
{"generation", "int transaction ID of the subvolume root"},
{"ctransid", "int transaction ID when an inode was last changed"},
{"otransid", "int transaction ID when this subvolume was created"},
{"stransid", "int transaction ID of the sent subvolume this subvolume was received from"},
{"rtransid", "int transaction ID when this subvolume was received"},
{"ctime", "float time when an inode was last changed"},
{"otime", "float time when this subvolume was created"},
{"stime", "float time, usually zero"},
{"rtime", "float time when this subvolume was received"},
{},
};
PyStructSequence_Desc SubvolumeInfo_desc = {
"btrfsutil.SubvolumeInfo",
"Information about a Btrfs subvolume.",
SubvolumeInfo_fields,
14,
};
PyTypeObject SubvolumeInfo_type;
PyObject *get_subvolume_read_only(PyObject *self, PyObject *args, PyObject *kwds)
{
static char *keywords[] = {"path", NULL};
struct path_arg path = {.allow_fd = true};
enum btrfs_util_error err;
bool read_only;
if (!PyArg_ParseTupleAndKeywords(args, kwds,
"O&:get_subvolume_read_only",
keywords, &path_converter, &path))
return NULL;
if (path.path) {
err = btrfs_util_get_subvolume_read_only(path.path, &read_only);
} else {
err = btrfs_util_get_subvolume_read_only_fd(path.fd,
&read_only);
}
if (err) {
SetFromBtrfsUtilErrorWithPath(err, &path);
path_cleanup(&path);
return NULL;
}
path_cleanup(&path);
return PyBool_FromLong(read_only);
}
PyObject *set_subvolume_read_only(PyObject *self, PyObject *args, PyObject *kwds)
{
static char *keywords[] = {"path", "read_only", NULL};
struct path_arg path = {.allow_fd = true};
enum btrfs_util_error err;
int read_only = 1;
if (!PyArg_ParseTupleAndKeywords(args, kwds,
"O&|p:set_subvolume_read_only",
keywords, &path_converter, &path,
&read_only))
return NULL;
if (path.path)
err = btrfs_util_set_subvolume_read_only(path.path, read_only);
else
err = btrfs_util_set_subvolume_read_only_fd(path.fd, read_only);
if (err) {
SetFromBtrfsUtilErrorWithPath(err, &path);
path_cleanup(&path);
return NULL;
}
path_cleanup(&path);
Py_RETURN_NONE;
}
PyObject *get_default_subvolume(PyObject *self, PyObject *args, PyObject *kwds)
{
static char *keywords[] = {"path", NULL};
struct path_arg path = {.allow_fd = true};
enum btrfs_util_error err;
uint64_t id;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&:get_default_subvolume",
keywords, &path_converter, &path))
return NULL;
if (path.path)
err = btrfs_util_get_default_subvolume(path.path, &id);
else
err = btrfs_util_get_default_subvolume_fd(path.fd, &id);
if (err) {
SetFromBtrfsUtilErrorWithPath(err, &path);
path_cleanup(&path);
return NULL;
}
path_cleanup(&path);
return PyLong_FromUnsignedLongLong(id);
}
PyObject *set_default_subvolume(PyObject *self, PyObject *args, PyObject *kwds)
{
static char *keywords[] = {"path", "id", NULL};
struct path_arg path = {.allow_fd = true};
enum btrfs_util_error err;
uint64_t id = 0;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&|K:set_default_subvolume",
keywords, &path_converter, &path, &id))
return NULL;
if (path.path)
err = btrfs_util_set_default_subvolume(path.path, id);
else
err = btrfs_util_set_default_subvolume_fd(path.fd, id);
if (err) {
SetFromBtrfsUtilErrorWithPath(err, &path);
path_cleanup(&path);
return NULL;
}
path_cleanup(&path);
Py_RETURN_NONE;
}
PyObject *create_subvolume(PyObject *self, PyObject *args, PyObject *kwds)
{
static char *keywords[] = {"path", "async_", "qgroup_inherit", NULL};
struct path_arg path = {.allow_fd = false};
enum btrfs_util_error err;
int async = 0;
QgroupInherit *inherit = NULL;
uint64_t transid;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&|pO!:create_subvolume",
keywords, &path_converter, &path,
&async, &QgroupInherit_type, &inherit))
return NULL;
err = btrfs_util_create_subvolume(path.path, 0, async ? &transid : NULL,
inherit ? inherit->inherit : NULL);
if (err) {
SetFromBtrfsUtilErrorWithPath(err, &path);
path_cleanup(&path);
return NULL;
}
path_cleanup(&path);
if (async)
return PyLong_FromUnsignedLongLong(transid);
else
Py_RETURN_NONE;
}
PyObject *create_snapshot(PyObject *self, PyObject *args, PyObject *kwds)
{
static char *keywords[] = {
"source", "path", "recursive", "read_only", "async_",
"qgroup_inherit", NULL,
};
struct path_arg src = {.allow_fd = true}, dst = {.allow_fd = false};
enum btrfs_util_error err;
int recursive = 0, read_only = 0, async = 0;
int flags = 0;
QgroupInherit *inherit = NULL;
uint64_t transid;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&O&|pppO!:create_snapshot",
keywords, &path_converter, &src,
&path_converter, &dst, &recursive,
&read_only, &async,
&QgroupInherit_type, &inherit))
return NULL;
if (recursive)
flags |= BTRFS_UTIL_CREATE_SNAPSHOT_RECURSIVE;
if (read_only)
flags |= BTRFS_UTIL_CREATE_SNAPSHOT_READ_ONLY;
if (src.path) {
err = btrfs_util_create_snapshot(src.path, dst.path, flags,
async ? &transid : NULL,
inherit ? inherit->inherit : NULL);
} else {
err = btrfs_util_create_snapshot_fd(src.fd, dst.path, flags,
async ? &transid : NULL,
inherit ? inherit->inherit : NULL);
}
if (err) {
SetFromBtrfsUtilErrorWithPaths(err, &src, &dst);
path_cleanup(&src);
path_cleanup(&dst);
return NULL;
}
path_cleanup(&src);
path_cleanup(&dst);
if (async)
return PyLong_FromUnsignedLongLong(transid);
else
Py_RETURN_NONE;
}
PyObject *delete_subvolume(PyObject *self, PyObject *args, PyObject *kwds)
{
static char *keywords[] = {"path", "recursive", NULL};
struct path_arg path = {.allow_fd = false};
enum btrfs_util_error err;
int recursive = 0;
int flags = 0;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&|p:delete_subvolume",
keywords, &path_converter, &path,
&recursive))
return NULL;
if (recursive)
flags |= BTRFS_UTIL_DELETE_SUBVOLUME_RECURSIVE;
err = btrfs_util_delete_subvolume(path.path, flags);
if (err) {
SetFromBtrfsUtilErrorWithPath(err, &path);
path_cleanup(&path);
return NULL;
}
path_cleanup(&path);
Py_RETURN_NONE;
}
PyObject *deleted_subvolumes(PyObject *self, PyObject *args, PyObject *kwds)
{
static char *keywords[] = {"path", NULL};
struct path_arg path = {.allow_fd = true};
PyObject *ret;
uint64_t *ids;
size_t n;
enum btrfs_util_error err;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&:deleted_subvolumes",
keywords, &path_converter, &path))
return NULL;
if (path.path)
err = btrfs_util_deleted_subvolumes(path.path, &ids, &n);
else
err = btrfs_util_deleted_subvolumes_fd(path.fd, &ids, &n);
if (err) {
SetFromBtrfsUtilErrorWithPath(err, &path);
path_cleanup(&path);
return NULL;
}
path_cleanup(&path);
ret = list_from_uint64_array(ids, n);
free(ids);
return ret;
}
typedef struct {
PyObject_HEAD
struct btrfs_util_subvolume_iterator *iter;
bool info;
} SubvolumeIterator;
static void SubvolumeIterator_dealloc(SubvolumeIterator *self)
{
btrfs_util_destroy_subvolume_iterator(self->iter);
Py_TYPE(self)->tp_free((PyObject *)self);
}
static PyObject *SubvolumeIterator_next(SubvolumeIterator *self)
{
enum btrfs_util_error err;
PyObject *ret, *tmp;
char *path;
if (!self->iter) {
PyErr_SetString(PyExc_ValueError,
"operation on closed iterator");
return NULL;
}
if (self->info) {
struct btrfs_util_subvolume_info subvol;
err = btrfs_util_subvolume_iterator_next_info(self->iter, &path,
&subvol);
if (err == BTRFS_UTIL_ERROR_STOP_ITERATION) {
PyErr_SetNone(PyExc_StopIteration);
return NULL;
} else if (err) {
SetFromBtrfsUtilError(err);
return NULL;
}
tmp = subvolume_info_to_object(&subvol);
} else {
uint64_t id;
err = btrfs_util_subvolume_iterator_next(self->iter, &path, &id);
if (err == BTRFS_UTIL_ERROR_STOP_ITERATION) {
PyErr_SetNone(PyExc_StopIteration);
return NULL;
} else if (err) {
SetFromBtrfsUtilError(err);
return NULL;
}
tmp = PyLong_FromUnsignedLongLong(id);
}
if (tmp) {
ret = Py_BuildValue("O&O", PyUnicode_DecodeFSDefault, path,
tmp);
Py_DECREF(tmp);
free(path);
} else {
ret = NULL;
}
return ret;
}
static int SubvolumeIterator_init(SubvolumeIterator *self, PyObject *args,
PyObject *kwds)
{
static char *keywords[] = {"path", "top", "info", "post_order", NULL};
struct path_arg path = {.allow_fd = true};
enum btrfs_util_error err;
unsigned long long top = 0;
int info = 0;
int post_order = 0;
int flags = 0;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&|Kpp:SubvolumeIterator",
keywords, &path_converter, &path, &top,
&info, &post_order))
return -1;
if (post_order)
flags |= BTRFS_UTIL_SUBVOLUME_ITERATOR_POST_ORDER;
if (path.path) {
err = btrfs_util_create_subvolume_iterator(path.path, top,
flags, &self->iter);
} else {
err = btrfs_util_create_subvolume_iterator_fd(path.fd, top,
flags,
&self->iter);
}
if (err) {
SetFromBtrfsUtilErrorWithPath(err, &path);
path_cleanup(&path);
return -1;
}
self->info = info;
return 0;
}
static PyObject *SubvolumeIterator_close(SubvolumeIterator *self)
{
if (self->iter) {
btrfs_util_destroy_subvolume_iterator(self->iter);
self->iter = NULL;
}
Py_RETURN_NONE;
}
static PyObject *SubvolumeIterator_fileno(SubvolumeIterator *self)
{
if (!self->iter) {
PyErr_SetString(PyExc_ValueError,
"operation on closed iterator");
return NULL;
}
return PyLong_FromLong(btrfs_util_subvolume_iterator_fd(self->iter));
}
static PyObject *SubvolumeIterator_enter(SubvolumeIterator *self)
{
Py_INCREF((PyObject *)self);
return (PyObject *)self;
}
static PyObject *SubvolumeIterator_exit(SubvolumeIterator *self, PyObject *args,
PyObject *kwds)
{
static char *keywords[] = {"exc_type", "exc_value", "traceback", NULL};
PyObject *exc_type, *exc_value, *traceback;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "OOO:__exit__", keywords,
&exc_type, &exc_value, &traceback))
return NULL;
return SubvolumeIterator_close(self);
}
#define SubvolumeIterator_DOC \
"SubvolumeIterator(path, top=0, info=False, post_order=False) -> new subvolume iterator\n\n" \
"Create a new iterator that produces tuples of (path, ID) representing\n" \
"subvolumes on a filesystem.\n\n" \
"Arguments:\n" \
"path -- string, bytes, path-like object, or open file descriptor in\n" \
"filesystem to list\n" \
"top -- if not zero, instead of only listing subvolumes beneath the\n" \
"given path, list subvolumes beneath the subvolume with this ID; passing\n" \
"BTRFS_FS_TREE_OBJECTID (5) here lists all subvolumes. The subvolumes\n" \
"are listed relative to the subvolume with this ID.\n" \
"info -- bool indicating the iterator should yield SubvolumeInfo instead of\n" \
"the subvolume ID\n" \
"post_order -- bool indicating whether to yield parent subvolumes before\n" \
"child subvolumes (e.g., 'foo/bar' before 'foo')"
static PyMethodDef SubvolumeIterator_methods[] = {
{"close", (PyCFunction)SubvolumeIterator_close,
METH_NOARGS,
"close()\n\n"
"Close this iterator."},
{"fileno", (PyCFunction)SubvolumeIterator_fileno,
METH_NOARGS,
"fileno() -> int\n\n"
"Get the file descriptor associated with this iterator."},
{"__enter__", (PyCFunction)SubvolumeIterator_enter,
METH_NOARGS, ""},
{"__exit__", (PyCFunction)SubvolumeIterator_exit,
METH_VARARGS | METH_KEYWORDS, ""},
{},
};
PyTypeObject SubvolumeIterator_type = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "btrfsutil.SubvolumeIterator",
.tp_basicsize = sizeof(SubvolumeIterator),
.tp_dealloc = (destructor)SubvolumeIterator_dealloc,
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_doc = SubvolumeIterator_DOC,
.tp_iter = PyObject_SelfIter,
.tp_iternext = (iternextfunc)SubvolumeIterator_next,
.tp_methods = SubvolumeIterator_methods,
.tp_init = (initproc)SubvolumeIterator_init,
};
|