fileutils.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2020 Alexey Edelev <semlanik@gmail.com>
  5. *
  6. * This file is part of gostfix project https://git.semlanik.org/semlanik/gostfix
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy of this
  9. * software and associated documentation files (the "Software"), to deal in the Software
  10. * without restriction, including without limitation the rights to use, copy, modify,
  11. * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
  12. * to permit persons to whom the Software is furnished to do so, subject to the following
  13. * conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all copies
  16. * or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  19. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  20. * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  21. * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  22. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. package utils
  26. import (
  27. "os"
  28. unix "golang.org/x/sys/unix"
  29. )
  30. func FileExists(filename string) bool {
  31. info, err := os.Stat(filename)
  32. if os.IsNotExist(err) {
  33. return false
  34. }
  35. return err == nil && !info.IsDir() && info != nil
  36. }
  37. func DirectoryExists(filename string) bool {
  38. info, err := os.Stat(filename)
  39. if os.IsNotExist(err) {
  40. return false
  41. }
  42. return err == nil && info.IsDir() && info != nil
  43. }
  44. type LockedFile struct {
  45. file *os.File
  46. lock *unix.Flock_t
  47. }
  48. func OpenAndLockWait(path string) (file *LockedFile, err error) {
  49. file = &LockedFile{}
  50. file.file, err = os.OpenFile(path, os.O_RDWR, 0)
  51. if err != nil {
  52. return nil, err
  53. }
  54. file.lock = &unix.Flock_t{
  55. Type: unix.F_WRLCK,
  56. }
  57. err = unix.FcntlFlock(file.file.Fd(), unix.F_SETLKW, file.lock)
  58. file.lock.Type = unix.F_UNLCK
  59. if err != nil {
  60. return nil, err
  61. }
  62. return
  63. }
  64. func (f *LockedFile) Read(p []byte) (n int, err error) {
  65. return f.file.Read(p)
  66. }
  67. func (f *LockedFile) Truncate(size int64) error {
  68. return f.file.Truncate(size)
  69. }
  70. func (f *LockedFile) CloseAndUnlock() error {
  71. err1 := unix.FcntlFlock(f.file.Fd(), unix.F_SETLKW, f.lock)
  72. err2 := f.file.Close()
  73. if err1 != nil {
  74. return err1
  75. }
  76. return err2
  77. }