Back to index

obnam  1.1
Defines | Functions | Variables
_obnammodule.c File Reference
#include <Python.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/xattr.h>
#include <unistd.h>
#include <stdlib.h>

Go to the source code of this file.

Defines

#define _XOPEN_SOURCE   600
#define _POSIX_C_SOURCE   200809L

Functions

static PyObject * fadvise_dontneed (PyObject *self, PyObject *args)
static PyObject * utimensat_wrapper (PyObject *self, PyObject *args)
static PyObject * lstat_wrapper (PyObject *self, PyObject *args)
static PyObject * llistxattr_wrapper (PyObject *self, PyObject *args)
static PyObject * lgetxattr_wrapper (PyObject *self, PyObject *args)
static PyObject * lsetxattr_wrapper (PyObject *self, PyObject *args)
PyMODINIT_FUNC init_obnam (void)

Variables

static PyMethodDef methods []

Define Documentation

#define _POSIX_C_SOURCE   200809L

Definition at line 40 of file _obnammodule.c.

#define _XOPEN_SOURCE   600

Definition at line 38 of file _obnammodule.c.


Function Documentation

static PyObject* fadvise_dontneed ( PyObject *  self,
PyObject *  args 
) [static]

Definition at line 52 of file _obnammodule.c.

{
#if POSIX_FADV_DONTNEED
    int fd;
    /* Can't use off_t for offset and len, since PyArg_ParseTuple
       doesn't know it. */
    unsigned long long offset;
    unsigned long long len;
    int ret;

    if (!PyArg_ParseTuple(args, "iLL", &fd, &offset, &len))
        return NULL;
    ret = posix_fadvise(fd, offset, len, POSIX_FADV_DONTNEED);
    return Py_BuildValue("i", ret);
#else
    return Py_BuildValue("i", 0);
#endif
}
PyMODINIT_FUNC init_obnam ( void  )

Definition at line 228 of file _obnammodule.c.

{
    (void) Py_InitModule("_obnam", methods);
}
static PyObject* lgetxattr_wrapper ( PyObject *  self,
PyObject *  args 
) [static]

Definition at line 163 of file _obnammodule.c.

{
    const char *filename;
    const char *attrname;
    size_t bufsize;
    PyObject *o;

    if (!PyArg_ParseTuple(args, "ss", &filename, &attrname))
        return NULL;

    bufsize = 0;
    o = NULL;
    do {
        bufsize += 1024;
        char *buf = malloc(bufsize);
        ssize_t n = lgetxattr(filename, attrname, buf, bufsize);

        if (n > 0)
            o = Py_BuildValue("s#", buf, (int) n);
        else if (n == -1 && errno != ERANGE)
            o = Py_BuildValue("i", errno);
        free(buf);
    } while (o == NULL);
    
    return o;
}
static PyObject* llistxattr_wrapper ( PyObject *  self,
PyObject *  args 
) [static]

Definition at line 135 of file _obnammodule.c.

{
    const char *filename;
    size_t bufsize;
    PyObject *o;

    if (!PyArg_ParseTuple(args, "s", &filename))
        return NULL;

    bufsize = 0;
    o = NULL;
    do {
        bufsize += 1024;
        char *buf = malloc(bufsize);
        ssize_t n = llistxattr(filename, buf, bufsize);

        if (n >= 0)
            o = Py_BuildValue("s#", buf, (int) n);
        else if (n == -1 && errno != ERANGE)
            o = Py_BuildValue("i", errno);
        free(buf);
    } while (o == NULL);
    
    return o;
}
static PyObject* lsetxattr_wrapper ( PyObject *  self,
PyObject *  args 
) [static]

Definition at line 192 of file _obnammodule.c.

{
    const char *filename;
    const char *name;
    const char *value;
    int size;
    int ret;

    if (!PyArg_ParseTuple(args, "sss#", &filename, &name, &value, &size))
        return NULL;

    ret = lsetxattr(filename, name, value, size, 0);
    if (ret == -1)
        ret = errno;
    return Py_BuildValue("i", ret);
}
static PyObject* lstat_wrapper ( PyObject *  self,
PyObject *  args 
) [static]

Definition at line 101 of file _obnammodule.c.

{
    int ret;
    const char *filename;
    struct stat st = {0};

    if (!PyArg_ParseTuple(args, "s", &filename))
        return NULL;

    ret = lstat(filename, &st);
    if (ret == -1)
        ret = errno;
    return Py_BuildValue("iLLLLLLLLLLLLLLLL", 
                         ret,
                         (long long) st.st_dev,
                         (long long) st.st_ino,
                         (long long) st.st_mode,
                         (long long) st.st_nlink,
                         (long long) st.st_uid,
                         (long long) st.st_gid,
                         (long long) st.st_rdev,
                         (long long) st.st_size,
                         (long long) st.st_blksize,
                         (long long) st.st_blocks,
                         (long long) st.st_atim.tv_sec,
                         (long long) st.st_atim.tv_nsec,
                         (long long) st.st_mtim.tv_sec,
                         (long long) st.st_mtim.tv_nsec,
                         (long long) st.st_ctim.tv_sec,
                         (long long) st.st_ctim.tv_nsec);
}
static PyObject* utimensat_wrapper ( PyObject *  self,
PyObject *  args 
) [static]

Definition at line 73 of file _obnammodule.c.

{
    int ret;
    const char *filename;
    long atime_sec, atime_nsec;
    long mtime_sec, mtime_nsec;
    struct timespec tv[2];

    if (!PyArg_ParseTuple(args, "sllll", 
                          &filename, 
                          &atime_sec,
                          &atime_nsec,
                          &mtime_sec,
                          &mtime_nsec))
        return NULL;

    tv[0].tv_sec = atime_sec;
    tv[0].tv_nsec = atime_nsec;
    tv[1].tv_sec = mtime_sec;
    tv[1].tv_nsec = mtime_nsec;
    ret = utimensat(AT_FDCWD, filename, tv, AT_SYMLINK_NOFOLLOW);
    if (ret == -1)
        ret = errno;
    return Py_BuildValue("i", ret);
}

Variable Documentation

PyMethodDef methods[] [static]
Initial value:
 {
    {"fadvise_dontneed",  fadvise_dontneed, METH_VARARGS, 
     "Call posix_fadvise(2) with POSIX_FADV_DONTNEED argument."},
    {"utimensat", utimensat_wrapper, METH_VARARGS,
     "utimensat(2) wrapper."},
    {"lstat", lstat_wrapper, METH_VARARGS,
     "lstat(2) wrapper; arg is filename, returns tuple."},
    {"llistxattr", llistxattr_wrapper, METH_VARARGS,
     "llistxattr(2) wrapper; arg is filename, returns tuple."},
    {"lgetxattr", lgetxattr_wrapper, METH_VARARGS,
     "lgetxattr(2) wrapper; arg is filename, returns tuple."},
    {"lsetxattr", lsetxattr_wrapper, METH_VARARGS,
     "lsetxattr(2) wrapper; arg is filename, returns errno."},
    {NULL, NULL, 0, NULL}        
}

Definition at line 210 of file _obnammodule.c.