<commit_before>/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
// vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:

#include "file_description.h"
#include "real_syscalls.h"
#include "backup_debug.h"

#include "assert.h"
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>

///////////////////////////////////////////////////////////////////////////////
//
// file_description() -
//
// Description: 
//
//     ...
//
file_description::file_description()
: m_refcount(1), m_offset(0), m_fd_in_dest_space(-1), m_name(NULL)
{}

///////////////////////////////////////////////////////////////////////////////
//
// print() -
//
// Description: 
//
//     Usefull print out of file description state.
//
void file_description::print() {
    printf("======================\n");
    printf("file_description: %s, @%p\n", m_name, this);
    printf("refcount:%d, offset:%ld, fd_in_dest_space:%d\n",
            this->m_refcount, this->m_offset, this->m_fd_in_dest_space);
    printf("======================\n");
}

///////////////////////////////////////////////////////////////////////////////
//
void file_description::set_name(char *name)
{
    m_name = name;
}

///////////////////////////////////////////////////////////////////////////////
//
// open() -
//
// Description: 
//
//     Calls the operating system's open() syscall for the current
// file description.  This also sets the file descriptor in the 
// destination/backup space for the backup copy of the original file.
//
// Notes:
//
//     Open assumes that the backup file exists.  Create assumes the 
// backup file does NOT exist.
//
void file_description::open()
{
    int fd = 0;
    fd = call_real_open(m_name, O_WRONLY, 0777);
    if (fd < 0) {
        int error = errno;
        assert(error == ENOENT);
        this->create();
    } else {
        this->m_fd_in_dest_space = fd;
    }
}

///////////////////////////////////////////////////////////////////////////////
//
// create() -
//
// Description: 
//
//     Calls the operating system's open() syscall with the create
// flag for the current file description.  This also sets the file 
// descriptor in the destination/backup space for the backup
// copy of the original file.
//
// Notes:
//
//     Open assumes that the backup file exists.  Create assumes the 
// backup file does NOT exist.
//
void file_description::create()
{
    // Create file that was just opened, this assumes the parent directories
    // exist.
    int fd = 0;
    fd = call_real_open(m_name, O_CREAT | O_WRONLY, 0777);
    if (fd < 0) {
        int error = errno;
        assert(error == EEXIST);
        fd = call_real_open(m_name, O_WRONLY, 0777);
        if (fd < 0) {
            perror("BACKUP: Couldn't open backup copy of recently opened file.");<commit_msg>refs #5599 Adding one trace for bug.<commit_after>/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
// vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:

#include "file_description.h"
#include "real_syscalls.h"
#include "backup_debug.h"

#include "assert.h"
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>

///////////////////////////////////////////////////////////////////////////////
//
// file_description() -
//
// Description: 
//
//     ...
//
file_description::file_description()
: m_refcount(1), m_offset(0), m_fd_in_dest_space(-1), m_name(NULL)
{}

///////////////////////////////////////////////////////////////////////////////
//
// print() -
//
// Description: 
//
//     Usefull print out of file description state.
//
void file_description::print() {
    printf("======================\n");
    printf("file_description: %s, @%p\n", m_name, this);
    printf("refcount:%d, offset:%ld, fd_in_dest_space:%d\n",
            this->m_refcount, this->m_offset, this->m_fd_in_dest_space);
    printf("======================\n");
}

///////////////////////////////////////////////////////////////////////////////
//
void file_description::set_name(char *name)
{
    m_name = name;
}

///////////////////////////////////////////////////////////////////////////////
//
// open() -
//
// Description: 
//
//     Calls the operating system's open() syscall for the current
// file description.  This also sets the file descriptor in the 
// destination/backup space for the backup copy of the original file.
//
// Notes:
//
//     Open assumes that the backup file exists.  Create assumes the 
// backup file does NOT exist.
//
void file_description::open()
{
    int fd = 0;
    fd = call_real_open(m_name, O_WRONLY, 0777);
    if (fd < 0) {
        int error = errno;
        assert(error == ENOENT);
        this->create();
    } else {
        this->m_fd_in_dest_space = fd;
    }
}

///////////////////////////////////////////////////////////////////////////////
//
// create() -
//
// Description: 
//
//     Calls the operating system's open() syscall with the create
// flag for the current file description.  This also sets the file 
// descriptor in the destination/backup space for the backup
// copy of the original file.
//
// Notes:
//
//     Open assumes that the backup file exists.  Create assumes the 
// backup file does NOT exist.
//
void file_description::create()
{
    // Create file that was just opened, this assumes the parent directories
    // exist.
    int fd = 0;
    fd = call_real_open(m_name, O_CREAT | O_WRONLY, 0777);
    if (fd < 0) {
        int error = errno;
        perror("[UhOh]: <CAPTURE> ");
        assert(error == EEXIST);
        fd = call_real_open(m_name, O_WRONLY, 0777);
        if (fd < 0) {
            perror("BACKUP: Couldn't open backup copy of recently opened file.");
{'fraction_non_alphanumeric': 0.28351686768047546, 'fraction_numerical': 0.009089320048942492, 'mean_word_length': 1.7105637138796779, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 24, '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': '467309', 'n_tokens_mistral': 1728, 'n_tokens_neox': 1659, 'n_words': 463}