get rid of threads
[scrobby.git] / src / misc.cpp
blobfd829a14df3c4ae515a9a4a15df041fa0e2b9800
1 /***************************************************************************
2 * Copyright (C) 2008 by Andrzej Rybczak *
3 * electricityispower@gmail.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include <cerrno>
22 #include <cstdlib>
23 #include <cstring>
24 #include <fstream>
25 #include <iostream>
26 #include <sstream>
27 #include <openssl/evp.h>
28 #include <pwd.h>
30 #include "configuration.h"
31 #include "misc.h"
33 extern ScrobbyConfig config;
35 size_t write_data(char *buffer, size_t size, size_t nmemb, std::string data)
37 size_t result = size * nmemb;
38 data.append(buffer, result);
39 return result;
42 void ChangeToUser()
44 if (config.dedicated_user.empty() || getuid() != 0)
45 return;
47 passwd *userinfo;
48 if (!(userinfo = getpwnam(config.dedicated_user.c_str())))
50 std::cerr << "user " << config.dedicated_user << " not found!\n";
51 exit(1);
53 if (setgid(userinfo->pw_gid) == -1)
55 std::cerr << "cannot set gid for user " << config.dedicated_user << ": " << strerror(errno) << std::endl;
56 exit(1);
58 if (setuid(userinfo->pw_uid) == -1)
60 std::cerr << "cannot change to uid of user " << config.dedicated_user << ": " << strerror(errno) << std::endl;
61 exit(1);
63 config.user_home_folder = userinfo->pw_dir ? userinfo->pw_dir : "";
66 bool Daemonize()
68 if (daemon(0, 0) < 0)
69 return false;
71 std::ofstream f(config.file_pid.c_str(), std::ios_base::trunc);
72 if (f.is_open())
74 pid_t pid = getpid();
75 f << pid;
76 f.close();
77 return true;
79 else
80 return false;
83 void ClearCache()
85 std::ofstream f(config.file_cache.c_str(), std::ios::trunc);
86 f.close();
89 void GetCachedSongs(std::vector<std::string> &v)
91 std::ifstream f(config.file_cache.c_str());
92 if (f.is_open())
94 std::string line;
95 while (!f.eof())
97 getline(f, line);
98 if (!line.empty())
99 v.push_back(line);
104 void Cache(const std::string &s)
106 std::ofstream f(config.file_cache.c_str(), std::ios::app);
107 if (f.is_open())
109 f << s << std::endl;
110 f.close();
114 void Log(LogLevel ll, const char *format, ...)
116 if (config.log_level < ll)
117 return;
118 FILE *f = fopen(config.file_log.c_str(), "a");
119 if (!f)
121 perror("Cannot open log file!\n");
122 exit(1);
124 fprintf(f, "[%s] ", DateTime().c_str());
125 va_list list;
126 va_start(list, format);
127 vfprintf(f, format, list);
128 va_end(list);
129 fprintf(f, "\n");
130 fclose(f);
133 void ignore_newlines(std::string &s)
135 for (size_t i = s.find("\n"); i != std::string::npos; i = s.find("\n"))
136 s.replace(i, 1, "");
139 std::string md5sum(const std::string &s)
141 unsigned char md_value[EVP_MAX_MD_SIZE];
142 unsigned int md_len;
144 EVP_MD_CTX mdctx;
145 EVP_DigestInit(&mdctx, EVP_md5());
146 EVP_DigestUpdate(&mdctx, s.c_str(), s.length());
147 EVP_DigestFinal_ex(&mdctx, md_value, &md_len);
148 EVP_MD_CTX_cleanup(&mdctx);
150 char result[md_len*2+1];
151 for (unsigned i = 0; i < md_len; i++)
152 sprintf(&result[i*2], "%02x", md_value[i]);
153 result[md_len*2] = 0;
155 return result;
158 std::string DateTime()
160 char result[32];
161 time_t raw;
162 tm *t;
163 time(&raw);
164 t = localtime(&raw);
165 result[strftime(result, 31, "%Y/%m/%d %X", t)] = 0;
166 return result;
169 int StrToInt(const std::string &s)
171 return atoi(s.c_str());