File size: 9,996 Bytes
7e9dc27
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
------------------------------------------------------------------
--
--  Author: Alexey Melnichuk <alexeymelnichuck@gmail.com>
--
--  Copyright (C) 2013-2016 Alexey Melnichuk <alexeymelnichuck@gmail.com>
--
--  Licensed according to the included 'LICENCE' document
--
--  This file is part of lua-path library.
--
------------------------------------------------------------------

return function(lfs)
local os  = require "os"

local DIR_SEP = package.config:sub(1,1)
local IS_WINDOWS = DIR_SEP == '\\'

local _M = {
  DIR_SEP = DIR_SEP;
}

_M.currentdir = lfs.currentdir

_M.attributes = lfs.attributes

-- function _M.flags(P) end

local attrib = lfs.attributes

function _M.ctime(P)   return attrib(P,'change') end

function _M.atime(P)   return attrib(P,'access') end

function _M.mtime(P)   return attrib(P,'modification') end

function _M.size(P)    return attrib(P,'size') end

function _M.exists(P)  return attrib(P,'mode') ~= nil and P end

function _M.isdir(P)   return attrib(P,'mode') == 'directory' and P end

function _M.isfile(P)  return attrib(P,'mode') == 'file' and P end

function _M.islink(P)  return attrib(P,'mode') == 'link' and P end

_M.mkdir = lfs.mkdir

_M.rmdir = lfs.rmdir

_M.chdir = lfs.chdir

_M.link  = lfs.link

_M.setmode = lfs.setmode

function _M.copy(src, dst, force)
  if not IS_WINDOWS then
    if _M.isdir(src) or _M.isdir(dst) then
      return nil, 'can not copy directories'
    end
  end
  local f, err = io.open(src, 'rb')
  if not f then return nil, err end

  if not force then
    local t, err = io.open(dst, 'rb' )
    if t then 
      f:close()
      t:close()
      return nil, "file alredy exists"
    end
  end

  local t, err = io.open(dst, 'w+b')
  if not t then
    f:close()
    return nil, err
  end

  local CHUNK_SIZE = 4096
  while true do
    local chunk = f:read(CHUNK_SIZE)
    if not chunk then break end
    local ok, err = t:write(chunk)
    if not ok then
      t:close()
      f:close()
      return nil, err or "can not write"
    end
  end

  t:close()
  f:close()
  return true
end

function _M.move(src, dst, flags)
  if flags and _M.exists(dst) and _M.exists(src) then
    local ok, err = _M.remove(dst)
    -- do we have to remove dir?
    -- if not ok then ok, err = _M.rmdir(dst) end
    if not ok then return nil, err end
  end
  if (not IS_WINDOWS) and _M.exists(dst) then
    -- on windows os.rename return error when dst exists, 
    -- but on linux its just replace existed file
    return nil, "destination alredy exists"
  end
  return os.rename(src, dst)
end

function _M.remove(P)
  -- on windows os.remove can not remove dir
  if (not IS_WINDOWS) and _M.isdir(P) then
    return nil, "remove method can not remove dirs"
  end
  return os.remove(P)
end

local function splitpath(P) return string.match(P,"^(.-)[\\/]?([^\\/]*)$") end

function _M.tmpdir()
  if IS_WINDOWS then
    for _, p in ipairs{'TEMP', 'TMP'} do
      local dir = os.getenv(p)
      if dir and dir ~= '' then
        return dir
      end
    end
  end
  return (splitpath(os.tmpname()))
end

_M.dir = lfs.dir

_M.touch = lfs.touch

local function isdots(P)
  return P == '.' or P == '..'
end

local foreach_impl

local function do_foreach_recurse(base, match, callback, option)
  local dir_next, dir = lfs.dir(base)
  for name in dir_next, dir do if not isdots(name) then
    local path = base .. DIR_SEP .. name
    if _M.attributes(path,"mode") == "directory" then
      local ret, err = foreach_impl(path, match, callback, option)
      if ret or err then
        if dir then dir:close() end
        return ret, err
      end
    end
  end end
end

foreach_impl = function(base, match, callback, option)
  local tmp, origin_cb
  if option.delay then
    tmp, origin_cb, callback = {}, callback, function(base,name,fd) 
      table.insert(tmp, {base,name,fd})
    end;
  end

  if option.recurse and option.reverse == true then
    local ok, err = do_foreach_recurse(base, match, callback, option)
    if ok or err then return ok, err end
  end

  local dir_next, dir = lfs.dir(base)
  for name in dir_next, dir do if option.skipdots == false or not isdots(name) then
    local path = base .. DIR_SEP .. name
    local attr = _M.attributes(path)
    if not attr then return end

    if (option.skipdirs  and attr.mode == "directory")
       or (option.skipfiles and attr.mode == "file")
    then else
      if match(name) then
        local ret, err = callback(base, name, attr)
        if ret or err then
          if dir then dir:close() end
          return ret, err
        end
      end
    end

    local can_recurse = (not option.delay) and option.recurse and (option.reverse == nil)
    if can_recurse and attr.mode == "directory" and not isdots(name) then
      local ret, err = foreach_impl(path, match, callback, option)
      if ret or err then
        if dir then dir:close() end
        return ret, err
      end
    end
  end end

  if option.delay then
    for _, t in ipairs(tmp) do
      local ok, err = origin_cb(t[1], t[2], t[3])
      if ok or err then return ok, err end
    end
  end

  if option.recurse and (not option.reverse) then
    if option.delay or (option.reverse == false) then
      return do_foreach_recurse(base, match, origin_cb or callback, option)
    end
  end
end

local function filePat2rexPat(pat)
  if pat:find("[*?]") then
    local post = '$'
    if pat:find("*", 1, true) then 
      if pat:find(".", 1, true) then post = '[^.]*$'
      else post = '' end
    end
    pat = "^" .. pat:gsub("%.","%%."):gsub("%*",".*"):gsub("%?", ".?") .. post
  else
    pat = "^" .. pat:gsub("%.","%%.") .. "$"
  end
  if IS_WINDOWS then pat = pat:upper() end
  return pat
end

local function match_pat(pat)
  pat = filePat2rexPat(pat)
  return IS_WINDOWS
  and function(s) return nil ~= string.find(string.upper(s), pat) end
  or  function(s) return nil ~= string.find(s, pat)               end
end

function _M.foreach(base, callback, option)
  local base, mask = splitpath(base, DIR_SEP)
  if mask ~= '' then mask = match_pat(mask)
  else mask = function() return true end end
  return foreach_impl(base, mask, function(base, name, fd)
    return callback(base .. DIR_SEP .. name, fd)
  end, option or {})
end

local attribs = {
  f = function(base, name, fd) return base..DIR_SEP..name  end;
  p = function(base, name, fd) return base                 end;
  n = function(base, name, fd) return name                 end;
  m = function(base, name, fd) return fd.mode              end;
  a = function(base, name, fd) return fd                   end;
  z = function(base, name, fd) return fd.size              end;
  t = function(base, name, fd) return fd.modification      end;
  c = function(base, name, fd) return fd.change            end;
  l = function(base, name, fd) return fd.access            end;
}

local function make_attrib(str)
  local t = {}
  for i = 1, #str do 
    local ch = str:sub(i,i)
    local fn = attribs[ ch ]
    if not fn then return nil, 'unknown file attribute: ' .. ch end
    table.insert(t, fn)
  end

  return function(...)
    local res = {n = #t}
    for i, f in ipairs(t) do
      local ok, err = f(...)
      if ok == nil then return nil, err end
      table.insert(res, ok)
    end
    return res
  end
end

function _M.each_impl(option)
  if not option.file then return nil, 'no file mask present' end
  local base, mask = splitpath( option.file, DIR_SEP )
  if mask ~= '' then mask = match_pat(mask)
  else mask = function() return true end end

  local get_params, err = make_attrib(option.param or 'f')
  if not get_params then return nil, err end
  local unpack = unpack or table.unpack

  local filter = option.filter

  if option.callback then
    local callback = option.callback 

    local function cb(base, name, fd)
      local params = assert(get_params(base, name, fd))
      if filter and (not filter(unpack(params, 1, params.n))) then return end
      return callback(unpack(params, 1, params.n))
    end

    return foreach_impl(base, mask, cb, option)
  else
    local function cb(base, name, fd)
      local params = assert(get_params(base, name, fd))
      if filter and (not filter(unpack(params, 1, params.n))) then return end
      coroutine.yield(params)
    end
    local co = coroutine.create(function()
      foreach_impl(base, mask, cb, option)
    end)
    return function()
      local status, params = coroutine.resume(co)
      if status then if params then return unpack(params, 1, params.n) end
      else error(params, 2) end
    end
  end
end

local create_each = require "path.findfile".load

_M.each = create_each(_M.each_impl)

local function match_pat_selftest()

  local t = {
    ["*.txt"] = {
      [".txt"     ] = true;
      ["1.txt"    ] = true;
      ["1.txtdat" ] = true;
      [".txtdat"  ] = false;
      [".txt.dat" ] = false;
      [".dat.txt" ] = true;
    };
    ["*.txt*"] = {
      [".txt"     ] = true;
      ["1.txt"    ] = true;
      ["1.txtdat" ] = true;
      [".txtdat"  ] = true;
      [".txt.dat" ] = true;
      [".dat.txt" ] = true;
    };
    ["?.txt"] = {
      [".txt"     ] = true;
      ["1.txt"    ] = true;
      ["1.txtdat" ] = false;
      [".txtdat"  ] = false;
      [".txt.dat" ] = false;
      [".dat.txt" ] = false;
    };
    ["1?.txt"] = {
      [".txt"     ] = false;
      ["1.txt"    ] = true;
      ["1.txtdat" ] = false;
      [".txtdat"  ] = false;
      [".txt.dat" ] = false;
      [".dat.txt" ] = false;
    };
    ["1*.txt"] = {
      [".txt"     ] = false;
      ["1.txt"    ] = true;
      ["1.txtdat" ] = true;
      [".txtdat"  ] = false;
      [".txt.dat" ] = false;
      [".dat.txt" ] = false;
    };
  }

  local function test_match(pat, t)
    local cmp = match_pat(pat)
    for fname, status in pairs(t) do 
      if status ~= cmp(fname) then
        io.write("Pat: ", pat, " Name: ", fname, " Expected: ", tostring(status), " Got: ", tostring(cmp(fname)), "\n")
      end
    end
  end

  for k, v in pairs(t) do
    test_match(k,v)
  end

end

return _M
end