|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +package marketplace |
| 4 | + |
| 5 | +import ( |
| 6 | + "archive/zip" |
| 7 | + "bytes" |
| 8 | + "fmt" |
| 9 | + "io" |
| 10 | + |
| 11 | + modelsdk "github.com/mendixlabs/mxcli" |
| 12 | + "github.com/mendixlabs/mxcli/internal/zipentry" |
| 13 | + "github.com/mendixlabs/mxcli/mdl/types" |
| 14 | + "go.mongodb.org/mongo-driver/v2/bson" |
| 15 | +) |
| 16 | + |
| 17 | +// A module package normally carries its model as a v1 .mpr — an SQLite |
| 18 | +// database. Packages exported as protected (add-on) modules, delivered as |
| 19 | +// .mxmodule, carry something else under the same name: one BSON document, |
| 20 | +// {_ProductVersion, Contents}, where Contents is the whole project as a single |
| 21 | +// tree with every unit nested inside its container. Measured on Menditect's |
| 22 | +// MtaPluginModule 11.5.5 (Marketplace 214717): `marketplace install` failed on |
| 23 | +// it with "file is not a database", and `mx module-import` refuses it outright |
| 24 | +// ("Importing protected module is not supported", exit 111). |
| 25 | +// |
| 26 | +// The tree holds exactly the units a .mpr would, so it is split back into them |
| 27 | +// and written into the reference project with the same raw-unit writer the |
| 28 | +// transplant uses. Unit contents are copied as they are, protection settings |
| 29 | +// (ModuleSettings.ProtectedModuleType, ExportLevel) included: the result is the |
| 30 | +// module as the package ships it, not an unprotected copy. |
| 31 | + |
| 32 | +// sqliteMagic opens every SQLite database file, and so every v1 .mpr. |
| 33 | +var sqliteMagic = []byte("SQLite format 3\x00") |
| 34 | + |
| 35 | +// unitContainments are the properties under which a unit holds other units. |
| 36 | +// They are stored as separate units in a .mpr, and the parent's own contents |
| 37 | +// carry none of them — measured on an 11.15 MPR v2 project: a Projects$Folder |
| 38 | +// unit is {$ID, $Type, Name}, a Projects$ModuleImpl has no Folders, Documents, |
| 39 | +// DomainModel, ModuleSettings or ModuleSecurity key. |
| 40 | +var unitContainments = map[string]bool{ |
| 41 | + "Folders": true, |
| 42 | + "Documents": true, |
| 43 | + "DomainModel": true, |
| 44 | + "ModuleSettings": true, |
| 45 | + "ModuleSecurity": true, |
| 46 | +} |
| 47 | + |
| 48 | +// treeUnit is one unit cut out of a module tree, ready for AddRawUnit. |
| 49 | +type treeUnit struct { |
| 50 | + id, containerID, containment, unitType string |
| 51 | + contents []byte |
| 52 | +} |
| 53 | + |
| 54 | +// IsModuleTreePackage reports whether the package's project.mpr is a module tree |
| 55 | +// rather than an SQLite .mpr. |
| 56 | +func IsModuleTreePackage(mpkPath string) (bool, error) { |
| 57 | + data, err := readZipEntry(mpkPath, packageProjectEntry) |
| 58 | + if err != nil { |
| 59 | + return false, err |
| 60 | + } |
| 61 | + return isModuleTree(data), nil |
| 62 | +} |
| 63 | + |
| 64 | +func isModuleTree(data []byte) bool { |
| 65 | + if bytes.HasPrefix(data, sqliteMagic) || len(data) < 5 { |
| 66 | + return false |
| 67 | + } |
| 68 | + var head struct { |
| 69 | + ProductVersion string `bson:"_ProductVersion"` |
| 70 | + } |
| 71 | + return bson.Unmarshal(data, &head) == nil && head.ProductVersion != "" |
| 72 | +} |
| 73 | + |
| 74 | +// moduleTreeUnits splits the named module of a module tree into units, parents |
| 75 | +// before children. The module unit's containerID is left empty: the caller |
| 76 | +// attaches it to its own project unit. |
| 77 | +func moduleTreeUnits(data []byte, moduleName string) ([]treeUnit, error) { |
| 78 | + var root bson.D |
| 79 | + if err := bson.Unmarshal(data, &root); err != nil { |
| 80 | + return nil, fmt.Errorf("decode module tree: %w", err) |
| 81 | + } |
| 82 | + contents, ok := lookup(root, "Contents").(bson.D) |
| 83 | + if !ok { |
| 84 | + return nil, fmt.Errorf("module tree has no Contents document") |
| 85 | + } |
| 86 | + modules, _ := lookup(contents, "Modules").(bson.A) |
| 87 | + for _, m := range modules { |
| 88 | + mod, ok := m.(bson.D) |
| 89 | + if !ok || !equalFold(stringOf(lookup(mod, "Name")), moduleName) { |
| 90 | + continue |
| 91 | + } |
| 92 | + var units []treeUnit |
| 93 | + if err := splitUnit(mod, "", projectContainment, &units); err != nil { |
| 94 | + return nil, err |
| 95 | + } |
| 96 | + return units, nil |
| 97 | + } |
| 98 | + return nil, fmt.Errorf("module %q not found in the package's module tree", moduleName) |
| 99 | +} |
| 100 | + |
| 101 | +// splitUnit appends doc as a unit, then recurses into the units it contains. |
| 102 | +func splitUnit(doc bson.D, containerID, containment string, units *[]treeUnit) error { |
| 103 | + id, err := idOf(doc) |
| 104 | + if err != nil { |
| 105 | + return err |
| 106 | + } |
| 107 | + own := make(bson.D, 0, len(doc)) |
| 108 | + var children []bson.E |
| 109 | + for _, e := range doc { |
| 110 | + if unitContainments[e.Key] { |
| 111 | + children = append(children, e) |
| 112 | + continue |
| 113 | + } |
| 114 | + own = append(own, e) |
| 115 | + } |
| 116 | + encoded, err := bson.Marshal(own) |
| 117 | + if err != nil { |
| 118 | + return fmt.Errorf("encode unit %s: %w", id, err) |
| 119 | + } |
| 120 | + *units = append(*units, treeUnit{ |
| 121 | + id: id, |
| 122 | + containerID: containerID, |
| 123 | + containment: containment, |
| 124 | + unitType: stringOf(lookup(doc, "$Type")), |
| 125 | + contents: encoded, |
| 126 | + }) |
| 127 | + for _, c := range children { |
| 128 | + switch v := c.Value.(type) { |
| 129 | + case bson.D: |
| 130 | + if err := splitUnit(v, id, c.Key, units); err != nil { |
| 131 | + return err |
| 132 | + } |
| 133 | + case bson.A: |
| 134 | + // A Mendix list stores its storage-kind marker as element 0; the |
| 135 | + // units are the documents after it. |
| 136 | + for _, item := range v { |
| 137 | + if child, ok := item.(bson.D); ok { |
| 138 | + if err := splitUnit(child, id, c.Key, units); err != nil { |
| 139 | + return err |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + return nil |
| 146 | +} |
| 147 | + |
| 148 | +// InsertModuleTree writes the package's module into mprPath, a project that does |
| 149 | +// not have it yet, attached to that project's project unit. It is the |
| 150 | +// module-tree counterpart of `mx module-import` for the reference project. |
| 151 | +func InsertModuleTree(mpkPath, mprPath, moduleName string) (int, error) { |
| 152 | + data, err := readZipEntry(mpkPath, packageProjectEntry) |
| 153 | + if err != nil { |
| 154 | + return 0, err |
| 155 | + } |
| 156 | + units, err := moduleTreeUnits(data, moduleName) |
| 157 | + if err != nil { |
| 158 | + return 0, err |
| 159 | + } |
| 160 | + projectID, err := projectUnitID(mprPath) |
| 161 | + if err != nil { |
| 162 | + return 0, err |
| 163 | + } |
| 164 | + if err := refuseIfPresent(mprPath, moduleName); err != nil { |
| 165 | + return 0, err |
| 166 | + } |
| 167 | + writer, err := modelsdk.OpenForWriting(mprPath) |
| 168 | + if err != nil { |
| 169 | + return 0, fmt.Errorf("open %s for writing: %w", mprPath, err) |
| 170 | + } |
| 171 | + defer writer.Disconnect() |
| 172 | + for i, u := range units { |
| 173 | + container := u.containerID |
| 174 | + if container == "" { |
| 175 | + container = projectID |
| 176 | + } |
| 177 | + if err := writer.AddRawUnit(u.id, container, u.containment, u.unitType, u.contents); err != nil { |
| 178 | + return i, fmt.Errorf("write unit %s (%s): %w", u.id, u.unitType, err) |
| 179 | + } |
| 180 | + } |
| 181 | + return len(units), nil |
| 182 | +} |
| 183 | + |
| 184 | +func lookup(doc bson.D, key string) any { |
| 185 | + for _, e := range doc { |
| 186 | + if e.Key == key { |
| 187 | + return e.Value |
| 188 | + } |
| 189 | + } |
| 190 | + return nil |
| 191 | +} |
| 192 | + |
| 193 | +func stringOf(v any) string { |
| 194 | + s, _ := v.(string) |
| 195 | + return s |
| 196 | +} |
| 197 | + |
| 198 | +func idOf(doc bson.D) (string, error) { |
| 199 | + switch v := lookup(doc, "$ID").(type) { |
| 200 | + case bson.Binary: |
| 201 | + if len(v.Data) == 16 { |
| 202 | + return types.BlobToUUID(v.Data), nil |
| 203 | + } |
| 204 | + case string: |
| 205 | + if v != "" { |
| 206 | + return v, nil |
| 207 | + } |
| 208 | + } |
| 209 | + return "", fmt.Errorf("%s without a 16-byte $ID in the module tree", stringOf(lookup(doc, "$Type"))) |
| 210 | +} |
| 211 | + |
| 212 | +func readZipEntry(zipPath, entry string) ([]byte, error) { |
| 213 | + zr, err := zip.OpenReader(zipPath) |
| 214 | + if err != nil { |
| 215 | + return nil, err |
| 216 | + } |
| 217 | + defer zr.Close() |
| 218 | + for _, f := range zr.File { |
| 219 | + if f.Name != entry { |
| 220 | + continue |
| 221 | + } |
| 222 | + rc, err := zipentry.Open(f) |
| 223 | + if err != nil { |
| 224 | + return nil, err |
| 225 | + } |
| 226 | + defer rc.Close() |
| 227 | + return io.ReadAll(rc) |
| 228 | + } |
| 229 | + return nil, fmt.Errorf("no %s in %s", entry, zipPath) |
| 230 | +} |
0 commit comments