Created
April 7, 2015 14:07
-
-
Save T0aD/762f53074948e84fb96d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/python | |
from ctypes import * | |
libc = CDLL('libc.so.6') | |
quotactl = libc.quotactl | |
""" | |
struct dqblk | |
{ | |
u_int64_t dqb_bhardlimit; /* absolute limit on disk quota blocks alloc */ | |
u_int64_t dqb_bsoftlimit; /* preferred limit on disk quota blocks */ | |
u_int64_t dqb_curspace; /* current quota block count */ | |
u_int64_t dqb_ihardlimit; /* maximum # allocated inodes */ | |
u_int64_t dqb_isoftlimit; /* preferred inode limit */ | |
u_int64_t dqb_curinodes; /* current # allocated inodes */ | |
u_int64_t dqb_btime; /* time limit for excessive disk use */ | |
u_int64_t dqb_itime; /* time limit for excessive files */ | |
u_int32_t dqb_valid; /* bitmask of QIF_* constants */ | |
}; | |
""" | |
class dqblk(Structure): | |
_fields_ = [("bhardlimit", c_ulonglong), | |
("bsoftlimit", c_ulonglong), | |
("curspace", c_ulonglong), | |
("ihardlimit", c_ulonglong), | |
("isoftlimit", c_ulonglong), | |
("curinodes", c_ulonglong), | |
("btime", c_ulonglong), | |
("itime", c_ulonglong), | |
("valid", c_uint)] | |
USRQUOTA = 0 | |
GRPQUOTA = 1 | |
Q_GETQUOTA = 0x800007 | |
Q_SETQUOTA = 0x800008 | |
def QCMD(cmd, type): | |
return (cmd << 8) | (type & 0x00ff) | |
class Quota: | |
def __init__(self, dev = "/dev/md0"): | |
self.dev = dev | |
def get(self, uid): | |
blk = dqblk() | |
quotactl(QCMD(Q_GETQUOTA, USRQUOTA), self.dev, uid, byref(blk)) | |
return blk | |
def set(self, uid, blk): | |
quotactl(QCMD(Q_SETQUOTA, USRQUOTA), self.dev, uid, byref(blk)) | |
if __name__ == "__main__": | |
blk = dqblk() | |
dev = c_char_p("/dev/sda4") | |
uid = 5501 | |
quotactl(QCMD(Q_GETQUOTA, USRQUOTA), dev, uid, byref(blk)) | |
for f, t in dqblk._fields_: | |
print f, '=', blk.__getattribute__(f) | |
# New way | |
quota = Quota() | |
blk = quota.get(5512) | |
for f, t in dqblk._fields_: | |
print f, '=', blk.__getattribute__(f) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How does the set work if it is a tuple. Can you put an example for set command also?