Skip to content

Instantly share code, notes, and snippets.

@jochumdev
Last active April 16, 2025 07:07
Show Gist options
  • Save jochumdev/6c557f108eaba24f7866659830ce4e03 to your computer and use it in GitHub Desktop.
Save jochumdev/6c557f108eaba24f7866659830ce4e03 to your computer and use it in GitHub Desktop.
ocis->oc decomposed rewrite mpack
module github.com/opencloud-eu/mpak-convert
go 1.24
require github.com/shamaton/msgpack/v2 v2.2.3
Copyright 2025 Rene Jochum
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package main
import (
"flag"
"fmt"
"maps"
"os"
"path/filepath"
"slices"
"strings"
"github.com/shamaton/msgpack/v2"
)
func main() {
rewriteCmd := flag.NewFlagSet("rewrite", flag.ExitOnError)
rewritePrefix := rewriteCmd.String("prefix", "user.ocis.", "extended attributes prefix to match")
rewriteReplace := rewriteCmd.String("replace", "user.oc.", "replacement to use")
rewriteNoop := rewriteCmd.Bool("noop", false, "do not actually rewrite attributes")
if len(os.Args) < 3 {
fmt.Println("expected command and path")
os.Exit(1)
}
oneOf := []string{*rewritePrefix + "id", *rewritePrefix + "name", *rewritePrefix + "type"}
switch os.Args[1] {
case "rewrite":
rewriteCmd.Parse(os.Args[2:])
err := filepath.Walk(rewriteCmd.Args()[0], func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !strings.HasSuffix(path, ".mpk") {
return nil
}
fmt.Println(path, info.Size())
b, err := os.ReadFile(path)
if err != nil {
return err
}
m := map[string][]byte{}
if err := msgpack.Unmarshal(b, &m); err != nil {
return err
}
fileKeys := slices.Collect(maps.Keys(m))
// Skip files that do not have the prefix
found := false
for _, one := range oneOf {
if slices.Contains(fileKeys, one) {
found = true
break
}
}
if !found {
fmt.Println("# skipping", path)
return nil
}
for attr, value := range m {
if strings.HasPrefix(attr, *rewritePrefix) {
newAttr := *rewriteReplace + strings.TrimPrefix(attr, *rewritePrefix)
fmt.Println("#", attr, "->", newAttr)
if *rewriteNoop {
continue
}
// actual rewrite
m[newAttr] = value
// TODO: make optional? --remove? --noremove?
delete(m, attr)
}
}
b, err = msgpack.Marshal(m)
if err != nil {
return err
}
return os.WriteFile(path, b, 0600)
})
if err != nil {
fmt.Println(err)
}
default:
fmt.Println("unknown command")
os.Exit(1)
}
}
@jochumdev
Copy link
Author

ok, done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment