1 /*
2  * Copyright (c) 2015-2016 Hanspeter Portner (dev@open-music-kontrollers.ch)
3  *
4  * This is free software: you can redistribute it and/or modify
5  * it under the terms of the Artistic License 2.0 as published by
6  * The Perl Foundation.
7  *
8  * This source is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * Artistic License 2.0 for more details.
12  *
13  * You should have received a copy of the Artistic License 2.0
14  * along the source as a COPYING file. If not, obtain it from
15  * http://www.perlfoundation.org/artistic_license_2_0.
16  */
17 
18 #ifndef _SYNTHPOD_COMMON_H
19 #define _SYNTHPOD_COMMON_H
20 
21 #include <stdio.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <err.h>
25 #include <errno.h>
26 #include <string.h>
27 #include <ftw.h>
28 
29 #define SYNTHPOD_PREFIX					"http://open-music-kontrollers.ch/lv2/synthpod#"
30 
31 #define SYNTHPOD_STEREO_URI			SYNTHPOD_PREFIX"stereo"
32 #define SYNTHPOD_COMMON_NK_URI	SYNTHPOD_PREFIX"common_4_nk"
33 #define SYNTHPOD_ROOT_NK_URI	  SYNTHPOD_PREFIX"root_4_nk"
34 
35 #ifdef _WIN32
36 #	define mlock(...)
37 #	define munlock(...)
38 #else
39 #	include <sys/mman.h> // mlock
40 #endif
41 
42 #include <xpress.lv2/xpress.h>
43 
44 #define __realtime __attribute__((annotate("realtime")))
45 #define __non_realtime __attribute__((annotate("non-realtime")))
46 
47 static inline int
mkpath(char * path)48 mkpath(char *path)
49 {
50 	struct stat sb;
51 	char *slash;
52 	bool done = false;
53 
54 	slash = path;
55 
56 	while(!done)
57 	{
58 		slash += strspn(slash, "/");
59 		slash += strcspn(slash, "/");
60 
61 		done = *slash == '\0';
62 		*slash = '\0';
63 
64 		if(stat(path, &sb))
65 		{
66 			if(  (errno != ENOENT)
67 				|| (mkdir(path, 0777) && (errno != EEXIST)) )
68 			{
69 				return -1;
70 			}
71 		}
72 		else if(!S_ISDIR(sb.st_mode))
73 		{
74 			return -1;
75 		}
76 
77 		*slash = '/';
78 	}
79 
80 	return 0;
81 }
82 
83 static inline int
mkpath_const(const char * path)84 mkpath_const(const char *path)
85 {
86 	char *dup = strdup(path);
87 	if(!dup)
88 		return -1;
89 
90 	const int ret = mkpath(dup);
91 	free(dup);
92 	return ret;
93 }
94 
95 static inline int
_unlink_cb(const char * fpath,const struct stat * sb,int typeflag,struct FTW * ftwbuf)96 _unlink_cb(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf)
97 {
98 	return remove(fpath);
99 }
100 
101 static inline int
rmrf_const(const char * path)102 rmrf_const(const char *path)
103 {
104 	return nftw(path, _unlink_cb, 64, FTW_DEPTH | FTW_PHYS);
105 }
106 
107 #endif // _SYNTHPOD_COMMON_H
108