Blame view

go/src/themaru/env.go 1.12 KB
476d2547e   김태훈   태마루 시스템 설정 / 업데이트...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
  package main
  
  import (
  	"bytes"
  	"fmt"
  	"os"
  	"regexp"
  )
  
  func getEnv(blockn int) (map[string]string, error) {
  	m := make(map[string]string)
  
  	f, err := os.Open("/dev/mmcblk3boot1")
  	if err != nil {
  		return m, err
  	}
  	defer f.Close()
  
  	b := make([]byte, 512)
  	f.ReadAt(b, int64(blockn)*512)
  
  	n := bytes.IndexByte(b, 0)
  	s := string(b[:n])
  
  	r := regexp.MustCompile(`(.+?)=(.+)
  `)
  	ss := r.FindAllStringSubmatch(s, -1)
  	for _, v := range ss {
  		if len(v) != 3 {
  			continue
  		}
  
  		m[v[1]] = v[2]
  	}
  
  	return m, nil
  }
  
  func setEnv(blockn int, env map[string]string) error {
  	err := disableReadOnly()
  	if err != nil {
  		return err
  	}
  
  	f, err := os.OpenFile("/dev/mmcblk3boot1", os.O_RDWR, 0666)
  	if err != nil {
  		return err
  	}
  	defer f.Close()
  
  	var s string
  	for k, v := range env {
  		s += fmt.Sprintf("%s=%s
  ", k, v)
  	}
  	s += "\x00"
  
  	_, err = f.WriteAt([]byte(s), int64(blockn)*512)
  	if err != nil {
  		return err
  	}
  
  	return nil
  }
  
  func disableReadOnly() error {
  	f, err := os.OpenFile("/sys/block/mmcblk3boot1/force_ro", os.O_RDWR, 0666)
  	if err != nil {
  		return err
  	}
  	defer f.Close()
  
  	_, err = f.WriteString("0
  ")
  
  	return err
  }