summaryrefslogblamecommitdiff
path: root/src/common/lock.cpp
blob: 08ec2c427767df448aae9cb668d7919d6bc9109a (plain) (tree)
1
2
3
4
5
6
7
8
9
                   




                   
                       
                     
 

                                                     
 
                          
                                                 
 
             
                      
 
                                                
                        

      

                                                         
     
                
                 
              

 
                                              
                                                           
 
           
     
                    

                                                                      
     
 
#include "lock.hpp"

#include <unistd.h>

#include <cstdio>

#include "cxxstdio.hpp"
#include "socket.hpp"

/// Protected file writing
/// (Until the file is closed, it keeps the old file)

// Start writing a tmpfile
FILE *lock_fopen(const char *filename, int *info)
{
    FILE *fp;
    int no = getpid();

    // Get a filename that doesn't already exist
    std::string newfile;
    do
    {
        newfile = STRPRINTF("%s_%d.tmp", filename, no++);
        fp = fopen_(newfile.c_str(), "wx");
    }
    while (!fp);
    *info = --no;
    return fp;
}

// Delete the old file and rename the new file
void lock_fclose(FILE *fp, const char *filename, int *info)
{
    if (fp)
    {
        fclose_(fp);
        std::string newfile = STRPRINTF("%s_%d.tmp", filename, *info);
        rename(newfile.c_str(), filename);
    }
}