summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorTero Koskinen <tero.koskinen@iki.fi>2020-07-25 11:11:13 +0300
committerReto Brunner <reto@labrat.space>2020-07-27 18:26:57 +0200
commitf81cc2803cca4a5213a9d4514ddae8417c23f8ee (patch)
tree6b3c3c2264b267106bd922ee9734c248cc933934
parentc574a838fa89bf46bf7188442f400b206b04df95 (diff)
downloadaerc-f81cc2803cca4a5213a9d4514ddae8417c23f8ee.tar.gz
maildir: Provide nicer error message on invalid url
If accounts.conf contains an invalid maildir url, return a nice
error instead of panicking.

Log a couple of different error cases to provide extra
information about the error to the user.
-rw-r--r--worker/maildir/container.go21
-rw-r--r--worker/maildir/worker.go17
2 files changed, 34 insertions, 4 deletions
diff --git a/worker/maildir/container.go b/worker/maildir/container.go
index cd9a447..14815c9 100644
--- a/worker/maildir/container.go
+++ b/worker/maildir/container.go
@@ -21,15 +21,30 @@ type Container struct {
 }
 
 // NewContainer creates a new container at the specified directory
-// TODO: return an error if the provided directory is not accessible
-func NewContainer(dir string, l *log.Logger) *Container {
-	return &Container{dir: dir, uids: uidstore.NewStore(), log: l}
+func NewContainer(dir string, l *log.Logger) (*Container, error) {
+	f, err := os.Open(dir)
+	if err != nil {
+		return nil, err
+	}
+	defer f.Close()
+	s, err := f.Stat()
+	if err != nil {
+		return nil, err
+	}
+	if !s.IsDir() {
+		return nil, fmt.Errorf("Given maildir '%s' not a directory", dir)
+	}
+	return &Container{dir: dir, uids: uidstore.NewStore(), log: l}, nil
 }
 
 // ListFolders returns a list of maildir folders in the container
 func (c *Container) ListFolders() ([]string, error) {
 	folders := []string{}
 	err := filepath.Walk(c.dir, func(path string, info os.FileInfo, err error) error {
+		if err != nil {
+			return fmt.Errorf("Invalid path '%s': error: %v", path, err)
+
+		}
 		if !info.IsDir() {
 			return nil
 		}
diff --git a/worker/maildir/worker.go b/worker/maildir/worker.go
index ce548ff..d1ff3c2 100644
--- a/worker/maildir/worker.go
+++ b/worker/maildir/worker.go
@@ -1,6 +1,7 @@
 package maildir
 
 import (
+	"errors"
 	"fmt"
 	"io"
 	"net/url"
@@ -221,7 +222,15 @@ func (w *Worker) handleConfigure(msg *types.Configure) error {
 		}
 		dir = filepath.Join(home, u.Path)
 	}
-	w.c = NewContainer(dir, w.worker.Logger)
+	if len(dir) == 0 {
+		return fmt.Errorf("could not resolve maildir from URL '%s'", msg.Config.Source)
+	}
+	c, err := NewContainer(dir, w.worker.Logger)
+	if err != nil {
+		w.worker.Logger.Printf("could not configure maildir: %s", dir)
+		return err
+	}
+	w.c = c
 	w.worker.Logger.Printf("configured base maildir: %s", dir)
 	return nil
 }
@@ -231,6 +240,12 @@ func (w *Worker) handleConnect(msg *types.Connect) error {
 }
 
 func (w *Worker) handleListDirectories(msg *types.ListDirectories) error {
+	// TODO If handleConfigure has returned error, w.c is nil.
+	// It could be better if we skip directory listing completely
+	// when configure fails.
+	if w.c == nil {
+		return errors.New("Incorrect maildir directory")
+	}
 	dirs, err := w.c.ListFolders()
 	if err != nil {
 		w.worker.Logger.Printf("error listing directories: %v", err)
42 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499