|
| 1 | +package installer |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "sort" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/php-debugger/installer/internal/ini" |
| 11 | + "github.com/php-debugger/installer/internal/php" |
| 12 | +) |
| 13 | + |
| 14 | +// configPair is a source ini file and where its (sanitized) copy is written. |
| 15 | +type configPair struct{ src, dst string } |
| 16 | + |
| 17 | +// copyConfig copies the existing interpreter's ini files into the new |
| 18 | +// interpreter's compiled-in config path (so the new php loads the same |
| 19 | +// configuration), sanitizing each on the way: xdebug loader lines are stripped, |
| 20 | +// and disallowed xdebug.mode tokens are removed after confirmation. |
| 21 | +// |
| 22 | +// It registers undo steps on rb (restoring overwritten files / removing created |
| 23 | +// ones) and returns the list of destination files written. |
| 24 | +func copyConfig(existing, target *php.Info, rb *rollback, opts Options) ([]string, error) { |
| 25 | + pairs := configPairs(existing, target) |
| 26 | + if len(pairs) == 0 { |
| 27 | + if target.Ini.ConfigPath == "" && target.Ini.ScanDir == "" { |
| 28 | + opts.logf("Note: the interpreter reports no config path; skipping ini copy.") |
| 29 | + } |
| 30 | + return nil, nil |
| 31 | + } |
| 32 | + |
| 33 | + stripModes, err := decideStripModes(pairs, opts) |
| 34 | + if err != nil { |
| 35 | + return nil, err |
| 36 | + } |
| 37 | + |
| 38 | + var written []string |
| 39 | + for _, pr := range pairs { |
| 40 | + data, err := os.ReadFile(pr.src) |
| 41 | + if err != nil { |
| 42 | + return written, fmt.Errorf("reading ini %s: %w", pr.src, err) |
| 43 | + } |
| 44 | + content, removedLoaders := ini.StripXdebugLoaders(string(data)) |
| 45 | + content, commentedLoaders := ini.CommentExtensionLoaders(content) |
| 46 | + if stripModes { |
| 47 | + content, _, _ = ini.SanitizeXdebugMode(content) |
| 48 | + } |
| 49 | + |
| 50 | + if err := registerConfigUndo(pr.dst, rb); err != nil { |
| 51 | + return written, err |
| 52 | + } |
| 53 | + if err := os.MkdirAll(filepath.Dir(pr.dst), 0o755); err != nil { |
| 54 | + return written, fmt.Errorf("creating config dir: %w", err) |
| 55 | + } |
| 56 | + if err := os.WriteFile(pr.dst, []byte(content), 0o644); err != nil { |
| 57 | + return written, fmt.Errorf("writing ini %s: %w", pr.dst, err) |
| 58 | + } |
| 59 | + written = append(written, pr.dst) |
| 60 | + opts.logf(" wrote %s%s", pr.dst, loaderNote(len(removedLoaders), len(commentedLoaders))) |
| 61 | + } |
| 62 | + return written, nil |
| 63 | +} |
| 64 | + |
| 65 | +// configPairs builds the (source, destination) list: the existing main php.ini |
| 66 | +// goes to the new interpreter's ConfigPath, and each additional .ini goes to its |
| 67 | +// ScanDir (by base name). |
| 68 | +func configPairs(existing, target *php.Info) []configPair { |
| 69 | + var pairs []configPair |
| 70 | + if existing.Ini.LoadedFile != "" && target.Ini.ConfigPath != "" { |
| 71 | + pairs = append(pairs, configPair{ |
| 72 | + src: existing.Ini.LoadedFile, |
| 73 | + dst: filepath.Join(target.Ini.ConfigPath, "php.ini"), |
| 74 | + }) |
| 75 | + } |
| 76 | + if target.Ini.ScanDir != "" { |
| 77 | + for _, f := range existing.Ini.AdditionalFiles { |
| 78 | + pairs = append(pairs, configPair{ |
| 79 | + src: f, |
| 80 | + dst: filepath.Join(target.Ini.ScanDir, filepath.Base(f)), |
| 81 | + }) |
| 82 | + } |
| 83 | + } |
| 84 | + return pairs |
| 85 | +} |
| 86 | + |
| 87 | +// decideStripModes scans the source ini files for disallowed xdebug.mode tokens |
| 88 | +// and, if any are found, asks the user whether to remove them (auto-yes under |
| 89 | +// --yes). Returns true if the caller should sanitize xdebug.mode. |
| 90 | +func decideStripModes(pairs []configPair, opts Options) (bool, error) { |
| 91 | + seen := map[string]bool{} |
| 92 | + var disallowed []string |
| 93 | + for _, pr := range pairs { |
| 94 | + data, err := os.ReadFile(pr.src) |
| 95 | + if err != nil { |
| 96 | + return false, fmt.Errorf("reading ini %s: %w", pr.src, err) |
| 97 | + } |
| 98 | + for _, m := range ini.DisallowedModes(string(data)) { |
| 99 | + if !seen[m] { |
| 100 | + seen[m] = true |
| 101 | + disallowed = append(disallowed, m) |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + if len(disallowed) == 0 { |
| 106 | + return false, nil |
| 107 | + } |
| 108 | + sort.Strings(disallowed) |
| 109 | + return opts.confirm(fmt.Sprintf( |
| 110 | + "xdebug.mode lists disallowed mode(s): %s. Remove them (keeping only off/debug)?", |
| 111 | + strings.Join(disallowed, ", "))), nil |
| 112 | +} |
| 113 | + |
| 114 | +// registerConfigUndo records how to undo writing dst: restore its prior contents |
| 115 | +// if it existed, otherwise remove it on rollback. |
| 116 | +func registerConfigUndo(dst string, rb *rollback) error { |
| 117 | + if prev, err := os.ReadFile(dst); err == nil { |
| 118 | + rb.add(func() error { return os.WriteFile(dst, prev, 0o644) }) |
| 119 | + } else if os.IsNotExist(err) { |
| 120 | + rb.add(func() error { return removeIfExists(dst) }) |
| 121 | + } else { |
| 122 | + return fmt.Errorf("inspecting %s: %w", dst, err) |
| 123 | + } |
| 124 | + return nil |
| 125 | +} |
| 126 | + |
| 127 | +func removeIfExists(path string) error { |
| 128 | + if err := os.Remove(path); err != nil && !os.IsNotExist(err) { |
| 129 | + return err |
| 130 | + } |
| 131 | + return nil |
| 132 | +} |
| 133 | + |
| 134 | +// loaderNote summarizes what happened to extension loaders in a copied ini file. |
| 135 | +func loaderNote(removed, commented int) string { |
| 136 | + var parts []string |
| 137 | + if removed > 0 { |
| 138 | + parts = append(parts, fmt.Sprintf("removed %d xdebug loader(s)", removed)) |
| 139 | + } |
| 140 | + if commented > 0 { |
| 141 | + parts = append(parts, fmt.Sprintf("commented %d extension loader(s)", commented)) |
| 142 | + } |
| 143 | + if len(parts) == 0 { |
| 144 | + return "" |
| 145 | + } |
| 146 | + return " (" + strings.Join(parts, ", ") + ")" |
| 147 | +} |
0 commit comments