// hasWritePerm reports whether the current user has permission to write to the // file with the given info. private static bool hasWritePerm(@string _, os.FileInfo fi) { // Windows has a read-only attribute independent of ACLs, so use that to // determine whether the file is intended to be overwritten. // // Per https://golang.org/pkg/os/#Chmod: // “On Windows, only the 0200 bit (owner writable) of mode is used; it // controls whether the file's read-only attribute is set or cleared.” return(fi.Mode() & 0200L != 0L); }
// hasWritePerm reports whether the current user has permission to write to the // file with the given info. // // Although the root user on most Unix systems can write to files even without // permission, hasWritePerm reports false if no appropriate permission bit is // set even if the current user is root. private static bool hasWritePerm(@string path, os.FileInfo fi) { if (os.Getuid() == 0L) { // The root user can access any file, but we still want to default to // read-only mode if the go.mod file is marked as globally non-writable. // (If the user really intends not to be in readonly mode, they can // pass -mod=mod explicitly.) return(fi.Mode() & 0222L != 0L); } const ulong W_OK = (ulong)0x2UL; return(syscall.Access(path, W_OK) == null); }
// FileInfoHeader creates a partially-populated FileHeader from an // os.FileInfo. // Because os.FileInfo's Name method returns only the base name of // the file it describes, it may be necessary to modify the Name field // of the returned header to provide the full path name of the file. // If compression is desired, callers should set the FileHeader.Method // field; it is unset by default. public static (ref FileHeader, error) FileInfoHeader(os.FileInfo fi) { var size = fi.Size(); FileHeader fh = ref new FileHeader(Name: fi.Name(), UncompressedSize64: uint64(size), ); fh.SetModTime(fi.ModTime()); fh.SetMode(fi.Mode()); if (fh.UncompressedSize64 > uint32max) { fh.UncompressedSize = uint32max; } else { fh.UncompressedSize = uint32(fh.UncompressedSize64); } return(fh, null); }