github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/pkg/file/touch.go (about)

     1  // Copyright 2021 The TrueBlocks Authors. All rights reserved.
     2  // Use of this source code is governed by a license that can
     3  // be found in the LICENSE file.
     4  
     5  package file
     6  
     7  import (
     8  	"fmt"
     9  	"os"
    10  	"time"
    11  )
    12  
    13  func Touch(filename string) bool {
    14  	_, err := os.Stat(filename)
    15  	if os.IsNotExist(err) {
    16  		file, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
    17  		if err != nil {
    18  			fmt.Println(err.Error())
    19  			return false
    20  		}
    21  		defer file.Close()
    22  	} else {
    23  		currentTime := time.Now().Local()
    24  		err = os.Chtimes(filename, currentTime, currentTime)
    25  		if err != nil {
    26  			fmt.Println(err.Error())
    27  			return false
    28  		}
    29  	}
    30  	return true
    31  }