Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 221 lines (191 sloc) 6.499 kb
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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
/*
* Amanda, The Advanced Maryland Automatic Network Disk Archiver
* Copyright (c) 1991-1998 University of Maryland at College Park
* All Rights Reserved.
*
* Permission to use, copy, modify, distribute, and sell this software and its
* documentation for any purpose is hereby granted without fee, provided that
* the above copyright notice appear in all copies and that both that
* copyright notice and this permission notice appear in supporting
* documentation, and that the name of U.M. not be used in advertising or
* publicity pertaining to distribution of the software without specific,
* written prior permission. U.M. makes no representations about the
* suitability of this software for any purpose. It is provided "as is"
* without express or implied warranty.
*
* U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
* BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* Authors: the Amanda Development Team. Its members are listed in a
* file named AUTHORS, in the root directory of this distribution.
*/
/*
* $Id: amtrmlog.c,v 1.17 2006/07/25 18:27:57 martinea Exp $
*
* trims number of index files to only those still in system. Well
* actually, it keeps a few extra, plus goes back to the last level 0
* dump.
*/

#include "amanda.h"
#include "conffile.h"
#include "diskfile.h"
#include "tapefile.h"
#include "find.h"

int amtrmidx_debug = 0;

int main(int argc, char **argv);

int
main(
    int argc,
    char ** argv)
{
    disklist_t diskl;
    int no_keep; /* files per system to keep */
    char **output_find_log;
    DIR *dir;
    struct dirent *adir;
    char **name;
    int useful;
    char *olddir;
    char *oldfile = NULL, *newfile = NULL;
    time_t today, date_keep;
    char *logname = NULL;
    struct stat stat_log;
    struct stat stat_old;
    char *conf_diskfile;
    char *conf_tapelist;
    char *conf_logdir;
    int dumpcycle;
    config_overrides_t *cfg_ovr = NULL;

    if (argc > 1 && argv && argv[1] && g_str_equal(argv[1], "--version")) {
printf("amtrmlog-%s\n", VERSION);
return (0);
    }

    /*
* Configure program for internationalization:
* 1) Only set the message locale for now.
* 2) Set textdomain for all amanda related programs to "amanda"
* We don't want to be forced to support dozens of message catalogs.
*/
    setlocale(LC_MESSAGES, "C");
    textdomain("amanda");

    safe_fd(-1, 0);
    safe_cd();

    set_pname("amtrmlog");

    /* Don't die when child closes pipe */
    signal(SIGPIPE, SIG_IGN);

    cfg_ovr = extract_commandline_config_overrides(&argc, &argv);

    if (argc > 1 && g_str_equal(argv[1], "-t")) {
amtrmidx_debug = 1;
argc--;
argv++;
    }

    if (argc < 2) {
g_fprintf(stderr, _("Usage: %s [-t] <config> [-o configoption]*\n"), argv[0]);
return 1;
    }

    dbopen(DBG_SUBDIR_SERVER);
    dbprintf(_("%s: version %s\n"), argv[0], VERSION);

    set_config_overrides(cfg_ovr);
    config_init(CONFIG_INIT_EXPLICIT_NAME, argv[1]);

    conf_diskfile = config_dir_relative(getconf_str(CNF_DISKFILE));
    read_diskfile(conf_diskfile, &diskl);
    amfree(conf_diskfile);

    if (config_errors(NULL) >= CFGERR_WARNINGS) {
config_print_errors();
if (config_errors(NULL) >= CFGERR_ERRORS) {
g_critical(_("errors processing config file"));
}
    }

    check_running_as(RUNNING_AS_DUMPUSER);

    dbrename(get_config_name(), DBG_SUBDIR_SERVER);

    conf_tapelist = config_dir_relative(getconf_str(CNF_TAPELIST));
    if (read_tapelist(conf_tapelist)) {
error(_("could not load tapelist \"%s\""), conf_tapelist);
/*NOTREACHED*/
    }
    amfree(conf_tapelist);

    today = time((time_t *)NULL);
    dumpcycle = getconf_int(CNF_DUMPCYCLE);
    if(dumpcycle > 5000)
dumpcycle = 5000;
    date_keep = today - (dumpcycle * 86400);

    output_find_log = find_log();

    /* determine how many log to keep */
    no_keep = getconf_int(CNF_TAPECYCLE) * 2;
    dbprintf(plural(_("Keeping %d log file\n"),
_("Keeping %d log files\n"), no_keep),
no_keep);

    conf_logdir = config_dir_relative(getconf_str(CNF_LOGDIR));
    olddir = g_strjoin(NULL, conf_logdir, "/oldlog", NULL);
    if (mkpdir(olddir, 0700, (uid_t)-1, (gid_t)-1) != 0) {
error(_("could not create parents of %s: %s"), olddir, strerror(errno));
/*NOTREACHED*/
    }
    if (mkdir(olddir, 0700) != 0 && errno != EEXIST) {
error(_("could not create %s: %s"), olddir, strerror(errno));
/*NOTREACHED*/
    }

    if (stat(olddir,&stat_old) == -1) {
error(_("can't stat oldlog directory \"%s\": %s"), olddir, strerror(errno));
/*NOTREACHED*/
    }

    if (!S_ISDIR(stat_old.st_mode)) {
error(_("Oldlog directory \"%s\" is not a directory"), olddir);
/*NOTREACHED*/
    }

    if ((dir = opendir(conf_logdir)) == NULL) {
error(_("could not open log directory \"%s\": %s"), conf_logdir,strerror(errno));
/*NOTREACHED*/
    }
    while ((adir=readdir(dir)) != NULL) {
if(g_str_has_prefix(adir->d_name, "log.")) {
useful=0;
for (name=output_find_log;*name !=NULL; name++) {
if ((strlen(adir->d_name) >= 13 &&
strlen(*name) >= 13 &&
adir->d_name[12] == '.' && (*name)[12] == '.' &&
strncmp(adir->d_name, *name, 12)) ||
strncmp(adir->d_name, *name, 18)) {
useful=1;
break;
}
}
g_free(logname);
logname = g_strconcat(conf_logdir, "/", adir->d_name, NULL);
if(stat(logname,&stat_log)==0) {
if((time_t)stat_log.st_mtime > date_keep) {
useful = 1;
}
}
if(useful == 0) {
g_free(oldfile);
oldfile = g_strconcat(conf_logdir, "/", adir->d_name, NULL);

g_free(newfile);
newfile = g_strconcat(olddir, "/", adir->d_name, NULL);
if (rename(oldfile,newfile) != 0) {
error(_("could not rename \"%s\" to \"%s\": %s"),
oldfile, newfile, strerror(errno));
/*NOTREACHED*/
}
}
}
    }
    closedir(dir);
    for (name = output_find_log; *name != NULL; name++) {
amfree(*name);
    }
    amfree(output_find_log);
    amfree(logname);
    amfree(oldfile);
    amfree(newfile);
    amfree(olddir);
    amfree(conf_logdir);
    clear_tapelist();
    free_disklist(&diskl);

    dbclose();

    return 0;
}
Something went wrong with that request. Please try again.