diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/luarocks/tools/patch.lua | 23 |
1 files changed, 13 insertions, 10 deletions
diff --git a/src/luarocks/tools/patch.lua b/src/luarocks/tools/patch.lua index c9567bc6..5c21eaf0 100644 --- a/src/luarocks/tools/patch.lua +++ b/src/luarocks/tools/patch.lua | |||
@@ -1,4 +1,4 @@ | |||
1 | local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = pcall(require, 'compat53.module'); if p then _tl_compat = m end end; local assert = _tl_compat and _tl_compat.assert or assert; local debug = _tl_compat and _tl_compat.debug or debug; local io = _tl_compat and _tl_compat.io or io; local ipairs = _tl_compat and _tl_compat.ipairs or ipairs; local math = _tl_compat and _tl_compat.math or math; local os = _tl_compat and _tl_compat.os or os; local pairs = _tl_compat and _tl_compat.pairs or pairs; local pcall = _tl_compat and _tl_compat.pcall or pcall; local string = _tl_compat and _tl_compat.string or string; local table = _tl_compat and _tl_compat.table or table | 1 | local _tl_compat; if (tonumber((_VERSION or ''):match('[%d.]*$')) or 0) < 5.3 then local p, m = pcall(require, 'compat53.module'); if p then _tl_compat = m end end; local assert = _tl_compat and _tl_compat.assert or assert; local io = _tl_compat and _tl_compat.io or io; local ipairs = _tl_compat and _tl_compat.ipairs or ipairs; local math = _tl_compat and _tl_compat.math or math; local pairs = _tl_compat and _tl_compat.pairs or pairs; local pcall = _tl_compat and _tl_compat.pcall or pcall; local string = _tl_compat and _tl_compat.string or string; local table = _tl_compat and _tl_compat.table or table |
2 | 2 | ||
3 | 3 | ||
4 | 4 | ||
@@ -47,6 +47,10 @@ local fs = require("luarocks.fs") | |||
47 | 47 | ||
48 | 48 | ||
49 | 49 | ||
50 | local function open(filename, mode) | ||
51 | return assert(io.open(fs.absolute_name(filename), mode)) | ||
52 | end | ||
53 | |||
50 | 54 | ||
51 | local debugmode = false | 55 | local debugmode = false |
52 | local function debug(_) end | 56 | local function debug(_) end |
@@ -188,7 +192,7 @@ function patch.read_patch(filename, data) | |||
188 | if data then | 192 | if data then |
189 | fp = string_as_file(data) | 193 | fp = string_as_file(data) |
190 | else | 194 | else |
191 | fp = filename == '-' and io.stdin or assert(io.open(filename, "rb")) | 195 | fp = filename == '-' and io.stdin or open(filename, "rb") |
192 | end | 196 | end |
193 | local lineno = 0 | 197 | local lineno = 0 |
194 | 198 | ||
@@ -386,7 +390,6 @@ function patch.read_patch(filename, data) | |||
386 | if state ~= 'hunkskip' then | 390 | if state ~= 'hunkskip' then |
387 | warning(string.format("patch file incomplete - %s", filename)) | 391 | warning(string.format("patch file incomplete - %s", filename)) |
388 | all_ok = false | 392 | all_ok = false |
389 | |||
390 | else | 393 | else |
391 | 394 | ||
392 | if debugmode and #files.source > 0 then | 395 | if debugmode and #files.source > 0 then |
@@ -444,7 +447,7 @@ local function find_hunk(file, h, hno) | |||
444 | end | 447 | end |
445 | 448 | ||
446 | local function load_file(filename) | 449 | local function load_file(filename) |
447 | local fp = assert(io.open(filename)) | 450 | local fp = open(filename) |
448 | local file = {} | 451 | local file = {} |
449 | local readline = file_lines(fp) | 452 | local readline = file_lines(fp) |
450 | while true do | 453 | while true do |
@@ -495,8 +498,8 @@ local function check_patched(file, hunks) | |||
495 | end | 498 | end |
496 | 499 | ||
497 | local function patch_hunks(srcname, tgtname, hunks) | 500 | local function patch_hunks(srcname, tgtname, hunks) |
498 | local src = assert(io.open(srcname, "rb")) | 501 | local src = open(srcname, "rb") |
499 | local tgt = assert(io.open(tgtname, "wb")) | 502 | local tgt = open(tgtname, "wb") |
500 | 503 | ||
501 | local src_readline = file_lines(src) | 504 | local src_readline = file_lines(src) |
502 | 505 | ||
@@ -566,7 +569,7 @@ local function strip_dirs(filename, strip) | |||
566 | end | 569 | end |
567 | 570 | ||
568 | local function write_new_file(filename, hunk) | 571 | local function write_new_file(filename, hunk) |
569 | local fh = io.open(filename, "wb") | 572 | local fh = io.open(fs.absolute_name(filename), "wb") |
570 | if not fh then return false end | 573 | if not fh then return false end |
571 | for _, hline in ipairs(hunk.text) do | 574 | for _, hline in ipairs(hunk.text) do |
572 | local c = hline:sub(1, 1) | 575 | local c = hline:sub(1, 1) |
@@ -624,7 +627,7 @@ local function patch_file(source, target, epoch, hunks, strip, create_delete) | |||
624 | 627 | ||
625 | if create_delete then | 628 | if create_delete then |
626 | if epoch and #hunks == 1 and hunks[1].starttgt == 0 and hunks[1].linestgt == 0 then | 629 | if epoch and #hunks == 1 and hunks[1].starttgt == 0 and hunks[1].linestgt == 0 then |
627 | local ok = os.remove(source) | 630 | local ok = fs.delete(fs.absolute_name(source)) |
628 | if not ok then | 631 | if not ok then |
629 | return false | 632 | return false |
630 | end | 633 | end |
@@ -717,14 +720,14 @@ local function patch_file(source, target, epoch, hunks, strip, create_delete) | |||
717 | backupname)) | 720 | backupname)) |
718 | return false | 721 | return false |
719 | end | 722 | end |
720 | local ok = os.rename(source, backupname) | 723 | local ok = fs.move(fs.absolute_name(source), fs.absolute_name(backupname)) |
721 | if not ok then | 724 | if not ok then |
722 | warning(string.format("failed backing up %s when patching", source)) | 725 | warning(string.format("failed backing up %s when patching", source)) |
723 | return false | 726 | return false |
724 | end | 727 | end |
725 | patch_hunks(backupname, source, hunks) | 728 | patch_hunks(backupname, source, hunks) |
726 | info(string.format("successfully patched %s", source)) | 729 | info(string.format("successfully patched %s", source)) |
727 | os.remove(backupname) | 730 | fs.delete(fs.absolute_name(backupname)) |
728 | return true | 731 | return true |
729 | end | 732 | end |
730 | 733 | ||