backend.go 6.6 KB

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