Mercurial > prosody-hg
comparison util/datamanager.lua @ 7202:5bf0ff3882aa
util.datamanager: Explicit handling of each error condition (see #632)
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Sat, 27 Feb 2016 15:29:56 +0100 |
| parents | 7a8cffafeff0 |
| children | 92f721226753 |
comparison
equal
deleted
inserted
replaced
| 7201:7a8cffafeff0 | 7202:5bf0ff3882aa |
|---|---|
| 142 end | 142 end |
| 143 | 143 |
| 144 local function atomic_store(filename, data) | 144 local function atomic_store(filename, data) |
| 145 local scratch = filename.."~"; | 145 local scratch = filename.."~"; |
| 146 local f, ok, msg; | 146 local f, ok, msg; |
| 147 repeat | 147 |
| 148 f, msg = io_open(scratch, "w"); | 148 f, msg = io_open(scratch, "w"); |
| 149 if not f then break end | 149 if not f then |
| 150 | 150 return nil, msg; |
| 151 ok, msg = f:write(data); | 151 end |
| 152 if not ok then break end | 152 |
| 153 | 153 ok, msg = f:write(data); |
| 154 ok, msg = f:close(); | 154 if not ok then |
| 155 f = nil; -- no longer valid | 155 f:close(); |
| 156 if not ok then break end | 156 os_remove(scratch); |
| 157 | 157 return nil, msg; |
| 158 return os_rename(scratch, filename); | 158 end |
| 159 until false; | 159 |
| 160 | 160 ok, msg = f:close(); |
| 161 -- Cleanup | 161 if not ok then |
| 162 if f then f:close(); end | 162 os_remove(scratch); |
| 163 os_remove(scratch); | 163 return nil, msg; |
| 164 return nil, msg; | 164 end |
| 165 | |
| 166 return os_rename(scratch, filename); | |
| 165 end | 167 end |
| 166 | 168 |
| 167 if prosody and prosody.platform ~= "posix" then | 169 if prosody and prosody.platform ~= "posix" then |
| 168 -- os.rename does not overwrite existing files on Windows | 170 -- os.rename does not overwrite existing files on Windows |
| 169 -- TODO We could use Transactional NTFS on Vista and above | 171 -- TODO We could use Transactional NTFS on Vista and above |
