From e7d1ac84c32a10f8deb7bcfc94531386312179c7 Mon Sep 17 00:00:00 2001 From: Kegsay Date: Thu, 4 Jun 2020 11:18:08 +0100 Subject: [PATCH] Add ParseFileURI and use it when dealing with file URIs (#1088) * Add ParseFileURI and use it when dealing with file URIs Fixes #1059 * Missing file * Linting --- appservice/storage/sqlite3/storage.go | 6 ++++- appservice/storage/storage_wasm.go | 2 +- .../auth/storage/accounts/sqlite3/storage.go | 6 ++++- .../auth/storage/accounts/storage_wasm.go | 2 +- .../auth/storage/devices/sqlite3/storage.go | 6 ++++- .../auth/storage/devices/storage_wasm.go | 2 +- federationsender/storage/sqlite3/storage.go | 6 ++++- federationsender/storage/storage_wasm.go | 2 +- internal/basecomponent/base.go | 9 +++---- internal/sqlutil/uri.go | 24 +++++++++++++++++++ mediaapi/storage/sqlite3/storage.go | 6 ++++- mediaapi/storage/storage_wasm.go | 2 +- publicroomsapi/storage/sqlite3/storage.go | 6 ++++- publicroomsapi/storage/storage_wasm.go | 2 +- roomserver/storage/sqlite3/storage.go | 12 +--------- serverkeyapi/storage/sqlite3/keydb.go | 12 +--------- syncapi/storage/sqlite3/syncserver.go | 12 +--------- 17 files changed, 66 insertions(+), 51 deletions(-) create mode 100644 internal/sqlutil/uri.go diff --git a/appservice/storage/sqlite3/storage.go b/appservice/storage/sqlite3/storage.go index 275c66526..2238b3ff9 100644 --- a/appservice/storage/sqlite3/storage.go +++ b/appservice/storage/sqlite3/storage.go @@ -37,7 +37,11 @@ type Database struct { func NewDatabase(dataSourceName string) (*Database, error) { var result Database var err error - if result.db, err = sqlutil.Open(internal.SQLiteDriverName(), dataSourceName, nil); err != nil { + cs, err := sqlutil.ParseFileURI(dataSourceName) + if err != nil { + return nil, err + } + if result.db, err = sqlutil.Open(internal.SQLiteDriverName(), cs, nil); err != nil { return nil, err } if err = result.prepare(); err != nil { diff --git a/appservice/storage/storage_wasm.go b/appservice/storage/storage_wasm.go index de1acf92f..a6144b435 100644 --- a/appservice/storage/storage_wasm.go +++ b/appservice/storage/storage_wasm.go @@ -34,7 +34,7 @@ func NewDatabase( case "postgres": return nil, fmt.Errorf("Cannot use postgres implementation") case "file": - return sqlite3.NewDatabase(uri.Path) + return sqlite3.NewDatabase(dataSourceName) default: return nil, fmt.Errorf("Cannot use postgres implementation") } diff --git a/clientapi/auth/storage/accounts/sqlite3/storage.go b/clientapi/auth/storage/accounts/sqlite3/storage.go index 5233001f2..31426e471 100644 --- a/clientapi/auth/storage/accounts/sqlite3/storage.go +++ b/clientapi/auth/storage/accounts/sqlite3/storage.go @@ -48,7 +48,11 @@ type Database struct { func NewDatabase(dataSourceName string, serverName gomatrixserverlib.ServerName) (*Database, error) { var db *sql.DB var err error - if db, err = sqlutil.Open(internal.SQLiteDriverName(), dataSourceName, nil); err != nil { + cs, err := sqlutil.ParseFileURI(dataSourceName) + if err != nil { + return nil, err + } + if db, err = sqlutil.Open(internal.SQLiteDriverName(), cs, nil); err != nil { return nil, err } partitions := internal.PartitionOffsetStatements{} diff --git a/clientapi/auth/storage/accounts/storage_wasm.go b/clientapi/auth/storage/accounts/storage_wasm.go index 7cf50de79..81e77cf79 100644 --- a/clientapi/auth/storage/accounts/storage_wasm.go +++ b/clientapi/auth/storage/accounts/storage_wasm.go @@ -36,7 +36,7 @@ func NewDatabase( case "postgres": return nil, fmt.Errorf("Cannot use postgres implementation") case "file": - return sqlite3.NewDatabase(uri.Path, serverName) + return sqlite3.NewDatabase(dataSourceName, serverName) default: return nil, fmt.Errorf("Cannot use postgres implementation") } diff --git a/clientapi/auth/storage/devices/sqlite3/storage.go b/clientapi/auth/storage/devices/sqlite3/storage.go index 5cd894517..e05a53b46 100644 --- a/clientapi/auth/storage/devices/sqlite3/storage.go +++ b/clientapi/auth/storage/devices/sqlite3/storage.go @@ -41,7 +41,11 @@ type Database struct { func NewDatabase(dataSourceName string, serverName gomatrixserverlib.ServerName) (*Database, error) { var db *sql.DB var err error - if db, err = sqlutil.Open(internal.SQLiteDriverName(), dataSourceName, nil); err != nil { + cs, err := sqlutil.ParseFileURI(dataSourceName) + if err != nil { + return nil, err + } + if db, err = sqlutil.Open(internal.SQLiteDriverName(), cs, nil); err != nil { return nil, err } d := devicesStatements{} diff --git a/clientapi/auth/storage/devices/storage_wasm.go b/clientapi/auth/storage/devices/storage_wasm.go index c89ad887b..14c19e74b 100644 --- a/clientapi/auth/storage/devices/storage_wasm.go +++ b/clientapi/auth/storage/devices/storage_wasm.go @@ -36,7 +36,7 @@ func NewDatabase( case "postgres": return nil, fmt.Errorf("Cannot use postgres implementation") case "file": - return sqlite3.NewDatabase(uri.Path, serverName) + return sqlite3.NewDatabase(dataSourceName, serverName) default: return nil, fmt.Errorf("Cannot use postgres implementation") } diff --git a/federationsender/storage/sqlite3/storage.go b/federationsender/storage/sqlite3/storage.go index dc60ebb01..8699fc19a 100644 --- a/federationsender/storage/sqlite3/storage.go +++ b/federationsender/storage/sqlite3/storage.go @@ -38,7 +38,11 @@ type Database struct { func NewDatabase(dataSourceName string) (*Database, error) { var result Database var err error - if result.db, err = sqlutil.Open(internal.SQLiteDriverName(), dataSourceName, nil); err != nil { + cs, err := sqlutil.ParseFileURI(dataSourceName) + if err != nil { + return nil, err + } + if result.db, err = sqlutil.Open(internal.SQLiteDriverName(), cs, nil); err != nil { return nil, err } if err = result.prepare(); err != nil { diff --git a/federationsender/storage/storage_wasm.go b/federationsender/storage/storage_wasm.go index 3d071bfef..f593fd448 100644 --- a/federationsender/storage/storage_wasm.go +++ b/federationsender/storage/storage_wasm.go @@ -33,7 +33,7 @@ func NewDatabase( } switch uri.Scheme { case "file": - return sqlite3.NewDatabase(uri.Path) + return sqlite3.NewDatabase(dataSourceName) case "postgres": return nil, fmt.Errorf("Cannot use postgres implementation") default: diff --git a/internal/basecomponent/base.go b/internal/basecomponent/base.go index beaf0e86d..42d236f85 100644 --- a/internal/basecomponent/base.go +++ b/internal/basecomponent/base.go @@ -277,12 +277,9 @@ func setupNaffka(cfg *config.Dendrite) (sarama.Consumer, sarama.SyncProducer) { uri, err := url.Parse(string(cfg.Database.Naffka)) if err != nil || uri.Scheme == "file" { var cs string - if uri.Opaque != "" { // file:filename.db - cs = uri.Opaque - } else if uri.Path != "" { // file:///path/to/filename.db - cs = uri.Path - } else { - logrus.Panic("file uri has no filename") + cs, err = sqlutil.ParseFileURI(string(cfg.Database.Naffka)) + if err != nil { + logrus.WithError(err).Panic("Failed to parse naffka database file URI") } db, err = sqlutil.Open(internal.SQLiteDriverName(), cs, nil) if err != nil { diff --git a/internal/sqlutil/uri.go b/internal/sqlutil/uri.go new file mode 100644 index 000000000..f72e02426 --- /dev/null +++ b/internal/sqlutil/uri.go @@ -0,0 +1,24 @@ +package sqlutil + +import ( + "fmt" + "net/url" +) + +// ParseFileURI returns the filepath in the given file: URI. Specifically, this will handle +// both relative (file:foo.db) and absolute (file:///path/to/foo) paths. +func ParseFileURI(dataSourceName string) (string, error) { + uri, err := url.Parse(dataSourceName) + if err != nil { + return "", err + } + var cs string + if uri.Opaque != "" { // file:filename.db + cs = uri.Opaque + } else if uri.Path != "" { // file:///path/to/filename.db + cs = uri.Path + } else { + return "", fmt.Errorf("invalid file uri: %s", dataSourceName) + } + return cs, nil +} diff --git a/mediaapi/storage/sqlite3/storage.go b/mediaapi/storage/sqlite3/storage.go index 6da7132d8..ea81f9120 100644 --- a/mediaapi/storage/sqlite3/storage.go +++ b/mediaapi/storage/sqlite3/storage.go @@ -37,7 +37,11 @@ type Database struct { func Open(dataSourceName string) (*Database, error) { var d Database var err error - if d.db, err = sqlutil.Open(internal.SQLiteDriverName(), dataSourceName, nil); err != nil { + cs, err := sqlutil.ParseFileURI(dataSourceName) + if err != nil { + return nil, err + } + if d.db, err = sqlutil.Open(internal.SQLiteDriverName(), cs, nil); err != nil { return nil, err } if err = d.statements.prepare(d.db); err != nil { diff --git a/mediaapi/storage/storage_wasm.go b/mediaapi/storage/storage_wasm.go index aa188997f..78de2cb82 100644 --- a/mediaapi/storage/storage_wasm.go +++ b/mediaapi/storage/storage_wasm.go @@ -35,7 +35,7 @@ func Open( case "postgres": return nil, fmt.Errorf("Cannot use postgres implementation") case "file": - return sqlite3.Open(uri.Path) + return sqlite3.Open(dataSourceName) default: return nil, fmt.Errorf("Cannot use postgres implementation") } diff --git a/publicroomsapi/storage/sqlite3/storage.go b/publicroomsapi/storage/sqlite3/storage.go index 898101127..f51051823 100644 --- a/publicroomsapi/storage/sqlite3/storage.go +++ b/publicroomsapi/storage/sqlite3/storage.go @@ -42,7 +42,11 @@ type attributeValue interface{} func NewPublicRoomsServerDatabase(dataSourceName string, localServerName gomatrixserverlib.ServerName) (*PublicRoomsServerDatabase, error) { var db *sql.DB var err error - if db, err = sqlutil.Open(internal.SQLiteDriverName(), dataSourceName, nil); err != nil { + cs, err := sqlutil.ParseFileURI(dataSourceName) + if err != nil { + return nil, err + } + if db, err = sqlutil.Open(internal.SQLiteDriverName(), cs, nil); err != nil { return nil, err } storage := PublicRoomsServerDatabase{ diff --git a/publicroomsapi/storage/storage_wasm.go b/publicroomsapi/storage/storage_wasm.go index fa3356c03..70ceeaf85 100644 --- a/publicroomsapi/storage/storage_wasm.go +++ b/publicroomsapi/storage/storage_wasm.go @@ -32,7 +32,7 @@ func NewPublicRoomsServerDatabase(dataSourceName string, localServerName gomatri case "postgres": return nil, fmt.Errorf("Cannot use postgres implementation") case "file": - return sqlite3.NewPublicRoomsServerDatabase(uri.Path, localServerName) + return sqlite3.NewPublicRoomsServerDatabase(dataSourceName, localServerName) default: return nil, fmt.Errorf("Cannot use postgres implementation") } diff --git a/roomserver/storage/sqlite3/storage.go b/roomserver/storage/sqlite3/storage.go index 54a2f2658..5596b5e3c 100644 --- a/roomserver/storage/sqlite3/storage.go +++ b/roomserver/storage/sqlite3/storage.go @@ -18,8 +18,6 @@ package sqlite3 import ( "context" "database/sql" - "errors" - "net/url" "github.com/matrix-org/dendrite/internal/sqlutil" @@ -50,18 +48,10 @@ type Database struct { // nolint: gocyclo func Open(dataSourceName string) (*Database, error) { var d Database - uri, err := url.Parse(dataSourceName) + cs, err := sqlutil.ParseFileURI(dataSourceName) if err != nil { return nil, err } - var cs string - if uri.Opaque != "" { // file:filename.db - cs = uri.Opaque - } else if uri.Path != "" { // file:///path/to/filename.db - cs = uri.Path - } else { - return nil, errors.New("no filename or path in connect string") - } if d.db, err = sqlutil.Open(internal.SQLiteDriverName(), cs, nil); err != nil { return nil, err } diff --git a/serverkeyapi/storage/sqlite3/keydb.go b/serverkeyapi/storage/sqlite3/keydb.go index 1ad6e7e88..e8cb78bd6 100644 --- a/serverkeyapi/storage/sqlite3/keydb.go +++ b/serverkeyapi/storage/sqlite3/keydb.go @@ -17,8 +17,6 @@ package sqlite3 import ( "context" - "errors" - "net/url" "time" "golang.org/x/crypto/ed25519" @@ -46,18 +44,10 @@ func NewDatabase( serverKey ed25519.PublicKey, serverKeyID gomatrixserverlib.KeyID, ) (*Database, error) { - uri, err := url.Parse(dataSourceName) + cs, err := sqlutil.ParseFileURI(dataSourceName) if err != nil { return nil, err } - var cs string - if uri.Opaque != "" { // file:filename.db - cs = uri.Opaque - } else if uri.Path != "" { // file:///path/to/filename.db - cs = uri.Path - } else { - return nil, errors.New("no filename or path in connect string") - } db, err := sqlutil.Open(internal.SQLiteDriverName(), cs, nil) if err != nil { return nil, err diff --git a/syncapi/storage/sqlite3/syncserver.go b/syncapi/storage/sqlite3/syncserver.go index 5ba07617e..38ce5bcf0 100644 --- a/syncapi/storage/sqlite3/syncserver.go +++ b/syncapi/storage/sqlite3/syncserver.go @@ -17,8 +17,6 @@ package sqlite3 import ( "database/sql" - "errors" - "net/url" "github.com/matrix-org/dendrite/internal/sqlutil" @@ -43,18 +41,10 @@ type SyncServerDatasource struct { // nolint: gocyclo func NewDatabase(dataSourceName string) (*SyncServerDatasource, error) { var d SyncServerDatasource - uri, err := url.Parse(dataSourceName) + cs, err := sqlutil.ParseFileURI(dataSourceName) if err != nil { return nil, err } - var cs string - if uri.Opaque != "" { // file:filename.db - cs = uri.Opaque - } else if uri.Path != "" { // file:///path/to/filename.db - cs = uri.Path - } else { - return nil, errors.New("no filename or path in connect string") - } if d.db, err = sqlutil.Open(internal.SQLiteDriverName(), cs, nil); err != nil { return nil, err }