github.com/meulengracht/snapd@v0.0.0-20210719210640-8bde69bcc84e/osutil/chattr.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2016 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package osutil
    21  
    22  import (
    23  	"os"
    24  	"syscall"
    25  	"unsafe"
    26  )
    27  
    28  const (
    29  	// from /usr/include/linux/fs.h
    30  	FS_SECRM_FL        = 0x00000001 /* Secure deletion */
    31  	FS_UNRM_FL         = 0x00000002 /* Undelete */
    32  	FS_COMPR_FL        = 0x00000004 /* Compress file */
    33  	FS_SYNC_FL         = 0x00000008 /* Synchronous updates */
    34  	FS_IMMUTABLE_FL    = 0x00000010 /* Immutable file */
    35  	FS_APPEND_FL       = 0x00000020 /* writes to file may only append */
    36  	FS_NODUMP_FL       = 0x00000040 /* do not dump file */
    37  	FS_NOATIME_FL      = 0x00000080 /* do not update atime */
    38  	FS_DIRTY_FL        = 0x00000100
    39  	FS_COMPRBLK_FL     = 0x00000200 /* One or more compressed clusters */
    40  	FS_NOCOMP_FL       = 0x00000400 /* Don't compress */
    41  	FS_ECOMPR_FL       = 0x00000800 /* Compression error */
    42  	FS_BTREE_FL        = 0x00001000 /* btree format dir */
    43  	FS_INDEX_FL        = 0x00001000 /* hash-indexed directory */
    44  	FS_IMAGIC_FL       = 0x00002000 /* AFS directory */
    45  	FS_JOURNAL_DATA_FL = 0x00004000 /* Reserved for ext3 */
    46  	FS_NOTAIL_FL       = 0x00008000 /* file tail should not be merged */
    47  	FS_DIRSYNC_FL      = 0x00010000 /* dirsync behaviour (directories only) */
    48  	FS_TOPDIR_FL       = 0x00020000 /* Top of directory hierarchies*/
    49  	FS_EXTENT_FL       = 0x00080000 /* Extents */
    50  	FS_DIRECTIO_FL     = 0x00100000 /* Use direct i/o */
    51  	FS_NOCOW_FL        = 0x00800000 /* Do not cow file */
    52  	FS_PROJINHERIT_FL  = 0x20000000 /* Create with parents projid */
    53  	FS_RESERVED_FL     = 0x80000000 /* reserved for ext2 lib */
    54  )
    55  
    56  func ioctl(f *os.File, request uintptr, attrp *int32) error {
    57  	argp := uintptr(unsafe.Pointer(attrp))
    58  	_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, f.Fd(), request, argp)
    59  	if errno != 0 {
    60  		return os.NewSyscallError("ioctl", errno)
    61  	}
    62  
    63  	return nil
    64  }
    65  
    66  // GetAttr retrieves the attributes of a file on a linux filesystem
    67  func GetAttr(f *os.File) (int32, error) {
    68  	attr := int32(-1)
    69  	err := ioctl(f, _FS_IOC_GETFLAGS, &attr)
    70  	return attr, err
    71  }
    72  
    73  // SetAttr sets the attributes of a file on a linux filesystem to the given value
    74  func SetAttr(f *os.File, attr int32) error {
    75  	return ioctl(f, _FS_IOC_SETFLAGS, &attr)
    76  }