fileutils.cpp 3.44 KB
Newer Older
1
/*
2
 *  Copyright (C) 2011-2012 Savoir-Faire Linux Inc.
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
 *  Author: Rafaël Carré <rafael.carre@savoirfairelinux.com>
 *
 *  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 3 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, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  Additional permission under GNU GPL version 3 section 7:
 *
 *  If you modify this program, or any covered work, by linking or
 *  combining it with the OpenSSL project's OpenSSL library (or a
 *  modified version of that library), containing parts covered by the
 *  terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
 *  grants you additional permission to convey the resulting work.
 *  Corresponding Source for a non-source form of such a combination
 *  shall include the source code for the parts of OpenSSL used as well
 *  as that of the covered work.
 */

#include <libgen.h>
32 33
#include <dirent.h>
#include <sys/stat.h>
34
#include <fstream>
35
#include <cstdlib>
36
#include <signal.h>
37 38
#include <string>
#include <sstream>
39
#include <iostream>
Xavier Devlamynck's avatar
Xavier Devlamynck committed
40
#include <unistd.h>
41
#include "fileutils.h"
42

43
namespace fileutils {
44 45 46 47 48
// returns true if directory exists
bool check_dir(const char *path)
{
    DIR *dir = opendir(path);

49
    if (!dir) { // doesn't exist
50 51 52 53 54 55 56 57 58
        if (mkdir(path, 0755) != 0) {   // couldn't create the dir
            perror(path);
            return false;
        }
    } else
        closedir(dir);

    return true;
}
59

60
static char *program_dir = NULL;
61 62 63 64 65 66

void set_program_dir(char *program_path)
{
    program_dir = dirname(program_path);
}

67
const char *get_program_dir()
68 69 70
{
    return program_dir;
}
71

72 73 74 75 76 77 78
//TODO it is faking this, implement proper system 
const char *get_data_dir()
{
    std::string path = std::string(get_program_dir()) + "/../../share/sflphone/ringtones/";
    return path.c_str();
}

79 80
bool create_pidfile()
{
81 82
    std::string xdg_env(XDG_CACHE_HOME);
    std::string path = (not xdg_env.empty()) ? xdg_env : std::string(HOMEDIR) + DIR_SEPARATOR_STR ".cache/";
83 84 85 86 87 88 89 90 91 92

    if (!check_dir(path.c_str()))
        return false;

    path += "sflphone";

    if (!check_dir(path.c_str()))
        return false;

    std::string pidfile = path + "/" PIDFILE;
93
    std::ifstream is(pidfile.c_str());
94

95
    if (is) {
Tristan Matthews's avatar
Tristan Matthews committed
96 97
        // PID file exists. Check if the former process is still alive or
        // not. If alive, give user a hint.
98
        int oldPid;
99
        is >> oldPid;
100 101

        if (kill(oldPid, 0) == 0) {
102 103 104
            // Use cerr because logging has not been initialized
            std::cerr << "There is already a sflphoned daemon running in " <<
                "the system. Starting Failed." << std::endl;
105 106 107 108 109
            return false;
        }
    }

    // write pid file
110
    std::ofstream os(pidfile.c_str());
111

112
    if (!os) {
113 114 115
        perror(pidfile.c_str());
        return false;
    } else {
116
        os << getpid();
117 118 119 120
    }

    return true;
}
121 122 123 124 125

bool isDirectoryWritable(const std::string &directory)
{
    return access(directory.c_str(), W_OK) == 0;
}
126
}