sqlite.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package dataprovider
  2. import (
  3. "database/sql"
  4. "errors"
  5. "fmt"
  6. "os"
  7. "path/filepath"
  8. "github.com/drakkan/sftpgo/logger"
  9. )
  10. // SQLiteProvider auth provider for SQLite database
  11. type SQLiteProvider struct {
  12. }
  13. func initializeSQLiteProvider(basePath string) error {
  14. var err error
  15. var connectionString string
  16. if len(config.ConnectionString) == 0 {
  17. dbPath := config.Name
  18. if !filepath.IsAbs(dbPath) {
  19. dbPath = filepath.Join(basePath, dbPath)
  20. }
  21. fi, err := os.Stat(dbPath)
  22. if err != nil {
  23. logger.Warn(logSender, "sqlite database file does not exists, please be sure to create and initialize"+
  24. " a database before starting sftpgo")
  25. return err
  26. }
  27. if fi.Size() == 0 {
  28. return errors.New("sqlite database file is invalid, please be sure to create and initialize" +
  29. " a database before starting sftpgo")
  30. }
  31. connectionString = fmt.Sprintf("file:%v?cache=shared", dbPath)
  32. } else {
  33. connectionString = config.ConnectionString
  34. }
  35. dbHandle, err = sql.Open("sqlite3", connectionString)
  36. if err == nil {
  37. logger.Debug(logSender, "sqlite database handle created, connection string: '%v'", connectionString)
  38. dbHandle.SetMaxOpenConns(1)
  39. } else {
  40. logger.Warn(logSender, "error creating sqlite database handler, connection string: '%v', error: %v", connectionString, err)
  41. }
  42. return err
  43. }
  44. func (p SQLiteProvider) validateUserAndPass(username string, password string) (User, error) {
  45. return sqlCommonValidateUserAndPass(username, password)
  46. }
  47. func (p SQLiteProvider) validateUserAndPubKey(username string, publicKey string) (User, error) {
  48. return sqlCommonValidateUserAndPubKey(username, publicKey)
  49. }
  50. func (p SQLiteProvider) getUserByID(ID int64) (User, error) {
  51. return sqlCommonGetUserByID(ID)
  52. }
  53. func (p SQLiteProvider) updateQuota(username string, filesAdd int, sizeAdd int64, reset bool) error {
  54. // we keep only 1 open connection (SetMaxOpenConns(1)) so a transaction is not needed and it could block
  55. // the database access since it will try to open a new connection
  56. return sqlCommonUpdateQuota(username, filesAdd, sizeAdd, reset, p)
  57. }
  58. func (p SQLiteProvider) getUsedQuota(username string) (int, int64, error) {
  59. return sqlCommonGetUsedQuota(username)
  60. }
  61. func (p SQLiteProvider) userExists(username string) (User, error) {
  62. return sqlCommonCheckUserExists(username)
  63. }
  64. func (p SQLiteProvider) addUser(user User) error {
  65. return sqlCommonAddUser(user)
  66. }
  67. func (p SQLiteProvider) updateUser(user User) error {
  68. return sqlCommonUpdateUser(user)
  69. }
  70. func (p SQLiteProvider) deleteUser(user User) error {
  71. return sqlCommonDeleteUser(user)
  72. }
  73. func (p SQLiteProvider) getUsers(limit int, offset int, order string, username string) ([]User, error) {
  74. return sqlCommonGetUsers(limit, offset, order, username)
  75. }