File: file.c

package info (click to toggle)
pysmbc 1.0.15.3-0.1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 260 kB
  • ctags: 248
  • sloc: ansic: 2,031; python: 741; makefile: 24
file content (428 lines) | stat: -rw-r--r-- 11,708 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
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
/* -*- Mode: C; c-file-style: "gnu" -*-
 * pysmbc - Python bindings for libsmbclient
 * Copyright (C) 2002, 2005, 2006, 2007, 2008, 2010, 2011, 2012  Red Hat, Inc
 * Copyright (C) 2010  Open Source Solution Technology Corporation
 * Copyright (C) 2010  Patrick Geltinger <patlkli@patlkli.org>
 * Authors:
 *  Tim Waugh <twaugh@redhat.com>
 *  Tsukasa Hamano <hamano@osstech.co.jp>
 *  Patrick Geltinger <patlkli@patlkli.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <Python.h>
#include "smbcmodule.h"
#include "context.h"
#include "file.h"

//////////
// File //
//////////

/*
  The size of off_t is potentionally unknown, so try to use the
  biggest integer type possible when coverting between ptyhon
  values and off_t.

  In order to communicate offset values between python objects and C
  types use off_t_long as the C type and OFF_T_FORMAT as the format
  character for Py_BuildValue, PyArg_Parse, and similar functions.
*/
#ifdef HAVE_LONG_LONG
typedef PY_LONG_LONG off_t_long;
#define OFF_T_FORMAT "L"
#else
typedef PY_LONG off_t_long;
#define OFF_T_FORMAT "l"
#endif

static PyObject *
File_new (PyTypeObject *type, PyObject *args, PyObject *kwds)
{
  File *self;
  self = (File *) type->tp_alloc (type, 0);
  if (self != NULL)
    self->file = NULL;

  return (PyObject *) self;
}

static int
File_init (File *self, PyObject *args, PyObject *kwds)
{
  PyObject *ctxobj;
  Context *ctx;
  char *uri = NULL;
  int flags = 0;
  int mode = 0;
  smbc_open_fn fn;
  SMBCFILE *file;
  static char *kwlist[] = 
    {
      "context",
      "uri",
      "flags",
      "mode",
      NULL
    };

  if (!PyArg_ParseTupleAndKeywords (args, kwds, "O|sii", kwlist, &ctxobj,
				    &uri, &flags, &mode))
    return -1;

  debugprintf ("-> File_init (%p, \"%s\")\n", ctxobj, uri);
  if (!PyObject_TypeCheck (ctxobj, &smbc_ContextType))
    {
      PyErr_SetString (PyExc_TypeError, "Expected smbc.Context");
      debugprintf ("<- File_init() EXCEPTION\n");
      return -1;
    }

  Py_INCREF (ctxobj);
  ctx = (Context *) ctxobj;
  self->context = ctx;
  if (uri)
    {
      fn = smbc_getFunctionOpen (ctx->context);
      file = (*fn) (ctx->context, uri, (int) flags, (mode_t) mode);
      if (file == NULL)
	{
	  pysmbc_SetFromErrno();
          Py_DECREF (ctxobj);
	  return -1;
	}

      self->file = file;
    }

  debugprintf ("%p open()\n", self->file);
  debugprintf ("%p <- File_init() = 0\n", self->file);
  return 0;
}

static void
File_dealloc (File *self)
{
  Context *ctx = self->context;
  smbc_close_fn fn;
  if (self->file)
    {
      debugprintf ("%p close()\n", self->file);
      fn = smbc_getFunctionClose (ctx->context);
      (*fn) (ctx->context, self->file);
    }

  if (self->context)
    Py_DECREF ((PyObject *) self->context);

  Py_TYPE (self)->tp_free ((PyObject *) self);
}

static PyObject *
File_read (File *self, PyObject *args)
{
  Context *ctx = self->context;
  size_t size = 0;
  smbc_read_fn fn;
  char *buf;
  ssize_t len;
  PyObject *ret;
  smbc_fstat_fn fn_fstat;
  struct stat st;

  if (!PyArg_ParseTuple (args, "|k", &size))
	return NULL;

  fn = smbc_getFunctionRead (ctx->context);

  if (size == 0)
    {
      fn_fstat = smbc_getFunctionFstat (ctx->context);
      (*fn_fstat) (ctx->context, self->file, &st);
      size = st.st_size;
    }

  buf = (char *)malloc (size);
  if (!buf)
    return PyErr_NoMemory ();

  len = (*fn) (ctx->context, self->file, buf, size);
  if (len < 0)
    {
      pysmbc_SetFromErrno ();
      free (buf);
      return NULL;
    }

  ret = PyBytes_FromStringAndSize (buf, len);
  free (buf);
  return ret;
}

static PyObject *
File_write (File *self, PyObject *args)
{
  Context *ctx = self->context;
  int size = 0;
  smbc_write_fn fn;
  char *buf;
  ssize_t len;

  if (!PyArg_ParseTuple (args, "s#", &buf, &size))
    return NULL;

  fn = smbc_getFunctionWrite (ctx->context);
  len = (*fn) (ctx->context, self->file, buf, size);
  if (len < 0)
    {
      pysmbc_SetFromErrno ();
      return NULL;
    }

  return PyLong_FromLong (len);
}

static PyObject *
File_fstat (File *self, PyObject *args)
{
  Context *ctx = self->context;
  smbc_fstat_fn fn;
  struct stat st;
  int ret;

  fn = smbc_getFunctionFstat (ctx->context);
  errno = 0;
  ret = (*fn) (ctx->context, self->file, &st);
  if (ret < 0)
    {
      pysmbc_SetFromErrno ();
      return NULL;
    }

  return Py_BuildValue ("(IKKKIIKIII)",
			st.st_mode,
			(unsigned long long)st.st_ino,
			(unsigned long long)st.st_dev,
			(unsigned long long)st.st_nlink,
			st.st_uid,
			st.st_gid,
			st.st_size,
			st.st_atime,
			st.st_mtime,
			st.st_ctime);
}

static PyObject *
File_close (File *self, PyObject *args)
{
  Context *ctx = self->context;
  smbc_close_fn fn;
  int ret = 0;

  fn = smbc_getFunctionClose (ctx->context);
  if (self->file)
    {
      ret = (*fn) (ctx->context, self->file);
      self->file = NULL;
    }

  return PyLong_FromLong (ret);
}

static PyObject *
File_iter (PyObject *self)
{
  Py_INCREF (self);
  return self;
}

static PyObject *
File_iternext (PyObject *self)
{
  File *file = (File *) self;
  Context *ctx = file->context;
  smbc_read_fn fn;
  char buf[2048];
  ssize_t len;
  fn = smbc_getFunctionRead (ctx->context);
  len = (*fn) (ctx->context, file->file, buf, 2048);
  if (len > 0)
    return PyBytes_FromStringAndSize (buf, len);
  else if (len == 0)
    PyErr_SetNone (PyExc_StopIteration);
  else
    pysmbc_SetFromErrno ();

  return NULL;
}

static PyObject *
File_lseek (File *self, PyObject *args)
{
  Context *ctx = self->context;
  smbc_lseek_fn fn;
  off_t_long py_offset;
  off_t offset;
  int whence=0;
  off_t ret;

  if (!PyArg_ParseTuple (args, (OFF_T_FORMAT "|i"), &py_offset, &whence))
    return NULL;

  offset = py_offset;

  /* check for data loss from cast */
  if ((off_t_long)offset != py_offset)
    PyErr_SetString (PyExc_OverflowError, "Data loss in casting off_t");

  fn = smbc_getFunctionLseek (ctx->context);
  ret = (*fn) (ctx->context, self->file, offset, whence);
  if (ret < 0)
    {
      pysmbc_SetFromErrno ();
      return NULL;
    }

  return Py_BuildValue (OFF_T_FORMAT, ret);
}

PyMethodDef File_methods[] =
  {
	{"read", (PyCFunction)File_read, METH_VARARGS,
	 "read(size) -> string\n\n"
	 "@type size: int\n"
	 "@param size: size of reading\n"
	 "@return: read data"
	},
	{"write", (PyCFunction)File_write, METH_VARARGS,
	 "write(buf) -> int\n\n"
	 "@type buf: string\n"
	 "@param buf: write data\n"
	 "@return: size of written"
	 },
	{"fstat", (PyCFunction)File_fstat, METH_NOARGS,
	 "fstat() -> tuple\n\n"
	 "@return: fstat information"
	},
	{"close", (PyCFunction)File_close, METH_NOARGS,
	 "close() -> int\n\n"
	 "@return: on success, < 0 on error"
	},
	{"lseek", (PyCFunction)File_lseek, METH_VARARGS,
	 "lseek(offset, whence=0)\n\n"
	 "@return: on success, current offset location, othwerwise -1"
	},
	{"seek", (PyCFunction)File_lseek, METH_VARARGS,
	 "seek(offset, whence=0)\n\n"
	 "@return: on success, current offset location, othwerwise -1"
	},
    { NULL } /* Sentinel */
  };

#if PY_MAJOR_VERSION >= 3
  PyTypeObject smbc_FileType =
    {
      PyVarObject_HEAD_INIT(NULL, 0)
      "smbc.File",               /*tp_name*/
      sizeof(File),              /*tp_basicsize*/
      0,                         /*tp_itemsize*/
      (destructor)File_dealloc,  /*tp_dealloc*/
      0,                         /*tp_print*/
      0,                         /*tp_getattr*/
      0,                         /*tp_setattr*/
      0,                         /*tp_reserved*/
      0,                         /*tp_repr*/
      0,                         /*tp_as_number*/
      0,                         /*tp_as_sequence*/
      0,                         /*tp_as_mapping*/
      0,                         /*tp_hash */
      0,                         /*tp_call*/
      0,                         /*tp_str*/
      0,                         /*tp_getattro*/
      0,                         /*tp_setattro*/
      0,                         /*tp_as_buffer*/
      Py_TPFLAGS_DEFAULT,        /*tp_flags*/
      "SMBC File\n"
      "=========\n\n"
  
      "  A file object."
      "",                        /* tp_doc */
      0,                         /* tp_traverse */
      0,                         /* tp_clear */
      0,                         /* tp_richcompare */
      0,                         /* tp_weaklistoffset */
      File_iter,                 /* tp_iter */
      File_iternext,             /* tp_iternext */
      File_methods,              /* tp_methods */
      0,                         /* tp_members */
      0,                         /* tp_getset */
      0,                         /* tp_base */
      0,                         /* tp_dict */
      0,                         /* tp_descr_get */
      0,                         /* tp_descr_set */
      0,                         /* tp_dictoffset */
      (initproc)File_init,       /* tp_init */
      0,                         /* tp_alloc */
      File_new,                  /* tp_new */
    };
#else
  PyTypeObject smbc_FileType =
    {
      PyObject_HEAD_INIT(NULL)
      0,                         /*ob_size*/
      "smbc.File",               /*tp_name*/
      sizeof(File),              /*tp_basicsize*/
      0,                         /*tp_itemsize*/
      (destructor)File_dealloc,  /*tp_dealloc*/
      0,                         /*tp_print*/
      0,                         /*tp_getattr*/
      0,                         /*tp_setattr*/
      0,                         /*tp_compare*/
      0,                         /*tp_repr*/
      0,                         /*tp_as_number*/
      0,                         /*tp_as_sequence*/
      0,                         /*tp_as_mapping*/
      0,                         /*tp_hash */
      0,                         /*tp_call*/
      0,                         /*tp_str*/
      0,                         /*tp_getattro*/
      0,                         /*tp_setattro*/
      0,                         /*tp_as_buffer*/
      Py_TPFLAGS_DEFAULT,        /*tp_flags*/
      "SMBC File\n"
      "=========\n\n"
  
      "  A file object."
      "",                        /* tp_doc */
      0,                         /* tp_traverse */
      0,                         /* tp_clear */
      0,                         /* tp_richcompare */
      0,                         /* tp_weaklistoffset */
      File_iter,                 /* tp_iter */
      File_iternext,             /* tp_iternext */
      File_methods,              /* tp_methods */
      0,                         /* tp_members */
      0,                         /* tp_getset */
      0,                         /* tp_base */
      0,                         /* tp_dict */
      0,                         /* tp_descr_get */
      0,                         /* tp_descr_set */
      0,                         /* tp_dictoffset */
      (initproc)File_init,       /* tp_init */
      0,                         /* tp_alloc */
      File_new,                  /* tp_new */
    };
#endif