-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcommon.c
92 lines (86 loc) · 1.42 KB
/
common.c
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* common.c
* Copyright (C) 2011 KLab Inc.
*/
#define _GNU_SOURCE
#define _FILE_OFFSET_BITS 64
#include "mtn.h"
#include "libmtn.h"
#include "common.h"
void dirbase(const char *path, char *d, char *f)
{
char b[PATH_MAX];
if(d){
strcpy(b, path);
strcpy(d, dirname(b));
}
if(f){
strcpy(b, path);
strcpy(f, basename(b));
}
}
int mkpidfile(char *path)
{
FILE *fd;
if(strlen(path)){
if((fd = fopen(path, "w"))){
fprintf(fd, "%d\n", getpid());
fclose(fd);
}else{
mtnlogger(NULL, 0, "[error] %s: %s %s\n", __func__, strerror(errno), path);
return(-1);
}
}
return(0);
}
int rmpidfile(char *pidfile)
{
if(strlen(pidfile)){
unlink(pidfile);
}
return(0);
}
uint64_t atoikmg(char *str)
{
int unit = 1;
uint64_t val;
char buff[1024];
char *p = buff;
strcpy(buff, str);
while(*p){
if((*p >= '0') && (*p <= '9')){
p++;
continue;
}
switch(*p){
case 'K':
unit = 1024;
break;
case 'M':
unit = 1024 * 1024;
break;
case 'G':
unit = 1024 * 1024 * 1024;
break;
}
*p = 0;
}
val = atoi(buff);
return(val * unit);
}
int mkdir_ex(const char *path)
{
struct stat st;
char d[PATH_MAX];
char f[PATH_MAX];
dirbase(path,d,f);
if(stat(path, &st) == 0){
return(0);
}
if(stat(d, &st) == -1){
if(mkdir_ex(d) == -1){
return(-1);
}
}
return(mkdir(path, 0755));
}