Debian Codegraph @

texmf-dist/doc/support/cluttex/src/texrunner/fsutil.lua

raw symbols SCIP
1 --[[
2 Copyright 2016 ARATA Mizuki
3
4 This file is part of ClutTeX.
5
6 ClutTeX 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.
10
11 ClutTeX 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.
15
16 You should have received a copy of the GNU General Public License
17 along with ClutTeX. If not, see <http://www.gnu.org/licenses/>.
18 ]]
19
20 local assert = assert
21 local os = os
22 local os_execute = os.execute
23 local os_remove = os.remove
24 local filesys = require "lfs"
25 local pathutil = require "texrunner.pathutil"
26 local shellutil = require "texrunner.shellutil"
27 local escape = shellutil.escape
28
29 local copy_command
30 if os.type == "windows" then
31 function copy_command(from, to)
32 -- TODO: What if `from` begins with a slash?
33 return "copy " .. escape(from) .. " " .. escape(to) .. " > NUL"
34 end
35 else
36 function copy_command(from, to)
37 -- TODO: What if `from` begins with a hypen?
38 return "cp " .. escape(from) .. " " .. escape(to)
39 end
40 end
41
42 local isfile = filesys.isfile or function(path)
43 return filesys.attributes(path, "mode") == "file"
44 end
45
46 local isdir = filesys.isdir or function(path)
47 return filesys.attributes(path, "mode") == "directory"
48 end
49
50 local function mkdir_rec(path)
51 local succ, err = filesys.mkdir(path)
52 if not succ then
53 succ, err = mkdir_rec(pathutil.parentdir(path))
54 if succ then
55 return filesys.mkdir(path)
56 end
57 end
58 return succ, err
59 end
60
61 local function remove_rec(path)
62 if isdir(path) then
63 for file in filesys.dir(path) do
64 if file ~= "." and file ~= ".." then
65 local succ, err = remove_rec(pathutil.join(path, file))
66 if not succ then
67 return succ, err
68 end
69 end
70 end
71 return filesys.rmdir(path)
72 else
73 return os_remove(path)
74 end
75 end
76
77 return {
78 copy_command = copy_command,
79 isfile = isfile,
80 isdir = isdir,
81 mkdir_rec = mkdir_rec,
82 remove_rec = remove_rec,
83 }