diff options
author | Kegsay <kegan@matrix.org> | 2020-06-04 11:18:08 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-04 11:18:08 +0100 |
commit | e7d1ac84c32a10f8deb7bcfc94531386312179c7 (patch) | |
tree | cfb8176ebcd9cd1087aa1c34f6f362dd2851dcdc | |
parent | d4f9a4bb97c0c66963d7fd9c1395614a2198112e (diff) |
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
-rw-r--r-- | appservice/storage/sqlite3/storage.go | 6 | ||||
-rw-r--r-- | appservice/storage/storage_wasm.go | 2 | ||||
-rw-r--r-- | clientapi/auth/storage/accounts/sqlite3/storage.go | 6 | ||||
-rw-r--r-- | clientapi/auth/storage/accounts/storage_wasm.go | 2 | ||||
-rw-r--r-- | clientapi/auth/storage/devices/sqlite3/storage.go | 6 | ||||
-rw-r--r-- | clientapi/auth/storage/devices/storage_wasm.go | 2 | ||||
-rw-r--r-- | federationsender/storage/sqlite3/storage.go | 6 | ||||
-rw-r--r-- | federationsender/storage/storage_wasm.go | 2 | ||||
-rw-r--r-- | internal/basecomponent/base.go | 9 | ||||
-rw-r--r-- | internal/sqlutil/uri.go | 24 | ||||
-rw-r--r-- | mediaapi/storage/sqlite3/storage.go | 6 | ||||
-rw-r--r-- | mediaapi/storage/storage_wasm.go | 2 | ||||
-rw-r--r-- | publicroomsapi/storage/sqlite3/storage.go | 6 | ||||
-rw-r--r-- | publicroomsapi/storage/storage_wasm.go | 2 | ||||
-rw-r--r-- | roomserver/storage/sqlite3/storage.go | 12 | ||||
-rw-r--r-- | serverkeyapi/storage/sqlite3/keydb.go | 12 | ||||
-rw-r--r-- | syncapi/storage/sqlite3/syncserver.go | 12 |
17 files changed, 66 insertions, 51 deletions
diff --git a/appservice/storage/sqlite3/storage.go b/appservice/storage/sqlite3/storage.go index 275c6652..2238b3ff 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 de1acf92..a6144b43 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 5233001f..31426e47 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 7cf50de7..81e77cf7 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 5cd89451..e05a53b4 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 c89ad887..14c19e74 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 dc60ebb0..8699fc19 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 3d071bfe..f593fd44 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 beaf0e86..42d236f8 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 00000000..f72e0242 --- /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 6da7132d..ea81f912 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 aa188997..78de2cb8 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 89810112..f5105182 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 fa3356c0..70ceeaf8 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 54a2f265..5596b5e3 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 1ad6e7e8..e8cb78bd 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 5ba07617..38ce5bcf 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 } |