backend.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. package file
  2. import (
  3. "context"
  4. "io/ioutil"
  5. "log"
  6. "os"
  7. "path/filepath"
  8. "strings"
  9. "time"
  10. "github.com/containerd/continuity/fs"
  11. "github.com/docker/docker/pkg/idtools"
  12. "github.com/moby/buildkit/snapshot"
  13. "github.com/moby/buildkit/solver/llbsolver/ops/fileoptypes"
  14. "github.com/moby/buildkit/solver/pb"
  15. "github.com/pkg/errors"
  16. copy "github.com/tonistiigi/fsutil/copy"
  17. )
  18. func timestampToTime(ts int64) *time.Time {
  19. if ts == -1 {
  20. return nil
  21. }
  22. tm := time.Unix(ts/1e9, ts%1e9)
  23. return &tm
  24. }
  25. func mapUser(user *copy.ChownOpt, idmap *idtools.IdentityMapping) (*copy.ChownOpt, error) {
  26. if idmap == nil || user == nil {
  27. return user, nil
  28. }
  29. identity, err := idmap.ToHost(idtools.Identity{
  30. UID: user.Uid,
  31. GID: user.Gid,
  32. })
  33. if err != nil {
  34. return nil, err
  35. }
  36. return &copy.ChownOpt{Uid: identity.UID, Gid: identity.GID}, nil
  37. }
  38. func mkdir(ctx context.Context, d string, action pb.FileActionMkDir, user *copy.ChownOpt, idmap *idtools.IdentityMapping) error {
  39. p, err := fs.RootPath(d, filepath.Join(filepath.Join("/", action.Path)))
  40. if err != nil {
  41. return err
  42. }
  43. user, err = mapUser(user, idmap)
  44. if err != nil {
  45. return err
  46. }
  47. if action.MakeParents {
  48. if err := copy.MkdirAll(p, os.FileMode(action.Mode)&0777, user, timestampToTime(action.Timestamp)); err != nil {
  49. return err
  50. }
  51. } else {
  52. if err := os.Mkdir(p, os.FileMode(action.Mode)&0777); err != nil {
  53. if os.IsExist(err) {
  54. return nil
  55. }
  56. return err
  57. }
  58. if err := copy.Chown(p, user); err != nil {
  59. return err
  60. }
  61. if err := copy.Utimes(p, timestampToTime(action.Timestamp)); err != nil {
  62. return err
  63. }
  64. }
  65. return nil
  66. }
  67. func mkfile(ctx context.Context, d string, action pb.FileActionMkFile, user *copy.ChownOpt, idmap *idtools.IdentityMapping) error {
  68. p, err := fs.RootPath(d, filepath.Join(filepath.Join("/", action.Path)))
  69. if err != nil {
  70. return err
  71. }
  72. user, err = mapUser(user, idmap)
  73. if err != nil {
  74. return err
  75. }
  76. if err := ioutil.WriteFile(p, action.Data, os.FileMode(action.Mode)&0777); err != nil {
  77. return err
  78. }
  79. if err := copy.Chown(p, user); err != nil {
  80. return err
  81. }
  82. if err := copy.Utimes(p, timestampToTime(action.Timestamp)); err != nil {
  83. return err
  84. }
  85. return nil
  86. }
  87. func rm(ctx context.Context, d string, action pb.FileActionRm) error {
  88. p, err := fs.RootPath(d, filepath.Join(filepath.Join("/", action.Path)))
  89. if err != nil {
  90. return err
  91. }
  92. if err := os.RemoveAll(p); err != nil {
  93. if os.IsNotExist(errors.Cause(err)) && action.AllowNotFound {
  94. return nil
  95. }
  96. return err
  97. }
  98. return nil
  99. }
  100. func docopy(ctx context.Context, src, dest string, action pb.FileActionCopy, u *copy.ChownOpt, idmap *idtools.IdentityMapping) error {
  101. srcPath := cleanPath(action.Src)
  102. destPath := cleanPath(action.Dest)
  103. if !action.CreateDestPath {
  104. p, err := fs.RootPath(dest, filepath.Join(filepath.Join("/", action.Dest)))
  105. if err != nil {
  106. return err
  107. }
  108. if _, err := os.Lstat(filepath.Dir(p)); err != nil {
  109. return errors.Wrapf(err, "failed to stat %s", action.Dest)
  110. }
  111. }
  112. xattrErrorHandler := func(dst, src, key string, err error) error {
  113. log.Println(err)
  114. return nil
  115. }
  116. u, err := mapUser(u, idmap)
  117. if err != nil {
  118. return err
  119. }
  120. opt := []copy.Opt{
  121. func(ci *copy.CopyInfo) {
  122. ci.Chown = u
  123. ci.Utime = timestampToTime(action.Timestamp)
  124. if m := int(action.Mode); m != -1 {
  125. ci.Mode = &m
  126. }
  127. ci.CopyDirContents = action.DirCopyContents
  128. ci.FollowLinks = action.FollowSymlink
  129. },
  130. copy.WithXAttrErrorHandler(xattrErrorHandler),
  131. }
  132. if !action.AllowWildcard {
  133. if action.AttemptUnpackDockerCompatibility {
  134. if ok, err := unpack(ctx, src, srcPath, dest, destPath, u, timestampToTime(action.Timestamp)); err != nil {
  135. return err
  136. } else if ok {
  137. return nil
  138. }
  139. }
  140. return copy.Copy(ctx, src, srcPath, dest, destPath, opt...)
  141. }
  142. m, err := copy.ResolveWildcards(src, srcPath, action.FollowSymlink)
  143. if err != nil {
  144. return err
  145. }
  146. if len(m) == 0 {
  147. if action.AllowEmptyWildcard {
  148. return nil
  149. }
  150. return errors.Errorf("%s not found", srcPath)
  151. }
  152. for _, s := range m {
  153. if action.AttemptUnpackDockerCompatibility {
  154. if ok, err := unpack(ctx, src, s, dest, destPath, u, timestampToTime(action.Timestamp)); err != nil {
  155. return err
  156. } else if ok {
  157. continue
  158. }
  159. }
  160. if err := copy.Copy(ctx, src, s, dest, destPath, opt...); err != nil {
  161. return err
  162. }
  163. }
  164. return nil
  165. }
  166. func cleanPath(s string) string {
  167. s2 := filepath.Join("/", s)
  168. if strings.HasSuffix(s, "/.") {
  169. if s2 != "/" {
  170. s2 += "/"
  171. }
  172. s2 += "."
  173. } else if strings.HasSuffix(s, "/") && s2 != "/" {
  174. s2 += "/"
  175. }
  176. return s2
  177. }
  178. type Backend struct {
  179. }
  180. func (fb *Backend) Mkdir(ctx context.Context, m, user, group fileoptypes.Mount, action pb.FileActionMkDir) error {
  181. mnt, ok := m.(*Mount)
  182. if !ok {
  183. return errors.Errorf("invalid mount type %T", m)
  184. }
  185. lm := snapshot.LocalMounter(mnt.m)
  186. dir, err := lm.Mount()
  187. if err != nil {
  188. return err
  189. }
  190. defer lm.Unmount()
  191. u, err := readUser(action.Owner, user, group)
  192. if err != nil {
  193. return err
  194. }
  195. return mkdir(ctx, dir, action, u, mnt.m.IdentityMapping())
  196. }
  197. func (fb *Backend) Mkfile(ctx context.Context, m, user, group fileoptypes.Mount, action pb.FileActionMkFile) error {
  198. mnt, ok := m.(*Mount)
  199. if !ok {
  200. return errors.Errorf("invalid mount type %T", m)
  201. }
  202. lm := snapshot.LocalMounter(mnt.m)
  203. dir, err := lm.Mount()
  204. if err != nil {
  205. return err
  206. }
  207. defer lm.Unmount()
  208. u, err := readUser(action.Owner, user, group)
  209. if err != nil {
  210. return err
  211. }
  212. return mkfile(ctx, dir, action, u, mnt.m.IdentityMapping())
  213. }
  214. func (fb *Backend) Rm(ctx context.Context, m fileoptypes.Mount, action pb.FileActionRm) error {
  215. mnt, ok := m.(*Mount)
  216. if !ok {
  217. return errors.Errorf("invalid mount type %T", m)
  218. }
  219. lm := snapshot.LocalMounter(mnt.m)
  220. dir, err := lm.Mount()
  221. if err != nil {
  222. return err
  223. }
  224. defer lm.Unmount()
  225. return rm(ctx, dir, action)
  226. }
  227. func (fb *Backend) Copy(ctx context.Context, m1, m2, user, group fileoptypes.Mount, action pb.FileActionCopy) error {
  228. mnt1, ok := m1.(*Mount)
  229. if !ok {
  230. return errors.Errorf("invalid mount type %T", m1)
  231. }
  232. mnt2, ok := m2.(*Mount)
  233. if !ok {
  234. return errors.Errorf("invalid mount type %T", m2)
  235. }
  236. lm := snapshot.LocalMounter(mnt1.m)
  237. src, err := lm.Mount()
  238. if err != nil {
  239. return err
  240. }
  241. defer lm.Unmount()
  242. lm2 := snapshot.LocalMounter(mnt2.m)
  243. dest, err := lm2.Mount()
  244. if err != nil {
  245. return err
  246. }
  247. defer lm2.Unmount()
  248. u, err := readUser(action.Owner, user, group)
  249. if err != nil {
  250. return err
  251. }
  252. return docopy(ctx, src, dest, action, u, mnt2.m.IdentityMapping())
  253. }