github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/timeutil/executable.go (about)

     1  // Copyright 2023 The GitBundle Inc. All rights reserved.
     2  // Copyright 2017 The Gitea Authors. All rights reserved.
     3  // Use of this source code is governed by a MIT-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package timeutil
     7  
     8  import (
     9  	"os"
    10  	"path/filepath"
    11  	"sync"
    12  	"time"
    13  
    14  	"github.com/gitbundle/modules/log"
    15  )
    16  
    17  var (
    18  	executablModTime     = time.Now()
    19  	executablModTimeOnce sync.Once
    20  )
    21  
    22  // GetExecutableModTime get executable file modified time of current process.
    23  func GetExecutableModTime() time.Time {
    24  	executablModTimeOnce.Do(func() {
    25  		exePath, err := os.Executable()
    26  		if err != nil {
    27  			log.Error("os.Executable: %v", err)
    28  			return
    29  		}
    30  
    31  		exePath, err = filepath.Abs(exePath)
    32  		if err != nil {
    33  			log.Error("filepath.Abs: %v", err)
    34  			return
    35  		}
    36  
    37  		exePath, err = filepath.EvalSymlinks(exePath)
    38  		if err != nil {
    39  			log.Error("filepath.EvalSymlinks: %v", err)
    40  			return
    41  		}
    42  
    43  		st, err := os.Stat(exePath)
    44  		if err != nil {
    45  			log.Error("os.Stat: %v", err)
    46  			return
    47  		}
    48  
    49  		executablModTime = st.ModTime()
    50  	})
    51  	return executablModTime
    52  }