()
| 3169 | await assertWritablePath(bkpPath); |
| 3170 | |
| 3171 | var writeFile = async function() |
| 3172 | { |
| 3173 | let fh; |
| 3174 | |
| 3175 | try |
| 3176 | { |
| 3177 | // O_SYNC is for sync I/O and reduce risk of file corruption |
| 3178 | fh = await fsProm.open(fileObject.path, O_SYNC | O_CREAT | O_WRONLY | O_TRUNC); |
| 3179 | await fsProm.writeFile(fh, data, writeEnc); |
| 3180 | await fh.sync(); // Flush to disk |
| 3181 | } |
| 3182 | finally |
| 3183 | { |
| 3184 | await fh?.close(); |
| 3185 | } |
| 3186 | |
| 3187 | let stat2 = await fsProm.stat(fileObject.path); |
| 3188 | // Workaround for possible writing errors is to check the written |
| 3189 | // contents of the file and retry 3 times before showing an error |
| 3190 | let writtenData = await fsProm.readFile(fileObject.path, writeEnc); |
| 3191 | |
| 3192 | if (data != writtenData) |
| 3193 | { |
| 3194 | retryCount++; |
| 3195 | |
| 3196 | if (retryCount < 3) |
| 3197 | { |
| 3198 | return await writeFile(); |
| 3199 | } |
| 3200 | else |
| 3201 | { |
| 3202 | throw new Error('all saving trials failed'); |
| 3203 | } |
| 3204 | } |
| 3205 | else |
| 3206 | { |
| 3207 | //We'll keep the backup file in case the original file is corrupted. TODO When should we delete the backup file? |
| 3208 | if (backupCreated) |
| 3209 | { |
| 3210 | //fs.unlink(bkpPath, (err) => {}); //Ignore errors! |
| 3211 | |
| 3212 | //Delete old backup file with old prefix |
| 3213 | if (fs.existsSync(oldBkpPath)) |
| 3214 | { |
| 3215 | try |
| 3216 | { |
| 3217 | await assertWritablePath(oldBkpPath); |
| 3218 | fs.unlink(oldBkpPath, (err) => {}); //Ignore errors |
| 3219 | } |
| 3220 | catch (e) {} //Ignore — path failed authorisation, skip cleanup. |
| 3221 | } |
| 3222 | } |
| 3223 | |
| 3224 | return stat2; |
| 3225 | } |
| 3226 | }; |
| 3227 | |
| 3228 | async function doSaveFile(isNew) |
no test coverage detected