github.com/cyverse/go-irodsclient@v0.13.2/irods/types/file.go (about)

     1  package types
     2  
     3  import (
     4  	log "github.com/sirupsen/logrus"
     5  )
     6  
     7  // Whence determines where to start counting the offset
     8  type Whence int
     9  
    10  const (
    11  	// SeekSet means offset starts from file start
    12  	SeekSet Whence = 0
    13  	// SeekCur means offset starts from current offset
    14  	SeekCur Whence = 1
    15  	// SeekEnd means offset starts from file end
    16  	SeekEnd Whence = 2
    17  )
    18  
    19  // FileOpenMode determines file open mode
    20  type FileOpenMode string
    21  
    22  // FileOpenFlag is internally used value, derived from FileOpenMode
    23  type FileOpenFlag int
    24  
    25  const (
    26  	// FileOpenModeReadOnly is for read
    27  	FileOpenModeReadOnly FileOpenMode = "r"
    28  	// FileOpenModeReadWrite is for read and write
    29  	FileOpenModeReadWrite FileOpenMode = "r+"
    30  	// FileOpenModeWriteOnly is for write
    31  	FileOpenModeWriteOnly FileOpenMode = "w"
    32  	// FileOpenModeWriteTruncate is for write, but truncates the file
    33  	FileOpenModeWriteTruncate FileOpenMode = "w+"
    34  	// FileOpenModeAppend is for write, not trucate, but appends from the file end
    35  	FileOpenModeAppend FileOpenMode = "a"
    36  	// FileOpenModeReadAppend is for read and write, but appends from the file end
    37  	FileOpenModeReadAppend FileOpenMode = "a+"
    38  )
    39  
    40  const (
    41  	// O_RDONLY is for read
    42  	O_RDONLY FileOpenFlag = 0
    43  	// O_WRONLY is for write
    44  	O_WRONLY FileOpenFlag = 1
    45  	// O_RDWR is for read and write
    46  	O_RDWR FileOpenFlag = 2
    47  	// O_APPEND is for append (moving the file pointer to the file end)
    48  	O_APPEND FileOpenFlag = 1024
    49  	// O_CREAT is for creating a file if not exists
    50  	O_CREAT FileOpenFlag = 64
    51  	// O_EXCL ...
    52  	O_EXCL FileOpenFlag = 128
    53  	// O_TRUNC is for truncating content
    54  	O_TRUNC FileOpenFlag = 512
    55  )
    56  
    57  // GetFlag returns file open flag
    58  func (mode FileOpenMode) GetFlag() int {
    59  	flag, _ := mode.GetFlagSeekToEnd()
    60  	return flag
    61  }
    62  
    63  // SeekToEnd returns if the mode needs seeking to end
    64  func (mode FileOpenMode) SeekToEnd() bool {
    65  	_, seekToEnd := mode.GetFlagSeekToEnd()
    66  	return seekToEnd
    67  }
    68  
    69  // GetFlagSeekToEnd returns file open flag and returns true if file pointer moves to the file end
    70  func (mode FileOpenMode) GetFlagSeekToEnd() (int, bool) {
    71  	logger := log.WithFields(log.Fields{
    72  		"package":  "types",
    73  		"function": "GetFileOpenFlagSeekToEnd",
    74  	})
    75  
    76  	switch mode {
    77  	case FileOpenModeReadOnly:
    78  		return int(O_RDONLY), false
    79  	case FileOpenModeReadWrite:
    80  		return int(O_RDWR), false
    81  	case FileOpenModeWriteOnly:
    82  		return int(O_WRONLY) | int(O_CREAT), false
    83  	case FileOpenModeWriteTruncate:
    84  		return int(O_RDWR) | int(O_CREAT) | int(O_TRUNC), false
    85  	case FileOpenModeAppend:
    86  		return int(O_WRONLY) | int(O_CREAT), true
    87  	case FileOpenModeReadAppend:
    88  		return int(O_RDWR) | int(O_CREAT), true
    89  	default:
    90  		logger.Errorf("Unhandled file open mode %s", mode)
    91  		return -1, false
    92  	}
    93  }
    94  
    95  // IsRead returns true if the file open mode is for read
    96  func (mode FileOpenMode) IsRead() bool {
    97  	switch mode {
    98  	case FileOpenModeReadOnly, FileOpenModeReadWrite, FileOpenModeReadAppend:
    99  		return true
   100  	default:
   101  		return false
   102  	}
   103  }
   104  
   105  // IsReadOnly returns true if the file open mode is for read-only
   106  func (mode FileOpenMode) IsReadOnly() bool {
   107  	return mode == FileOpenModeReadOnly
   108  }
   109  
   110  // IsWrite returns true if the file open mode is for write
   111  func (mode FileOpenMode) IsWrite() bool {
   112  	switch mode {
   113  	case FileOpenModeReadWrite, FileOpenModeWriteOnly, FileOpenModeWriteTruncate, FileOpenModeAppend, FileOpenModeReadAppend:
   114  		return true
   115  	default:
   116  		return false
   117  	}
   118  }
   119  
   120  // IsWriteOnly returns true if the file open mode is for write-only
   121  func (mode FileOpenMode) IsWriteOnly() bool {
   122  	switch mode {
   123  	case FileOpenModeWriteOnly, FileOpenModeWriteTruncate, FileOpenModeAppend:
   124  		return true
   125  	default:
   126  		return false
   127  	}
   128  }
   129  
   130  // IsOpeningExisting returns true if the file open mode is for opening existing file
   131  func (mode FileOpenMode) IsOpeningExisting() bool {
   132  	switch mode {
   133  	case FileOpenModeReadOnly, FileOpenModeReadWrite, FileOpenModeReadAppend, FileOpenModeAppend:
   134  		return true
   135  	default:
   136  		return false
   137  	}
   138  }