Libgourou

Libgourou Git Source Tree

Root/utils/utils_common.cpp

1/*
2 Copyright (c) 2022, Grégory Soutadé
3
4 All rights reserved.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 * Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13 * Neither the name of the copyright holder nor the
14 names of its contributors may be used to endorse or promote products
15 derived from this software without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
18 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
21 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27*/
28
29#include <sys/types.h>
30#include <sys/stat.h>
31#include <unistd.h>
32#include <string.h>
33#include <stdio.h>
34#include <fcntl.h>
35#include <limits.h>
36
37#include <iostream>
38
39#include <libgourou.h>
40#include <libgourou_common.h>
41#include "utils_common.h"
42
43static const char* defaultDirs[] = {
44 ".adept/",
45 "./adobe-digital-editions/",
46 "./.adobe-digital-editions/"
47};
48
49void version(void)
50{
51 std::cout << "Current libgourou version : " << gourou::DRMProcessor::VERSION << std::endl ;
52}
53
54bool fileExists(const char* filename)
55{
56 struct stat _stat;
57 int ret = stat(filename, &_stat);
58
59 return (ret == 0);
60}
61
62const char* findFile(const char* filename, bool inDefaultDirs)
63{
64 std::string path;
65
66 const char* adeptDir = getenv("ADEPT_DIR");
67 if (adeptDir && adeptDir[0])
68 {
69path = adeptDir + std::string("/") + filename;
70if (fileExists(path.c_str()))
71 return strdup(path.c_str());
72 }
73
74 path = gourou::DRMProcessor::getDefaultAdeptDir() + filename;
75 if (fileExists(path.c_str()))
76return strdup(path.c_str());
77
78 if (fileExists(filename))
79return strdup(filename);
80
81 if (!inDefaultDirs) return 0;
82
83 for (int i=0; i<(int)ARRAY_SIZE(defaultDirs); i++)
84 {
85path = std::string(defaultDirs[i]) + filename;
86if (fileExists(path.c_str()))
87 return strdup(path.c_str());
88 }
89
90 return 0;
91}
92
93// https://stackoverflow.com/questions/2336242/recursive-mkdir-system-call-on-unix
94void mkpath(const char *dir)
95{
96 char tmp[PATH_MAX];
97 char *p = NULL;
98 size_t len;
99
100 snprintf(tmp, sizeof(tmp),"%s",dir);
101 len = strlen(tmp);
102 if (tmp[len - 1] == '/')
103 tmp[len - 1] = 0;
104 for (p = tmp + 1; *p; p++)
105 if (*p == '/') {
106 *p = 0;
107 mkdir(tmp, S_IRWXU);
108 *p = '/';
109 }
110 mkdir(tmp, S_IRWXU);
111}
112
113void fileCopy(const char* in, const char* out)
114{
115 char buffer[4096], *_buffer;
116 int ret, ret2, fdIn, fdOut;
117
118 fdIn = open(in, O_RDONLY);
119
120 if (!fdIn)
121EXCEPTION(gourou::CLIENT_FILE_ERROR, "Unable to open " << in);
122
123 fdOut = gourou::createNewFile(out);
124
125 if (!fdOut)
126 {
127close (fdIn);
128EXCEPTION(gourou::CLIENT_FILE_ERROR, "Unable to open " << out);
129 }
130
131 while (true)
132 {
133ret = ::read(fdIn, buffer, sizeof(buffer));
134if (ret <= 0)
135 break;
136do
137{
138 _buffer = buffer;
139 ret2 = ::write(fdOut, _buffer, ret);
140 if (ret2 >= 0)
141 {
142ret -= ret2;
143_buffer += ret2;
144 }
145 else
146 {
147EXCEPTION(gourou::CLIENT_FILE_ERROR, "Error writing " << out);
148 }
149} while (ret);
150 }
151
152 close (fdIn);
153 close (fdOut);
154}

Archive Download this file