summaryrefslogtreecommitdiffstats
path: root/tests/utils/gfid-access.py
blob: 4fd4b6dfa8de15ea045f6c00b8a1dc455b25a9fc (plain)
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
#!/usr/bin/env python2
#
# Copyright (c) 2011-2014 Red Hat, Inc. <http://www.redhat.com>
# This file is part of GlusterFS.

# This file is licensed to you under your choice of the GNU Lesser
# General Public License, version 3 or any later version (LGPLv3 or
# later), or the GNU General Public License, version 2 (GPLv2), in all
# cases as published by the Free Software Foundation.
#

import os
import sys
import stat
import time
import struct
import random
import libcxattr

from errno import EEXIST

Xattr = libcxattr.Xattr()

def umask():
    return os.umask(0)

def _fmt_mknod(l):
    return "!II%dsI%dsIII" % (37, l+1)

def _fmt_mkdir(l):
    return "!II%dsI%dsII" % (37, l+1)

def _fmt_symlink(l1, l2):
    return "!II%dsI%ds%ds" % (37, l1+1, l2+1)

def entry_pack_reg(gf, bn, mo, uid, gid):
    blen = len(bn)
    return struct.pack(_fmt_mknod(blen),
                       uid, gid, gf, mo, bn,
                       stat.S_IMODE(mo), 0, umask())

def entry_pack_dir(gf, bn, mo, uid, gid):
    blen = len(bn)
    return struct.pack(_fmt_mkdir(blen),
                       uid, gid, gf, mo, bn,
                       stat.S_IMODE(mo), umask())

def entry_pack_symlink(gf, bn, lnk, mo, uid, gid):
    blen = len(bn)
    llen = len(lnk)
    return struct.pack(_fmt_symlink(blen, llen),
                       uid, gid, gf, mo, bn, lnk)

if __name__ == '__main__':
    if len(sys.argv) < 6:
        print("USAGE: %s <mount> <pargfid|ROOT> <filename> <GFID> <file type>" % (sys.argv[0]))
        sys.exit(-1) # nothing to do
    mtpt       = sys.argv[1]
    pargfid    = sys.argv[2]
    fname      = sys.argv[3]
    randomgfid = sys.argv[4]
    ftype      = sys.argv[5]

    os.chdir(mtpt)
    if pargfid == 'ROOT':
        pargfid = '.gfid/00000000-0000-0000-0000-000000000001'
    else:
        pargfid = '.gfid/' + pargfid

    blob = None

    # entry op: use non-zero uid/gid (to catch gfid-access xlator bugs)
    if ftype == 'file':
        mode = stat.S_IFREG | 644
        blob = entry_pack_reg(randomgfid, fname, mode, 10, 10)
    elif ftype =='dir':
        mode = stat.S_IFDIR | 755
        blob = entry_pack_dir(randomgfid, fname, mode, 10, 10)
    else: # not yet...
        sys.exit(-1)

    if blob == None:
        sys.exit(-1)
    try:
        Xattr.lsetxattr(pargfid, 'glusterfs.gfid.newfile', blob)
    except OSError:
        ex = sys.exc_info()[1]
        if not ex.errno in [EEXIST]:
            raise
            sys.exit(-1)
    print "File creation OK"
    sys.exit(0)