mirror of
https://github.com/octoleo/lsyncd.git
synced 2025-01-07 09:04:05 +00:00
This commit is contained in:
parent
f64228d612
commit
ef9ceb9a4e
@ -38,16 +38,91 @@ local function dirname(dir, name)
|
||||
return dirname(dir.parent, dir.name .. "/" .. name)
|
||||
end
|
||||
|
||||
-----
|
||||
-- Picks a random dir.
|
||||
--
|
||||
local function pickDir(notRoot)
|
||||
if notRoot then
|
||||
if #alldirs <= 2 then
|
||||
return nil
|
||||
end
|
||||
return alldirs[2, math.random(#alldirs)]
|
||||
end
|
||||
return alldirs[math.random(#alldirs)]
|
||||
end
|
||||
|
||||
----
|
||||
-- Picks a random file.
|
||||
--
|
||||
-- Returns 3 values:
|
||||
-- * the directory
|
||||
-- * the filename
|
||||
-- * number of files in directory
|
||||
--
|
||||
local function pickFile()
|
||||
-- picks the random directory
|
||||
if #dirsWithFileI < 1 then
|
||||
return
|
||||
end
|
||||
local rdir = dirsWithFileI[math.random(1, #dirsWithFileI)]
|
||||
if not rdir then
|
||||
return
|
||||
end
|
||||
|
||||
-- counts the files in there
|
||||
local c = 0
|
||||
for name, _ in pairs(rdir) do
|
||||
if #name == 2 then
|
||||
c = c + 1
|
||||
end
|
||||
end
|
||||
|
||||
-- picks one file at random
|
||||
local cr = math.random(1, c)
|
||||
local fn
|
||||
for name, _ in pairs(rdir) do
|
||||
if #name == 2 then
|
||||
-- filenames are 2 chars wide.
|
||||
cr = cr - 1
|
||||
if cr == 0 then
|
||||
fn = name
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
return rdir, fn, c
|
||||
end
|
||||
|
||||
-----
|
||||
-- Removes a reference to a file
|
||||
--
|
||||
-- @param dir --- directory reference
|
||||
-- @param fn --- filename
|
||||
-- @param c --- number of files in dir
|
||||
--
|
||||
local function rmFileReference(dir, fn, c)
|
||||
dir[fn] = nil
|
||||
if c == 1 then
|
||||
-- if last file from origin dir, it has no files anymore
|
||||
for i, v in ipairs(dirsWithFileI) do
|
||||
if v == dir then
|
||||
table.remove(dirsWithFileI, i)
|
||||
break
|
||||
end
|
||||
end
|
||||
dirsWithFileD[dir] = nil
|
||||
end
|
||||
end
|
||||
|
||||
cwriteln("making random data")
|
||||
for ai=1,15 do
|
||||
-- throw a die what to do
|
||||
local acn = math.random(4)
|
||||
local acn = math.random(5)
|
||||
|
||||
if acn <= 1 then
|
||||
-- creates a directory
|
||||
-- chooses a random directory to create it into
|
||||
local rdir = alldirs[math.random(#alldirs)]
|
||||
local rdir = pickDir()
|
||||
-- creates a new random one letter name
|
||||
local nn = string.char(96 + math.random(26))
|
||||
if not rdir[nn] then
|
||||
@ -64,7 +139,7 @@ for ai=1,15 do
|
||||
elseif acn <= 2 then
|
||||
-- creates a file
|
||||
-- chooses a random directory to create it into
|
||||
local rdir = alldirs[math.random(#alldirs)]
|
||||
local rdir = pickDir()
|
||||
-- creates a new random one letter name
|
||||
local nn = 'f'..string.char(96 + math.random(26))
|
||||
local fn = dirname(rdir) .. nn
|
||||
@ -86,9 +161,9 @@ for ai=1,15 do
|
||||
-- moves a directory
|
||||
if #alldirs > 2 then
|
||||
-- chooses a random directory to move
|
||||
local odir = alldirs[math.random(2, #alldirs)]
|
||||
local odir = pickDir()
|
||||
-- chooses a random directory to move to
|
||||
local tdir = alldirs[math.random(1, #alldirs)]
|
||||
local tdir = pickDir(true)
|
||||
if tdir[odir.name] == nil then
|
||||
-- origin name not in target dir already
|
||||
local on = dirname(odir)
|
||||
@ -102,54 +177,31 @@ for ai=1,15 do
|
||||
end
|
||||
elseif acn <= 4 then
|
||||
-- moves a file
|
||||
if #dirsWithFileI > 1 then
|
||||
local odir, fn, c = pickFile()
|
||||
if odir then
|
||||
-- picks a directory with a file at random
|
||||
local odir = dirsWithFileI[math.random(1, #dirsWithFileI)]
|
||||
local nf = 0
|
||||
-- counts the files in there
|
||||
for name, _ in pairs(odir) do
|
||||
if #name == 2 then
|
||||
nf = nf + 1
|
||||
end
|
||||
end
|
||||
-- picks one file at random
|
||||
local nfr = math.random(1, nf)
|
||||
local mn
|
||||
for name, _ in pairs(odir) do
|
||||
if #name == 2 then
|
||||
-- filenames are 2 chars wide.
|
||||
nfr = nfr - 1
|
||||
if nfr == 0 then
|
||||
mn = name
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
print("MN", mn)
|
||||
-- picks a target directory at random
|
||||
local tdir = alldirs[math.random(1, #alldirs)]
|
||||
local tdir = pickdir()
|
||||
local on = dirname(odir)
|
||||
local tn = dirname(tdir)
|
||||
cwriteln("mvfile ",srcdir,on,mn," -> ",srcdir,tn,mn)
|
||||
os.rename(srcdir..on..mn, srcdir..tn..mn)
|
||||
os.exit(1)
|
||||
odir[mn] = nil
|
||||
rmFileReference(odir, mn, c)
|
||||
tdir[mn] = true
|
||||
if nf == 1 then
|
||||
-- if last file from origin dir, it has no file anymore
|
||||
for i, v in ipairs(dirsWithFileI) do
|
||||
if v == odir then
|
||||
table.remove(dirsWithFileI, i)
|
||||
break
|
||||
end
|
||||
end
|
||||
dirsWithFileD[odir] = nil
|
||||
end
|
||||
if not dirsWithFileD[tdir] then
|
||||
dirsWithFileD[tdir] = true
|
||||
table.insert(dirsWithFileI, tdir)
|
||||
end
|
||||
end
|
||||
elseif acn <= 5 then
|
||||
-- removes a file
|
||||
local dir, fn, c = pickFile()
|
||||
if dir then
|
||||
local dn = dirname(dir)
|
||||
cwriteln("rmfile ",srcdir,dn,fn)
|
||||
posix.unlink(srcdir..dn..fn)
|
||||
rmFileReference(odir, mn, c)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user