Debian Codegraph @

t/cmd/lfstest-badlocalfile.go

raw symbols SCIP
1 //go:build testtools
2 // +build testtools
3
4 package main
5
6 import (
7 "os"
8 "strings"
9
10 "github.com/git-lfs/pktline"
11 )
12
13 func main() {
14 pl := pktline.NewPktline(os.Stdin, os.Stdout)
15
16 // For any file Git asks us to clean, rename it and create a
17 // 0-byte file in its place. This allows Git to continue to
18 // read from the original file and send us its contents via
19 // packets on stdin, while our filter-process command will
20 // see the 0-byte file as soon as the first flush packet is
21 // received.
22 //
23 // If we simply truncate the file to 0 bytes in situ, Git
24 // may not be able to finish reading its full contents, and
25 // might send us fewer packets than actually needed.
26
27 var command, pathname string
28
29 for {
30 b, n, err := pl.ReadPacketWithLength()
31 if err != nil {
32 return
33 }
34
35 if n == 0 {
36 pl.WriteFlush()
37 } else if n == 1 {
38 pl.WriteDelim()
39 } else {
40 parts := strings.SplitN(string(b), "=", 2)
41 if len(parts) == 2 {
42 if parts[0] == "command" {
43 command = strings.TrimSuffix(parts[1], "\n")
44 } else if parts[0] == "pathname" {
45 pathname = strings.TrimSuffix(parts[1], "\n")
46 }
47
48 if command != "" && pathname != "" {
49 if command == "clean" {
50 if err = os.Rename(pathname, pathname+".orig"); err != nil {
51 panic(err)
52 }
53
54 f, err := os.OpenFile(pathname, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o644)
55 if err != nil {
56 panic(err)
57 }
58 f.Close()
59 }
60
61 command = ""
62 pathname = ""
63 }
64 }
65
66 pl.WritePacket(b)
67 }
68 }
69 }