2 # Unix SMB/CIFS implementation.
4 # Copyright (C) Noel Power 2017
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
25 USER_DEFAULT_QUOTAS
= 2
27 GROUP_DEFAULT_QUOTAS
= 4
34 self
.quotatype
= USER_DEFAULT_QUOTAS
44 def quota_to_str(item
):
45 result
= str(item
.flags
) + " " + str(item
.usedblocks
) + " " + str(item
.softlimit
) + " " + str(item
.hardlimit
) + " " + str(item
.usedinodes
) + " " + str(item
.slimitinodes
) + " " + str(item
.hlimitinodes
)
48 def quota_to_db_str(item
):
49 result
= item
.uid
+ " " + str(item
.usedblocks
) + " " + str(item
.softlimit
) + " " + str(item
.hardlimit
) + " " + str(item
.usedinodes
) + " " + str(item
.slimitinodes
) + " " + str(item
.hlimitinodes
)
52 def load_quotas(input_file
):
53 fileContents
= open(input_file
,"r")
56 for line
in fileContents
:
57 if line
.strip().startswith("#"):
59 content
= line
.strip().split()
62 logging
.debug("ignoring line %d, doesn't have enough fields\n"%lineno
)
65 quota
.uid
= content
[0]
66 quota
.usedblocks
= content
[1]
67 quota
.softlimit
= content
[2]
68 quota
.hardlimit
= content
[3]
69 quota
.usedinodes
= content
[4]
70 quota
.slimitinodes
= content
[5]
71 quota
.hlimitinodes
= content
[6]
77 def set_quotas(quota_list
, output_file
):
78 filecontents
= open(output_file
,"w+")
79 if filecontents
== None:
82 for quota
in quota_list
:
83 next_line
= quota_to_db_str(quota
)
85 lines
= lines
+ next_line
+ "\n"
86 filecontents
.write(lines
)
90 def get_quotas(uid
, quota_list
):
91 logging
.debug("in get_quotas\n")
92 for quota
in quota_list
:
98 logging
.basicConfig(format
='%(asctime)s %(message)s', level
=logging
.DEBUG
)
99 logging
.debug("system args passed are %s\n"% str(sys
.argv
))
100 quota_file_dir
= os
.path
.dirname(sys
.argv
[0])
101 quota_file_db
= os
.path
.join(quota_file_dir
,"quotas.db")
102 logging
.debug("quota db is located %s\n", quota_file_db
)
103 quota_list
= load_quotas(quota_file_db
)
104 logging
.debug("quotas loaded have %s entries\n", len(quota_list
))
106 if len(sys
.argv
) == 4:
108 directory
= sys
.argv
[1]
109 if sys
.argv
[2] == "1":
110 query_type
= USER_QUOTAS
111 elif sys
.argv
[2] == "2":
112 query_type
= USER_DEFAULT_QUOTAS
113 elif sys
.argv
[2] == "3":
114 query_type
= GROUP_QUOTAS
115 elif sys
.argv
[2] == "4":
116 query_type
= GROUP_DEFAULT_QUOTAS
118 quota
= get_quotas(uid
, quota_list
)
120 logging
.debug("no result for uid %s"%uid)
122 result
= quota_to_str(quota
)
123 logging
.debug("got result for uid %s\n"%uid)
125 result
= "0 0 0 0 0 0 0"
126 logging
.debug("for uid %s returning quotas %s\n"%(uid
,result
))
128 elif len(sys
.argv
) > 8:
131 directory
= sys
.argv
[1]
132 quota
.query_type
= sys
.argv
[2]
133 quota
.uid
= sys
.argv
[3]
134 quota
.flags
= sys
.argv
[4]
135 quota
.softlimit
= sys
.argv
[5]
136 quota
.hardlimit
= sys
.argv
[6]
137 quota
.slimitinodes
= sys
.argv
[7]
138 quota
.hlimitinodes
= sys
.argv
[8]
139 found
= get_quotas(quota
.uid
, quota_list
)
141 found
.query_type
= quota
.query_type
142 found
.uid
= quota
.uid
143 found
.flags
= quota
.flags
144 found
.softlimit
= quota
.softlimit
145 found
.hardlimit
= quota
.hardlimit
146 found
.slimitinodes
= quota
.slimitinodes
147 found
.hlimitinodes
= quota
.hlimitinodes
149 quota_list
.append(quota
)
150 if set_quotas(quota_list
,quota_file_db
):
151 print ("%s\n"%quota_to_str(quota_list
[-1]))
153 if __name__
== '__main__':