[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: Safe file update library ready (sort of)

Shachar Shemesh <shachar@debian.org> writes:

> Hi all,
> I've promised to get a library out there, and here it is. The base URL
> is https://github.com/Shachar/safewrite, and the actual code is at
> https://github.com/Shachar/safewrite/blob/master/safewrite.c
> This is not a formal release just yet (plus one function is still
> missing an implementation, trivial though it might be). It's just that
> the list obviously has a few people knowledgeable on the subject who
> can give my code a second look and see whether there is anything I
> have missed.
> I'll probably make an official release over the next couple of
> days. Feedback most appreciated.
> Shachar

Some things I noticed:

- missing headers, e.g. for mode_t
- no 'extern "C" {'

I don't like how your functions are destructive to the path argument. I
get that you need to cerate the real path and return that. But maybe you
could use

tyepdef char * path_t;

int safe_open( const char *name, path_t *path, int flags, mode_t mode )
int safe_close( const path_t *path, int fd )
int safe_close_sync( char path_t *path, int fd)

That way one could use

path_t path;
int fd = safe_open(".myapp.rc", &path, ...);


typedef struct {
        int fd;
        char buffer[PATH_MAX];
} safe_t;

safe_t * safe_open( const char *name, int flags, mode_t mode );
int safe_close( const safe_t *object);
int safe_close_sync( safe_t *object) ;
static inline int safe_fd(const safe_t *object) {
       if (object == NULL) {
          return -1;
       } else {
          return object->fd;

Just some thoughts,

Reply to: