Yongbing's Blog

A personal technical note.

Atomic File Writing

| Comments

QA reported a bug, sometimes when power cycle target board right after pairing a bluetooth device, the whole bluetooth paring info lost. I found out the root cause is that the configure file of bluedroid lost all content in this case. Turned out the bluedroid configure file writing operation is not atomic. I came up with a revised file writing process, to guarantee it’s atomic:

  1. copy the configure file to a temporary file.
  2. write the update content to the temporary file.
  3. fsync the temporary file. (the step that bluedroid missed)
  4. rename the temporary file to configure file.

Because rename is atomic, and all steps before it are revertable (will not affect the configure file), so the whole process is atomic.

(atomic_write.c) download
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
/*
 * Copyright (C) 2013 Yongbing Chen <yongbing.chen.wh@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>

#define CHECK_RESULT(x) \
 do { \
     if (x != 0){ \
         printf("error %s\n", strerror(errno)); \
         return -1; \
     } \
 }while (0);

#ifndef TEMP_FAILURE_RETRY 
#define TEMP_FAILURE_RETRY(x) \
 ({ \
  int _result; \
  do _result = (int) (x); \
  while (_result == -1 && errno == EINTR); \
  _result; \
  })
#endif


static int copy_file(const char *src_file, const char *dst_file)
{
  int ret = -1;
  FILE *fp1, *fp2;
  fp1 = fopen(src_file,"r");
  if(fp1 == NULL){
      printf("open %s error %s\n",src_file, strerror(errno));
      return ret;
  }

  fp2 = fopen(dst_file,"w");
  if(fp2 == NULL){
      printf("open %s error %s\n", dst_file, strerror(errno)); fclose(fp1);
      return ret;
  }

  fseek(fp1, 0 , SEEK_END);
  int file_size = ftell(fp1);
  fseek(fp1, 0 , SEEK_SET);
  if (file_size == 0){
      fclose(fp1); fclose(fp2);
      return 0;
  }
  char *buffer = (char*)malloc(file_size);
  if (NULL == buffer){
      printf("error %s\n", strerror(errno)); fclose(fp1); fclose(fp2);
      return -1;
  }

  printf("copy len %d from %s to %s\n", file_size, src_file, dst_file);
  ret = fread(buffer, file_size, 1, fp1);
  if (ret != 1){
      printf("ret %d, error %s\n",ret, strerror(errno));
      goto error;
  }

  ret = fwrite(buffer, file_size, 1, fp2);
  if (ret != 1){
      printf("ret %d, error %s\n",ret, strerror(errno));
      goto error;
  }
  ret = 0;

error:
  fclose(fp1);
  fclose(fp2);
  free(buffer);
  return ret;
}


static int update_config_to_tmp_file(const char* curr_file, const char* file_name)
{
  int fd = open(file_name, O_CREAT | O_APPEND | O_RDWR, 0660);
  if(fd > 0){
      if(access(curr_file,  F_OK) == 0)
          CHECK_RESULT(copy_file(curr_file, file_name));
      int test_data = rand();
      printf("writing data %d to config file %s\n", test_data, file_name);
      int data_write = TEMP_FAILURE_RETRY(write(fd, &test_data, sizeof(test_data)));
      if (data_write < 0 || data_write != sizeof(test_data)){
          printf("%s failed len:%d %s",__func__, data_write, strerror(errno));
          return -1;
      }
  }
  else
      return -1;

  close(fd);
  return 0;

}

static int sync_conf_file(const char* file_name)
{
  int ret = -1;
  int fd = open(file_name, O_RDONLY);
  if(fd > 0){
      ret = fsync(fd);
  }
  close(fd);
  return ret;
}

int atomic_update_config_file()
{
  const char* file_name = "config";
  const char* file_name_new = "config.new";
  CHECK_RESULT(update_config_to_tmp_file(file_name, file_name_new));//open, write, close inside this operation.
  CHECK_RESULT(sync_conf_file(file_name_new));//open, fsync, close to achive a sync.
  CHECK_RESULT(rename(file_name_new, file_name));
  return 0;
}

int main(int argc, char **argv)
{
  int ret = atomic_update_config_file();
  if (ret != 0){
      printf("update config failed, keeping original one.\n");
  }
  return 0;
}

Reference:

Comments