File: handle.c

package info (click to toggle)
libnbd 1.22.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 10,636 kB
  • sloc: ansic: 53,855; ml: 12,311; sh: 8,499; python: 4,595; makefile: 2,902; perl: 165; cpp: 24
file content (204 lines) | stat: -rw-r--r-- 5,311 bytes parent folder | download | duplicates (2)
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
/* NBD client library in userspace
 * Copyright Red Hat
 *
 * This library 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 of the License, or (at your option) any later version.
 *
 * This library 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 this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/* These are the hand-written Python module bindings.  The majority of
 * bindings are generated, see python/methods.c
 */

#include <config.h>

#define PY_SSIZE_T_CLEAN 1
#include <Python.h>

#if PY_MAJOR_VERSION == 2
#error \
  These bindings will not work with Python 2.  \
  Recompile using Python 3 or use ./configure --disable-python.
#endif

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#include <libnbd.h>

#include "iszero.h"
#include "version.h"

#include "methods.h"

static inline PyObject *
put_handle (struct nbd_handle *h)
{
  assert (h);
  return PyCapsule_New (h, "nbd_handle", NULL);
}

PyObject *
nbd_internal_py_create (PyObject *self, PyObject *args)
{
  struct nbd_handle *h;

  if (!PyArg_ParseTuple (args, ":nbd_create"))
    return NULL;
  h = nbd_create ();
  if (h == NULL) {
    PyErr_SetString (PyExc_RuntimeError, nbd_get_error ());
    return NULL;
  }

  return put_handle (h);
}

PyObject *
nbd_internal_py_close (PyObject *self, PyObject *args)
{
  PyObject *py_h;
  struct nbd_handle *h;

  if (!PyArg_ParseTuple (args, "O:nbd_close", &py_h))
    return NULL;
  h = get_handle (py_h);

  nbd_close (h);

  Py_INCREF (Py_None);
  return Py_None;
}

/* A wrapper around common/utils/version.c:display_version, used when
 * you do "nbdsh --version".
 */
PyObject *
nbd_internal_py_display_version (PyObject *self, PyObject *args)
{
  const char *program_name;

  if (!PyArg_ParseTuple (args, "s:display_version", &program_name))
    return NULL;

  display_version (program_name);

  Py_INCREF (Py_None);
  return Py_None;
}

/* Return new reference to MemoryView wrapping buffer or aio_buffer contents.
 * buffertype is PyBUF_READ or PyBUF_WRITE, for how the buffer will be used
 * (remember, aio_pwrite READS the buffer, aio_pread WRITES into the buffer).
 */
PyObject *
nbd_internal_py_get_aio_view (PyObject *object, int buffertype)
{
  PyObject *buffer = NULL;

  if (PyObject_CheckBuffer (object))
    buffer = object;
  else if (PyObject_IsInstance (object,
                                nbd_internal_py_get_nbd_buffer_type ())) {
    buffer = PyObject_GetAttrString (object, "_o");

    if (buffertype == PyBUF_READ &&
        ! PyObject_HasAttrString (object, "_init")) {
      assert (PyByteArray_Check (buffer));
      memset (PyByteArray_AS_STRING (buffer), 0,
              PyByteArray_GET_SIZE (buffer));
      if (PyObject_SetAttrString (object, "_init", Py_True) < 0)
        return NULL;
    }
  }

  if (buffer)
    return PyMemoryView_GetContiguous (buffer, buffertype, 'A');

  PyErr_SetString (PyExc_TypeError,
                   "aio_buffer: expecting buffer or nbd.Buffer instance");
  return NULL;
}

int
nbd_internal_py_init_aio_buffer (PyObject *object)
{
  if (PyObject_IsInstance (object, nbd_internal_py_get_nbd_buffer_type ()))
    return PyObject_SetAttrString (object, "_init", Py_True);
  return 0;
}

/* Allocate an uninitialized persistent buffer used for nbd_aio_pread. */
PyObject *
nbd_internal_py_alloc_aio_buffer (PyObject *self, PyObject *args)
{
  Py_ssize_t len;

  if (!PyArg_ParseTuple (args, "n:nbd_internal_py_alloc_aio_buffer",
                         &len))
    return NULL;

  /* Constructing bytearray(len) in python zeroes the memory; doing it this
   * way gives uninitialized memory.  This correctly flags negative len.
   */
  return PyByteArray_FromStringAndSize (NULL, len);
}

PyObject *
nbd_internal_py_aio_buffer_is_zero (PyObject *self, PyObject *args)
{
  Py_buffer buf;
  Py_ssize_t offset, size;
  int init;
  PyObject *ret = NULL;

  if (!PyArg_ParseTuple (args,
                         "y*nnp:nbd_internal_py_aio_buffer_is_zero",
                         &buf, &offset, &size, &init))
    return NULL;

  if (size == 0) {
    ret = Py_True;
    goto out;
  }

  /* Check the bounds of the offset. */
  if (offset < 0 || offset > buf.len) {
    PyErr_SetString (PyExc_IndexError, "offset out of range");
    goto out;
  }

  /* Compute or check the length. */
  if (size == -1)
    size = buf.len - offset;
  else if (size < 0) {
    PyErr_SetString (PyExc_IndexError,
                     "size cannot be negative, "
                     "except -1 to mean to the end of the buffer");
    goto out;
  }
  else if ((size_t)offset + size > buf.len) {
    PyErr_SetString (PyExc_IndexError, "size out of range");
    goto out;
  }

  if (!init || is_zero (buf.buf + offset, size))
    ret = Py_True;
  else
    ret = Py_False;
 out:
  PyBuffer_Release (&buf);
  Py_XINCREF (ret);
  return ret;
}