Newer
Older
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
92
93
94
*
* All rights reserved.
*
* This file is part of Obarun. It is subject to the license terms in
* the LICENSE file found in the top-level directory of this
* distribution.
* This file may not be copied, modified, propagated, or distributed
* except according to the terms contained in the LICENSE file./
*/
#include <string.h>
#include <unistd.h>
#include <stdio.h>
#include <oblibs/log.h>
#include <oblibs/sastr.h>
#include <skalibs/stralloc.h>
#include <skalibs/djbunix.h>
#include <66/module.h>
#include <66/constants.h>
void regex_rename(stralloc *list, resolve_service_t *res, uint32_t element)
{
log_flow() ;
stralloc sa = STRALLOC_ZERO ;
if (!element)
return ;
if (!sastr_clean_string(&sa, res->sa.s + element))
log_dieu(LOG_EXIT_SYS, "clean string") ;
size_t pos = 0, idx = 0, salen = sa.len ;
char t[sa.len] ;
sastr_to_char(t, &sa) ;
for (; pos < salen ; pos += strlen(t + pos) + 1) {
idx = 0 ;
char *line = t + pos ;
char replace[SS_MAX_PATH] = { 0 } ;
char regex[SS_MAX_PATH] = { 0 } ;
regex_get_replace(replace,line) ;
regex_get_regex(regex,line) ;
FOREACH_SASTR(list, idx) {
sa.len = 0 ;
char *str = list->s + idx ;
size_t len = strlen(str) ;
char dname[len + 1] ;
if (!ob_dirname(dname, str))
log_dieu(LOG_EXIT_SYS, "get dirname of: ", str) ;
if (!sabasename(&sa, str, len))
log_dieu(LOG_EXIT_SYS, "get basename of: ", str) ;
if (!stralloc_0(&sa))
log_die_nomem("stralloc") ;
if (!sastr_replace(&sa, replace, regex))
log_dieu(LOG_EXIT_SYS, "replace: ", replace, " by: ", regex, " in file: ", str) ;
if (!stralloc_0(&sa))
log_die_nomem("stralloc") ;
char new[len + sa.len + 1] ;
auto_strings(new, dname, sa.s) ;
/** do not try to rename the same directory */
if (strcmp(str, new)) {
log_trace("rename: ", str, " to: ", new) ;
if (rename(str, new) == -1)
//log_warnusys( "rename: ", str, " to: ", new) ;
log_dieusys(LOG_EXIT_SYS, "rename: ", str, " to: ", new) ;
break ;
}
}
}
stralloc_free(&sa) ;
}