You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dbfs.go 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright 2022 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package dbfs
  4. import (
  5. "context"
  6. "os"
  7. "code.gitea.io/gitea/models/db"
  8. )
  9. type dbfsMeta struct {
  10. ID int64 `xorm:"pk autoincr"`
  11. FullPath string `xorm:"VARCHAR(500) UNIQUE NOT NULL"`
  12. BlockSize int64 `xorm:"BIGINT NOT NULL"`
  13. FileSize int64 `xorm:"BIGINT NOT NULL"`
  14. CreateTimestamp int64 `xorm:"BIGINT NOT NULL"`
  15. ModifyTimestamp int64 `xorm:"BIGINT NOT NULL"`
  16. }
  17. type dbfsData struct {
  18. ID int64 `xorm:"pk autoincr"`
  19. Revision int64 `xorm:"BIGINT NOT NULL"`
  20. MetaID int64 `xorm:"BIGINT index(meta_offset) NOT NULL"`
  21. BlobOffset int64 `xorm:"BIGINT index(meta_offset) NOT NULL"`
  22. BlobSize int64 `xorm:"BIGINT NOT NULL"`
  23. BlobData []byte `xorm:"BLOB NOT NULL"`
  24. }
  25. func init() {
  26. db.RegisterModel(new(dbfsMeta))
  27. db.RegisterModel(new(dbfsData))
  28. }
  29. func OpenFile(ctx context.Context, name string, flag int) (File, error) {
  30. f, err := newDbFile(ctx, name)
  31. if err != nil {
  32. return nil, err
  33. }
  34. err = f.open(flag)
  35. if err != nil {
  36. _ = f.Close()
  37. return nil, err
  38. }
  39. return f, nil
  40. }
  41. func Open(ctx context.Context, name string) (File, error) {
  42. return OpenFile(ctx, name, os.O_RDONLY)
  43. }
  44. func Create(ctx context.Context, name string) (File, error) {
  45. return OpenFile(ctx, name, os.O_RDWR|os.O_CREATE|os.O_TRUNC)
  46. }
  47. func Rename(ctx context.Context, oldPath, newPath string) error {
  48. f, err := newDbFile(ctx, oldPath)
  49. if err != nil {
  50. return err
  51. }
  52. defer f.Close()
  53. return f.renameTo(newPath)
  54. }
  55. func Remove(ctx context.Context, name string) error {
  56. f, err := newDbFile(ctx, name)
  57. if err != nil {
  58. return err
  59. }
  60. defer f.Close()
  61. return f.delete()
  62. }