<commit_before>/* leasefile.c - functions for writing the lease file
 * Time-stamp: <2011-04-19 16:22:36 njk>
 *
 * (c) 2011 <NAME> <<EMAIL>>
 *
 * 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 2 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.
 */

#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include <errno.h>
#include <limits.h>
#include "log.h"
#include "strl.h"
#include "io.h"
#include "defines.h"

static char leasefile[PATH_MAX] = "\0";
static int leasefilefd = -1;

void set_leasefile(char *lf)
{
    strlcpy(leasefile, lf, sizeof leasefile);
}

void open_leasefile()
{
    if (strlen(leasefile) > 0) {
        leasefilefd = open(leasefile, O_WRONLY|O_TRUNC|O_CREAT, 0644);
        if (leasefilefd < 0) {
            log_line("Failed to create lease file (%s)\n", leasefile);
            exit(EXIT_FAILURE);
        }
    }
}

void write_leasefile(struct in_addr ipnum)
{
    char ip[INET_ADDRSTRLEN+2];
    int ret;
    if (leasefilefd < 0)
        return;
    inet_ntop(AF_INET, &ipnum, ip, sizeof ip);
    strlcat(ip, "\n", sizeof ip);
  retry_trunc:
    ret = ftruncate(leasefilefd, 0);
    switch (ret) {
        default: break;
        case -1:
            if (errno == EINTR)
                goto retry_trunc;
            log_warning("Failed to truncate lease file.\n");
            return;
    }
    ret = safe_write(leasefilefd, ip, strlen(ip));
    if (ret == -1)
        log_warning("Failed to write ip to lease file.\n");<commit_msg>lseek() after ftruncate() just to be safe.<commit_after>/* leasefile.c - functions for writing the lease file
 * Time-stamp: <2011-04-25 01:02:26 njk>
 *
 * (c) 2011 <NAME> <<EMAIL>>
 *
 * 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 2 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.
 */

#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include <errno.h>
#include <limits.h>
#include "log.h"
#include "strl.h"
#include "io.h"
#include "defines.h"

static char leasefile[PATH_MAX] = "\0";
static int leasefilefd = -1;

void set_leasefile(char *lf)
{
    strlcpy(leasefile, lf, sizeof leasefile);
}

void open_leasefile()
{
    if (strlen(leasefile) > 0) {
        leasefilefd = open(leasefile, O_WRONLY|O_TRUNC|O_CREAT, 0644);
        if (leasefilefd < 0) {
            log_line("Failed to create lease file (%s)\n", leasefile);
            exit(EXIT_FAILURE);
        }
    }
}

void write_leasefile(struct in_addr ipnum)
{
    char ip[INET_ADDRSTRLEN+2];
    int ret;
    if (leasefilefd < 0)
        return;
    inet_ntop(AF_INET, &ipnum, ip, sizeof ip);
    strlcat(ip, "\n", sizeof ip);
  retry_trunc:
    ret = ftruncate(leasefilefd, 0);
    switch (ret) {
        default: break;
        case -1:
            if (errno == EINTR)
                goto retry_trunc;
            log_warning("Failed to truncate lease file.\n");
            return;
    }
    lseek(leasefilefd, 0, SEEK_SET);
    ret = safe_write(leasefilefd, ip, strlen(ip));
    if (ret == -1)
        log_warning("Failed to write ip to lease file.\n");
{'fraction_non_alphanumeric': 0.11639492753623189, 'fraction_numerical': 0.018342391304347828, 'mean_word_length': 3.0709677419354837, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2832095', 'n_tokens_mistral': 1517, 'n_tokens_neox': 1449, 'n_words': 500}