increase tolerance in detecting new songs if songid didn't change
[scrobby.git] / src / misc.cpp
blobc17bc14f2b81a9ae3a1f56412d6521b951f65d99
1 /***************************************************************************
2 * Copyright (C) 2008-2009 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 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include <cerrno>
22 #include <cstdlib>
23 #include <cstring>
24 #include <fstream>
25 #include <iostream>
26 #include <openssl/evp.h>
27 #include <pwd.h>
29 #include "configuration.h"
30 #include "misc.h"
32 size_t write_data(char *buffer, size_t size, size_t nmemb, void *data)
34 size_t result = size * nmemb;
35 static_cast<std::string *>(data)->append(buffer, result);
36 return result;
39 void ChangeToUser()
41 if (Config.dedicated_user.empty() || getuid() != 0)
42 return;
44 passwd *userinfo;
45 if (!(userinfo = getpwnam(Config.dedicated_user.c_str())))
47 std::cerr << "user " << Config.dedicated_user << " not found!\n";
48 exit(1);
50 if (setgid(userinfo->pw_gid) == -1)
52 std::cerr << "cannot set gid for user " << Config.dedicated_user << ": " << strerror(errno) << std::endl;
53 exit(1);
55 if (setuid(userinfo->pw_uid) == -1)
57 std::cerr << "cannot change to uid of user " << Config.dedicated_user << ": " << strerror(errno) << std::endl;
58 exit(1);
60 Config.user_home_folder = userinfo->pw_dir ? userinfo->pw_dir : "";
63 bool Daemonize()
65 if (daemon(0, 0) < 0)
66 return false;
68 std::ofstream f(Config.file_pid.c_str(), std::ios_base::trunc);
69 if (f.is_open())
71 pid_t pid = getpid();
72 f << pid;
73 f.close();
74 return true;
76 else
77 return false;
80 void WriteCache(const std::string &s)
82 std::ofstream f(Config.file_cache.c_str(), std::ios::app);
83 if (f.is_open())
85 f << s << std::endl;
86 f.close();
90 void Log(LogLevel ll, const char *format, ...)
92 if (Config.log_level < ll)
93 return;
94 FILE *f = fopen(Config.file_log.c_str(), "a");
95 if (!f)
97 perror("Cannot open log file!\n");
98 exit(1);
100 fprintf(f, "[%s] ", DateTime().c_str());
101 va_list list;
102 va_start(list, format);
103 vfprintf(f, format, list);
104 va_end(list);
105 fprintf(f, "\n");
106 fclose(f);
109 void IgnoreNewlines(std::string &s)
111 for (size_t i = s.find("\n"); i != std::string::npos; i = s.find("\n"))
112 s.replace(i, 1, "");
115 std::string md5sum(const std::string &s)
117 unsigned char md_value[EVP_MAX_MD_SIZE];
118 unsigned int md_len;
120 EVP_MD_CTX mdctx;
121 EVP_DigestInit(&mdctx, EVP_md5());
122 EVP_DigestUpdate(&mdctx, s.c_str(), s.length());
123 EVP_DigestFinal_ex(&mdctx, md_value, &md_len);
124 EVP_MD_CTX_cleanup(&mdctx);
126 char result[md_len*2+1];
127 for (unsigned i = 0; i < md_len; i++)
128 sprintf(&result[i*2], "%02x", md_value[i]);
129 result[md_len*2] = 0;
131 return result;
134 std::string DateTime()
136 static char result[32];
137 time_t raw;
138 tm *t;
139 time(&raw);
140 t = localtime(&raw);
141 result[strftime(result, 31, "%Y/%m/%d %X", t)] = 0;
142 return result;
145 int StrToInt(const std::string &s)
147 return atoi(s.c_str());