Goit

Simple and lightweight Git web server
git clone http://git.omkov.net/Goit
Log | Tree | Refs | README | Download

Goit/src/util/util.go (102 lines, 1.8 KiB) -rw-r--r-- blame download

0123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
// Copyright (C) 2023, Jakob Wakeling
// All rights reserved.

package util

import (
	"cmp"
	"errors"
	"io/fs"
	"net/http"
	"os"
	"path/filepath"
)

const ModeNotRegular = os.ModeSymlink | os.ModeDevice | os.ModeNamedPipe | os.ModeSocket | os.ModeCharDevice |
	os.ModeIrregular

func If[T any](cond bool, a, b T) T {
	if cond {
		return a
	}
	return b
}

func Min[T cmp.Ordered](a, b T) T {
	if a < b {
		return a
	}
	return b
}

func Max[T cmp.Ordered](a, b T) T {
	if a > b {
		return a
	}
	return b
}

/* Return the named cookie or nil if not found or invalid. */
func Cookie(r *http.Request, name string) *http.Cookie {
	c, err := r.Cookie(name)
	if err == nil && c.Valid() == nil {
		return c
	}

	return nil
}

/* Return a slice of form values with the specified key. */
func FormValues(r *http.Request, key string) []string {
	if r.Form == nil {
		r.ParseMultipartForm(32 << 20 /* 32 MB */)
	}

	return r.Form[key]
}

func ModeString(mode uint32) string {
	s := If((mode&0o40000) != 0, "d", "-")
	s += If((mode&0o400) != 0, "r", "-")
	s += If((mode&0o200) != 0, "w", "-")
	s += If((mode&0o100) != 0, "x", "-")
	s += If((mode&0o040) != 0, "r", "-")
	s += If((mode&0o020) != 0, "w", "-")
	s += If((mode&0o010) != 0, "x", "-")
	s += If((mode&0o004) != 0, "r", "-")
	s += If((mode&0o002) != 0, "w", "-")
	s += If((mode&0o001) != 0, "x", "-")
	return s
}

func DirSize(path string) (uint64, error) {
	var size int64

	err := filepath.WalkDir(path, func(_ string, d fs.DirEntry, err error) error {
		if err != nil {
			if errors.Is(err, os.ErrNotExist) {
				return nil
			}

			return err
		}

		if d.IsDir() {
			return nil
		}

		f, err := d.Info()
		if err != nil {
			return err
		}

		if (f.Mode() & ModeNotRegular) == 0 {
			size += f.Size()
		}

		return nil
	})

	return uint64(size), err
}