forked from mar-file-system/GUFI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gufi_ls
executable file
·228 lines (175 loc) · 9.55 KB
/
gufi_ls
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
#!/usr/bin/env python2
# This file is part of GUFI, which is part of MarFS, which is released
# under the BSD license.
# Copyright (c) 2017, Los Alamos National Security (LANS), LLC
# All rights reserved.
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation and/or
# other materials provided with the distribution.
# 3. Neither the name of the copyright holder nor the names of its contributors
# may be used to endorse or promote products derived from this software without
# specific prior written permission.
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# -----
# NOTE:
# -----
# GUFI uses the C-Thread-Pool library. The original version, written by
# Johan Hanssen Seferidis, is found at
# https://github.com/Pithikos/C-Thread-Pool/blob/master/LICENSE, and is
# released under the MIT License. LANS, LLC added functionality to the
# original work. The original work, plus LANS, LLC added functionality is
# found at https://github.com/jti-lanl/C-Thread-Pool, also under the MIT
# License. The MIT License can be found at
# https://opensource.org/licenses/MIT.
# From Los Alamos National Security, LLC:
# LA-CC-15-039
# Copyright (c) 2017, Los Alamos National Security, LLC All rights reserved.
# Copyright 2017. Los Alamos National Security, LLC. This software was produced
# under U.S. Government contract DE-AC52-06NA25396 for Los Alamos National
# Laboratory (LANL), which is operated by Los Alamos National Security, LLC for
# the U.S. Department of Energy. The U.S. Government has rights to use,
# reproduce, and distribute this software. NEITHER THE GOVERNMENT NOR LOS
# ALAMOS NATIONAL SECURITY, LLC MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR
# ASSUMES ANY LIABILITY FOR THE USE OF THIS SOFTWARE. If software is
# modified to produce derivative works, such modified software should be
# clearly marked, so as not to confuse it with the version available from
# LANL.
# THIS SOFTWARE IS PROVIDED BY LOS ALAMOS NATIONAL SECURITY, LLC AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL LOS ALAMOS NATIONAL SECURITY, LLC OR
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
# OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
# OF SUCH DAMAGE.
import argparse
import os
import subprocess
import sys
import query_builder as qb
BFQ_EXEC = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'bfq')
def build_select(args):
'''Build the SELECT portion of the query on the initial scan of entries'''
select = []
# ordering of concatenation matters
if args.inode:
select = ['inode'] + select
if args.size:
select += ['blocks']
if args.long_listing:
select += ['type', 'mode', 'nlink', 'uid']
if not args.no_group:
select += ['gid']
select += ['size', 'mtime', 'name', 'linkname']
else:
# at minimum, print the file name
select += ['name']
return select
def build_aggregate_select(args):
'''Build the SELECT portion of the query on the aggregate data'''
select = []
# ordering of concatenation matters
if args.inode:
select = ['inode'] + select
if args.size:
select += ['blocks']
if args.long_listing:
select += ['printf("%03o", mode)', 'nlink', 'uid']
if not args.no_group:
select += ['gid']
select += ['size', 'strftime("%m %d %H:%M", mtime, "unixepoch")', 'case when (type == "l") then (name || " -> " || linkname) else name end']
else:
# at minimum, print the file name
select += ['name']
return select
def build_where(args):
'''Build the WHERE portion of the query'''
where = []
if not args.all:
where += ['name REGEXP "^(?![\.]).*$"']
if args.almost_all:
where += ['name REGEXP "(?!(\.|\.\.))"']
if args.ignore_backups == True:
where += ['name REGEXP "^.*(?<![\~])$"']
return where
def build_order_by(args):
'''Build the ORDER BY portion of the query.'''
order_by = []
if args.reverse:
order_by += ['name DESC']
if args.sort_largest:
order_by += ['size DESC']
if args.mtime:
order_by += ['mtime DESC']
if len(order_by) == 0:
return ['name ASC']
if args.no_sort:
order_by = []
return order_by
if __name__=='__main__':
cwd = os.getcwd()
parser = argparse.ArgumentParser(description='GUFI version of ls')
parser.add_argument('-a', '--all', dest='all', action='store_true', help='do not ignore entries starting with .')
parser.add_argument('-A', '--amost-all', dest='almost_all', action='store_true', help='do not list implied . and ..')
parser.add_argument('-B', '--ignore-backups', dest='ignore_backups', action='store_true', help='do not list implied entries ending with ~')
parser.add_argument('-G', '--no-group', dest='no_group', action='store_true', help='in a long listing, don\'t print group names')
parser.add_argument('-i', '--inode', dest='inode', action='store_true', help='print the index number of each file')
parser.add_argument('-l', dest='long_listing', action='store_true', help='use a long listing format')
parser.add_argument('-r', '--reverse', dest='reverse', action='store_true', help='reverse order while sorting')
parser.add_argument('-R', '--recursive', dest='recursive', action='store_true', help='list subdirectories recursively')
parser.add_argument('-s', '--size', dest='size', action='store_true', help='print the allocated size of each file, in blocks')
parser.add_argument('-S', dest='sort_largest', action='store_true', help='sort by file size, largest first')
parser.add_argument('-t', dest='mtime', action='store_true', help='sort by modification time, newest first')
parser.add_argument('-U', dest='no_sort', action='store_true', help='do not sort; list entries in directory order')
# GUFI specific arguments
parser.add_argument('--delim', metavar='c', dest='delim', type=qb.get_char, help='delimiter separating output columns')
parser.add_argument('--num_results', metavar='n', dest='num_results', type=qb.get_non_negative, help='first n results')
parser.add_argument('--bfq', dest='bfq', default=BFQ_EXEC, help='Location of bfq executable')
parser.add_argument('GUFI_tree', default=[cwd], nargs='*', help='path containing GUFI tree database file')
args = parser.parse_args();
where = build_where(args)
order_by = build_order_by(args)
first_query = qb.build_query(build_select(args),
['entries'],
where,
None,
order_by,
args.num_results)
second_query = qb.build_query(build_aggregate_select(args),
['entries'],
where,
None,
order_by,
args.num_results)
rc = 0
for path in args.GUFI_tree:
# create the bfq command
bfq_cmd = [args.bfq,
'-p', '-P',
'-E', first_query,
'-J', 'SELECT * FROM entries;',
'-G', second_query,
'-z', '-1' if args.recursive else '1']
if args.delim:
bfq_cmd += ['-d', args.delim]
bfq = subprocess.Popen(bfq_cmd + [path])
bfq.communicate() # block until bfq finishes
if bfq.returncode != 0:
rc = bfq.returncode
sys.exit(rc)