diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/luarocks/tools/patch.tl | 36 |
1 files changed, 15 insertions, 21 deletions
diff --git a/src/luarocks/tools/patch.tl b/src/luarocks/tools/patch.tl index 95715176..1babbc88 100644 --- a/src/luarocks/tools/patch.tl +++ b/src/luarocks/tools/patch.tl | |||
@@ -47,6 +47,10 @@ local type Hunk = patch.Hunk | |||
47 | local type Files = patch.Files | 47 | local type Files = patch.Files |
48 | local type File = patch.File | 48 | local type File = patch.File |
49 | 49 | ||
50 | local function open(filename: string, mode?: io.OpenMode): FILE | ||
51 | return assert(io.open(fs.absolute_name(filename), mode) as (FILE, string)) | ||
52 | end | ||
53 | |||
50 | -- logging | 54 | -- logging |
51 | local debugmode = false | 55 | local debugmode = false |
52 | local function debug(_: string) end --! | 56 | local function debug(_: string) end --! |
@@ -75,14 +79,6 @@ local function table_copy<K, V>(t: {K: V}): {K: V} | |||
75 | return t2 | 79 | return t2 |
76 | end | 80 | end |
77 | 81 | ||
78 | local function exists(filename: string): boolean | ||
79 | local fh = io.open(filename) | ||
80 | local result = fh ~= nil | ||
81 | if fh then fh:close() end | ||
82 | return result | ||
83 | end | ||
84 | local function isfile(): boolean return true end --FIX? --! | ||
85 | |||
86 | local function string_as_file(s: string): File --! | 82 | local function string_as_file(s: string): File --! |
87 | return { | 83 | return { |
88 | at = 0, | 84 | at = 0, |
@@ -188,7 +184,7 @@ function patch.read_patch(filename: string, data: string): Files, boolean | |||
188 | if data then | 184 | if data then |
189 | fp = string_as_file(data) as FILE --! cast | 185 | fp = string_as_file(data) as FILE --! cast |
190 | else | 186 | else |
191 | fp = filename == '-' and io.stdin or assert(io.open(filename, "rb") as (FILE, string)) --! use of cast | 187 | fp = filename == '-' and io.stdin or open(filename, "rb") |
192 | end | 188 | end |
193 | local lineno = 0 | 189 | local lineno = 0 |
194 | 190 | ||
@@ -386,7 +382,6 @@ function patch.read_patch(filename: string, data: string): Files, boolean | |||
386 | if state ~= 'hunkskip' then | 382 | if state ~= 'hunkskip' then |
387 | warning(string.format("patch file incomplete - %s", filename)) | 383 | warning(string.format("patch file incomplete - %s", filename)) |
388 | all_ok = false | 384 | all_ok = false |
389 | -- os.exit(?) | ||
390 | else | 385 | else |
391 | -- duplicated message when an eof is reached | 386 | -- duplicated message when an eof is reached |
392 | if debugmode and #files.source > 0 then | 387 | if debugmode and #files.source > 0 then |
@@ -444,7 +439,7 @@ local function find_hunk(file: {string}, h: Hunk, hno: integer): boolean | |||
444 | end | 439 | end |
445 | 440 | ||
446 | local function load_file(filename: string): {string} | 441 | local function load_file(filename: string): {string} |
447 | local fp = assert(io.open(filename) as (FILE, string)) --! cast | 442 | local fp = open(filename) |
448 | local file = {} | 443 | local file = {} |
449 | local readline = file_lines(fp) | 444 | local readline = file_lines(fp) |
450 | while true do | 445 | while true do |
@@ -495,8 +490,8 @@ local function check_patched(file: {string}, hunks: {Hunk}): boolean | |||
495 | end | 490 | end |
496 | 491 | ||
497 | local function patch_hunks(srcname: string, tgtname: string, hunks: {Hunk}): boolean | 492 | local function patch_hunks(srcname: string, tgtname: string, hunks: {Hunk}): boolean |
498 | local src = assert(io.open(srcname, "rb") as (FILE, string)) --! cast | 493 | local src = open(srcname, "rb") |
499 | local tgt = assert(io.open(tgtname, "wb") as (FILE, string)) --! cast | 494 | local tgt = open(tgtname, "wb") |
500 | 495 | ||
501 | local src_readline = file_lines(src) | 496 | local src_readline = file_lines(src) |
502 | 497 | ||
@@ -566,7 +561,7 @@ local function strip_dirs(filename: string, strip: integer): string | |||
566 | end | 561 | end |
567 | 562 | ||
568 | local function write_new_file(filename: string, hunk: Hunk): boolean, string | 563 | local function write_new_file(filename: string, hunk: Hunk): boolean, string |
569 | local fh = io.open(filename, "wb") | 564 | local fh = io.open(fs.absolute_name(filename), "wb") |
570 | if not fh then return false end | 565 | if not fh then return false end |
571 | for _, hline in ipairs(hunk.text) do | 566 | for _, hline in ipairs(hunk.text) do |
572 | local c = hline:sub(1,1) | 567 | local c = hline:sub(1,1) |
@@ -593,17 +588,16 @@ local function patch_file(source: string, target: string, epoch: boolean, hunks: | |||
593 | end | 588 | end |
594 | source = strip_dirs(source, strip) | 589 | source = strip_dirs(source, strip) |
595 | local f2patch = source | 590 | local f2patch = source |
596 | if not exists(f2patch) then | 591 | if not fs.exists(f2patch) then |
597 | f2patch = strip_dirs(target, strip) | 592 | f2patch = strip_dirs(target, strip) |
598 | f2patch = fs.absolute_name(f2patch) | 593 | f2patch = fs.absolute_name(f2patch) |
599 | if not exists(f2patch) then --FIX:if f2patch nil | 594 | if not fs.exists(f2patch) then |
600 | warning(string.format("source/target file does not exist\n--- %s\n+++ %s", | 595 | warning(string.format("source/target file does not exist\n--- %s\n+++ %s", |
601 | source, f2patch)) | 596 | source, f2patch)) |
602 | return false | 597 | return false |
603 | end | 598 | end |
604 | end | 599 | end |
605 | -- if not isfile(f2patch) then --! | 600 | if not fs.is_file(f2patch) then |
606 | if not isfile() then --! | ||
607 | warning(string.format("not a file - %s", f2patch)) | 601 | warning(string.format("not a file - %s", f2patch)) |
608 | return false | 602 | return false |
609 | end | 603 | end |
@@ -624,7 +618,7 @@ local function patch_file(source: string, target: string, epoch: boolean, hunks: | |||
624 | 618 | ||
625 | if create_delete then | 619 | if create_delete then |
626 | if epoch and #hunks == 1 and hunks[1].starttgt == 0 and hunks[1].linestgt == 0 then | 620 | if epoch and #hunks == 1 and hunks[1].starttgt == 0 and hunks[1].linestgt == 0 then |
627 | local ok = os.remove(source) | 621 | local ok = fs.delete(fs.absolute_name(source)) |
628 | if not ok then | 622 | if not ok then |
629 | return false | 623 | return false |
630 | end | 624 | end |
@@ -717,14 +711,14 @@ local function patch_file(source: string, target: string, epoch: boolean, hunks: | |||
717 | backupname)) | 711 | backupname)) |
718 | return false | 712 | return false |
719 | end | 713 | end |
720 | local ok = os.rename(source, backupname) | 714 | local ok = fs.move(fs.absolute_name(source), fs.absolute_name(backupname)) |
721 | if not ok then | 715 | if not ok then |
722 | warning(string.format("failed backing up %s when patching", source)) | 716 | warning(string.format("failed backing up %s when patching", source)) |
723 | return false | 717 | return false |
724 | end | 718 | end |
725 | patch_hunks(backupname, source, hunks) | 719 | patch_hunks(backupname, source, hunks) |
726 | info(string.format("successfully patched %s", source)) | 720 | info(string.format("successfully patched %s", source)) |
727 | os.remove(backupname) | 721 | fs.delete(fs.absolute_name(backupname)) |
728 | return true | 722 | return true |
729 | end | 723 | end |
730 | 724 | ||