forked from scanoss/wayuu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_utils.c
126 lines (111 loc) · 2.57 KB
/
file_utils.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2018-2020 SCANOSS LTD
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include "log.h"
#include "file_utils.h"
bool find_in_file(char *path, char *needle)
{
if (!is_file(path))
{
return false;
}
bool result = false;
char *haystack = calloc(8192, 1);
read_file(haystack, path, 0);
if (strstr(haystack, needle))
result = true;
free(haystack);
return result;
}
bool check_createdir(char *path)
{
if (!is_dir(path) && mkdir(path, 0755))
{
return false;
}
return true;
}
bool is_dir(char *path)
{
struct stat pstat;
if (!stat(path, &pstat))
if (S_ISDIR(pstat.st_mode))
return true;
return false;
}
bool is_file(char *path)
{
struct stat pstat;
if (!stat(path, &pstat))
if (S_ISREG(pstat.st_mode))
return true;
return false;
}
void read_file(char *out, char *path, uint64_t maxlen)
{
char *src;
uint64_t length = 0;
out[0] = 0;
if (!is_file(path))
{
log_error("File not found: %s", path);
return;
}
FILE *file = fopen(path, "rb");
if (file)
{
fseek(file, 0, SEEK_END);
length = ftell(file);
fseek(file, 0, SEEK_SET);
src = calloc(length, 1);
if (src)
{
fread(src, 1, length, file);
}
fclose(file);
if (maxlen > 0)
if (length > maxlen - 1)
length = maxlen - 1;
memcpy(out, src, length);
out[length] = 0;
free(src);
}
}
void write_file(char *filename, char *ptr, int size)
{
log_debug("write_file(filename=%s, strlen(ptr)=%d, size=%d)", filename, strlen(ptr), size);
if (is_file(filename))
{
remove(filename);
}
FILE *f = fopen(filename, "wb+");
if (f == NULL)
{
log_error("There was an error opening file: %s", filename);
return;
}
size_t written = fwrite(ptr, 1, size, f);
if (written < 1)
{
log_error("There was an error writing file: %s", filename);
}
fclose(f);
}