summaryrefslogtreecommitdiff
path: root/src/libs6rc/s6rc_livedir_prefixsize.c
blob: d897fe6f34cea7064f7f7f6c64a91b1e34f2868f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
/* ISC license. */

#include <limits.h>
#include <string.h>
#include <sys/stat.h>
#include <errno.h>
#include <s6-rc/s6rc-utils.h>

int s6rc_livedir_prefixsize (char const *live, size_t *n)
{
  struct stat st ;
  size_t llen = strlen(live) ;
  char sfn[llen + 8] ;
  memcpy(sfn, live, llen) ;
  memcpy(sfn + llen, "/prefix", 8) ;
  if (stat(sfn, &st) < 0)
  {
    if (errno != ENOENT) return 0 ;
    *n = 0 ;
    return 1 ;
  }
  if (!S_ISREG(st.st_mode)) return (errno = EINVAL, 0) ;
  if (st.st_size > PATH_MAX) return (errno = ENAMETOOLONG, 0) ;
  *n = st.st_size ;
  return 1 ;
}