| 1 |
#!/usr/bin/python3 |
| 2 |
|
| 3 |
import argparse |
| 4 |
import hashlib |
| 5 |
import os |
| 6 |
import random |
| 7 |
import shlex |
| 8 |
import shutil |
| 9 |
import stat |
| 10 |
import string |
| 11 |
import sys |
| 12 |
import errno |
| 13 |
|
| 14 |
adjectives = ["adorable", "adventurous", "aggressive", "agreeable", "alert", "alive", "amused", "angry", "annoyed", "annoying", "anxious", "arrogant", "ashamed", "attractive", "average", "awful", "bad", "beautiful", "better", "bewildered", "black", "bloody", "blue", "blue-eyed", "blushing", "bored", "brainy", "brave", "breakable", "bright", "busy", "calm", "careful", "cautious", "charming", "cheerful", "clean", "clear", "clever", "cloudy", "clumsy", "colorful", "combative", "comfortable", "concerned", "condemned", "confused", "cooperative", "courageous", "crazy", "creepy", "crowded", "cruel", "curious", "cute", "dangerous", "dark", "dead", "defeated", "defiant", "delightful", "depressed", "determined", "different", "difficult", "disgusted", "distinct", "disturbed", "dizzy", "doubtful", "drab", "dull", "eager", "easy", "elated", "elegant", "embarrassed", "enchanting", "encouraging", "energetic", "enthusiastic", "envious", "evil", "excited", "expensive", "exuberant", "fair", "faithful", "famous", "fancy", "fantastic", "fierce", "filthy", "fine", "foolish", "fragile", "frail", "frantic", "friendly", "frightened", "funny", "gentle", "gifted", "glamorous", "gleaming", "glorious", "good", "gorgeous", "graceful", "grieving", "grotesque", "grumpy", "handsome", "happy", "healthy", "helpful", "helpless", "hilarious", "homeless", "homely", "horrible", "hungry", "hurt", "ill", "important", "impossible", "inexpensive", "innocent", "inquisitive", "itchy", "jealous", "jittery", "jolly", "joyous", "kind", "lazy", "light", "lively", "lonely", "long", "lovely", "lucky", "magnificent", "misty", "modern", "motionless", "muddy", "mushy", "mysterious", "nasty", "naughty", "nervous", "nice", "nutty", "obedient", "obnoxious", "odd", "old-fashioned", "open", "outrageous", "outstanding", "panicky", "perfect", "plain", "pleasant", "poised", "poor", "powerful", "precious", "prickly", "proud", "putrid", "puzzled", "quaint", "real", "relieved", "repulsive", "rich", "scary", "selfish", "shiny", "shy", "silly", "sleepy", "smiling", "smoggy", "sore", "sparkling", "splendid", "spotless", "stormy", "strange", "stupid", "successful", "super", "talented", "tame", "tasty", "tender", "tense", "terrible", "thankful", "thoughtful", "thoughtless", "tired", "tough", "troubled", "ugliest", "ugly", "uninterested", "unsightly", "unusual", "upset", "uptight", "vast", "victorious", "vivacious", "wandering", "weary", "wicked", "wide-eyed", "wild", "witty", "worried", "worrisome", "wrong", "zany", "zealous"] |
| 15 |
|
| 16 |
nouns = ["apple", "air", "conditioner", "airport", "ambulance", "aircraft", "apartment", "arrow", "antlers", "apro", "alligator", "architect", "ankle", "armchair", "aunt", "ball", "bermudas", "beans", "balloon", "bear", "blouse", "bed", "bow", "bread", "black", "board", "bones", "bill", "bitterness", "boxers", "belt", "brain", "buffalo", "bird", "baby", "book", "back", "butter", "bulb", "buckles", "bat", "bank", "bag", "bra", "boots", "blazer", "bikini", "bookcase", "bookstore", "bus", "stop", "brass", "brother", "boy", "blender", "bucket", "bakery", "bow", "bridge", "boat", "car", "cow", "cap", "cooker", "cheeks", "cheese", "credenza", "carpet", "crow", "crest", "chest", "chair", "candy", "cabinet", "cat", "coffee", "children", "cookware", "chaise", "longue", "chicken", "casino", "cabin", "castle", "church", "cafe", "cinema", "choker", "cravat", "cane", "costume", "cardigan", "chocolate", "crib", "couch", "cello", "cashier", "composer", "cave", "country", "computer", "canoe", "clock", "charlie", "dog", "deer", "donkey", "desk", "desktop", "dress", "dolphin", "doctor", "dentist", "drum", "dresser", "designer", "detective", "daughter", "egg", "elephant", "earrings", "ears", "eyes", "estate", "finger", "fox", "frock", "frog", "fan", "freezer", "fish", "film", "foot", "flag", "factory", "father", "farm", "forest", "flower", "fruit", "fork", "grapes", "goat", "gown", "garlic", "ginger", "giraffe", "gauva", "grains", "gas", "station", "garage", "gloves", "glasses", "gift", "galaxy", "guitar", "grandmother", "grandfather", "governor", "girl", "guest", "hamburger", "hand", "head", "hair", "heart", "house", "horse", "hen", "horn", "hat", "hammer", "hostel", "hospital", "hotel", "heels", "herbs", "host", "jacket", "jersey", "jewelry", "jaw", "jumper", "judge", "juicer", "keyboard", "kid", "kangaroo", "koala", "knife", "lemon", "lion", "leggings", "leg", "laptop", "library", "lamb", "london", "lips", "lung", "lighter", "luggage", "lamp", "lawyer", "mouse", "monkey", "mouth", "mango", "mobile", "milk", "music", "mirror", "musician", "mother", "man", "model", "mall", "museum", "market", "moonlight", "medicine", "microscope", "newspaper", "nose", "notebook", "neck", "noodles", "nurse", "necklace", "noise", "ocean", "ostrich", "oil", "orange", "onion", "oven", "owl", "paper", "panda", "pants", "palm", "pasta", "pumpkin", "pharmacist", "potato", "parfume", "panther", "pad", "pencil", "pipe", "police", "pen", "pharmacy", "petrol", "station", "police", "station", "parrot", "plane", "pigeon", "phone", "peacock", "pencil", "pig", "pouch", "pagoda", "pyramid", "purse", "pancake", "popcorn", "piano", "physician", "photographer", "professor", "painter", "park", "plant", "parfume", "radio", "razor", "ribs", "rainbow", "ring", "rabbit", "rice", "refrigerator", "remote", "restaurant", "road", "surgeon", "scale", "shampoo", "sink", "salt", "shark", "sandals", "shoulder", "spoon", "soap", "sand", "sheep", "sari", "stomach", "stairs", "soup", "shoes", "scissors", "sparrow", "shirt", "suitcase", "stove", "stairs", "snowman", "shower", "swan", "suit", "sweater", "smoke", "skirt", "sofa", "socks", "stadium", "skyscraper", "school", "sunglasses", "sandals", "slippers", "shorts", "sandwich", "strawberry", "spaghetti", "shrimp", "saxophone", "sister", "son", "singer", "senator", "street", "supermarket", "swimming", "pool", "star", "sky", "sun", "spoon", "ship", "smile", "table", "turkey", "tie", "toes", "truck", "train", "taxi", "tiger", "trousers", "tongue", "television", "teacher", "turtle", "tablet", "train", "station", "toothpaste", "tail", "theater", "trench", "coat", "tea", "tomato", "teen", "tunnel", "temple", "town", "toothbrush", "tree", "toy", "tissue", "telephone", "underwear", "uncle", "umbrella", "vest", "voice", "veterinarian", "villa", "violin", "village", "vehicle", "vase", "wallet", "wolf", "waist", "wrist", "water", "melon", "whale", "water", "wings", "whisker", "watch", "woman", "washing", "machine", "wheelchair", "waiter", "wound", "xylophone", "zebra", "zoo"] |
| 17 |
|
| 18 |
def with_chance(chance): |
| 19 |
return random.random() <= chance |
| 20 |
|
| 21 |
class Chance(): |
| 22 |
def __init__(self): |
| 23 |
self.value = random.random() |
| 24 |
self.start = 0 |
| 25 |
|
| 26 |
def with_chance(self, chance): |
| 27 |
if self.start > 1: |
| 28 |
print("Too many choices") |
| 29 |
start = self.start |
| 30 |
end = self.start + chance |
| 31 |
self.start = end |
| 32 |
return self.value >= start and self.value < end |
| 33 |
|
| 34 |
# Choose one of weighted options |
| 35 |
def choice(self, options): |
| 36 |
for value, chance in options: |
| 37 |
if self.with_chance(chance): |
| 38 |
return value |
| 39 |
# Default to first |
| 40 |
value, chance = options[0] |
| 41 |
return value |
| 42 |
|
| 43 |
def gen_dir_mode(): |
| 44 |
# For creation to work we want all dirs u+rwx |
| 45 |
return random.choice([0o777, 0o755, 0o750, 0o700]) |
| 46 |
|
| 47 |
def gen_file_mode(): |
| 48 |
return random.choice([0o644, 0o666, 0o755, 0o777]) |
| 49 |
|
| 50 |
def gen_filename(): |
| 51 |
if not args.unreadable: |
| 52 |
name = bytes(random.choice(adjectives) + "_" + random.choice(nouns) + str(random.randint(1,999)), "utf-8") |
| 53 |
if len(name) > 255: |
| 54 |
return gen_filename() |
| 55 |
return name |
| 56 |
|
| 57 |
name_len = random.randrange(1, 255) |
| 58 |
name = [0] * name_len |
| 59 |
for i in range(name_len): |
| 60 |
c = random.randrange(1, 255) |
| 61 |
while c == ord('/'): |
| 62 |
c = random.randrange(1, 255) |
| 63 |
name[i] = c |
| 64 |
name=bytes(name) |
| 65 |
if name == b'.' or name == b'..': |
| 66 |
return gen_filename() |
| 67 |
return name |
| 68 |
|
| 69 |
def gen_filenames(): |
| 70 |
c = Chance() |
| 71 |
# 5% of dirs are huge |
| 72 |
if c.with_chance(0.05): |
| 73 |
num_files = random.randrange(0, 4096) |
| 74 |
else: |
| 75 |
num_files = random.randrange(0, 25) |
| 76 |
|
| 77 |
files = [] |
| 78 |
for i in range(num_files): |
| 79 |
files.append(gen_filename()) |
| 80 |
|
| 81 |
return list(sorted(set(files))) |
| 82 |
|
| 83 |
def gen_xattrname(): |
| 84 |
return random.choice(nouns) + str(random.randint(1,9)) |
| 85 |
|
| 86 |
def gen_xattrdata(): |
| 87 |
return bytes(random.choice(adjectives) + str(random.randint(1,9)), "utf-8") |
| 88 |
|
| 89 |
|
| 90 |
def gen_hierarchy(root): |
| 91 |
num_dirs = random.randrange(30, 50) |
| 92 |
dirs = [] |
| 93 |
for i in range(num_dirs): |
| 94 |
parent = random.choice([root] * 3 + dirs); |
| 95 |
p = os.path.join(parent, gen_filename()) |
| 96 |
dirs.append(p) |
| 97 |
# Sort and drop any (unlikely) duplicateds |
| 98 |
return list(sorted(set(dirs))) |
| 99 |
|
| 100 |
def set_user_xattr(path): |
| 101 |
n_xattrs = random.randrange(0, 3) |
| 102 |
for i in range(n_xattrs): |
| 103 |
name = "user." + gen_xattrname() |
| 104 |
value = gen_xattrdata() |
| 105 |
try: |
| 106 |
os.setxattr(path, name, value, follow_symlinks=False) |
| 107 |
except OSError as e: |
| 108 |
# Not much we can do if the backing fs doesn't allow xattrs |
| 109 |
if e.errno not in (errno.EPERM, errno.ENOTSUP): |
| 110 |
raise |
| 111 |
|
| 112 |
old_files = [] |
| 113 |
def make_regular_file(path): |
| 114 |
with os.fdopen(os.open(path, os.O_WRONLY|os.O_CREAT, gen_file_mode()), 'wb') as fd: |
| 115 |
c = Chance(); |
| 116 |
# 5% of reuse old file data |
| 117 |
if len(old_files) > 0 and c.with_chance(0.05): |
| 118 |
reused = random.choice(old_files) |
| 119 |
with os.fdopen(os.open(reused, os.O_RDONLY), 'rb') as src: |
| 120 |
shutil.copyfileobj(src, fd) |
| 121 |
return |
| 122 |
|
| 123 |
# 5% of files are large |
| 124 |
if c.with_chance(0.05): |
| 125 |
size = random.randrange(0, 4*1024*1024) |
| 126 |
else: # Rest are small |
| 127 |
size = random.randrange(0, 256) |
| 128 |
|
| 129 |
data = random.randbytes(size) |
| 130 |
fd.write(data) |
| 131 |
# Save path for reuse |
| 132 |
old_files.append(path) |
| 133 |
|
| 134 |
set_user_xattr(path) |
| 135 |
|
| 136 |
def make_symlink(path): |
| 137 |
target = gen_filename() |
| 138 |
os.symlink(target, path) |
| 139 |
|
| 140 |
def make_node(path): |
| 141 |
if not args.privileged: |
| 142 |
return |
| 143 |
target = gen_filename() |
| 144 |
os.mknod(path, gen_file_mode() | random.choice([stat.S_IFCHR,stat.S_IFBLK]), os.makedev(random.randrange(1, 255),random.randrange(1, 255))) |
| 145 |
|
| 146 |
def make_whiteout(path): |
| 147 |
if args.nowhiteout: |
| 148 |
return |
| 149 |
target = gen_filename() |
| 150 |
os.mknod(path, gen_file_mode() | stat.S_IFCHR, device=os.makedev(0,0)) |
| 151 |
|
| 152 |
def make_fifo(path): |
| 153 |
target = gen_filename() |
| 154 |
os.mknod(path, gen_file_mode() | stat.S_IFIFO) |
| 155 |
|
| 156 |
def make_file(path): |
| 157 |
c = Chance(); |
| 158 |
f = c.choice([ |
| 159 |
(make_regular_file, 0.7), |
| 160 |
(make_symlink, 0.15), |
| 161 |
(make_fifo, 0.05), |
| 162 |
(make_node, 0.05), |
| 163 |
(make_whiteout, 0.05) |
| 164 |
]) |
| 165 |
f(path) |
| 166 |
|
| 167 |
def make_dir(path, dirs): |
| 168 |
os.mkdir(path, mode=gen_dir_mode()) |
| 169 |
set_user_xattr(path) |
| 170 |
files = gen_filenames() |
| 171 |
for f in files: |
| 172 |
child_path = os.path.join(path, f) |
| 173 |
if child_path in dirs: |
| 174 |
continue |
| 175 |
|
| 176 |
func = random.choice([make_file]) |
| 177 |
func(child_path) |
| 178 |
|
| 179 |
argParser = argparse.ArgumentParser() |
| 180 |
argParser.add_argument("--seed") |
| 181 |
argParser.add_argument("--unreadable", action='store_true') |
| 182 |
argParser.add_argument("--privileged", action='store_true') |
| 183 |
argParser.add_argument("--nowhiteout", action='store_true') |
| 184 |
argParser.add_argument('path') |
| 185 |
|
| 186 |
args = argParser.parse_args() |
| 187 |
|
| 188 |
if args.seed: |
| 189 |
seed = args.seed |
| 190 |
else: |
| 191 |
seed = os.urandom(16).hex() |
| 192 |
random.seed(seed) |
| 193 |
print(f"Using seed '{seed}'") |
| 194 |
|
| 195 |
# Generate tree structure |
| 196 |
root = bytes(args.path,"utf-8") |
| 197 |
dirs = gen_hierarchy(root) |
| 198 |
|
| 199 |
make_dir(root, dirs) |
| 200 |
for d in dirs: |
| 201 |
make_dir(d, dirs) |