| 87 | */ |
| 88 | |
| 89 | int /* O - 0 on success, -1 on error */ |
| 90 | cupsdCloseCreatedConfFile( |
| 91 | cups_file_t *fp, /* I - File to close */ |
| 92 | const char *filename) /* I - Filename */ |
| 93 | { |
| 94 | char newfile[1024], /* filename.N */ |
| 95 | oldfile[1024]; /* filename.O */ |
| 96 | |
| 97 | |
| 98 | /* |
| 99 | * Synchronize changes to disk if SyncOnClose is enabled. |
| 100 | */ |
| 101 | |
| 102 | if (SyncOnClose) |
| 103 | { |
| 104 | if (cupsFileFlush(fp)) |
| 105 | { |
| 106 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to write changes to \"%s\": %s", |
| 107 | filename, strerror(errno)); |
| 108 | cupsFileClose(fp); |
| 109 | return (-1); |
| 110 | } |
| 111 | |
| 112 | if (fsync(cupsFileNumber(fp))) |
| 113 | { |
| 114 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to sync changes to \"%s\": %s", |
| 115 | filename, strerror(errno)); |
| 116 | cupsFileClose(fp); |
| 117 | return (-1); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | /* |
| 122 | * First close the file... |
| 123 | */ |
| 124 | |
| 125 | if (cupsFileClose(fp)) |
| 126 | return (-1); |
| 127 | |
| 128 | /* |
| 129 | * Then remove "filename.O", rename "filename" to "filename.O", and rename |
| 130 | * "filename.N" to "filename". |
| 131 | */ |
| 132 | |
| 133 | snprintf(newfile, sizeof(newfile), "%s.N", filename); |
| 134 | snprintf(oldfile, sizeof(oldfile), "%s.O", filename); |
| 135 | |
| 136 | if ((cupsdUnlinkOrRemoveFile(oldfile) && errno != ENOENT) || |
| 137 | (rename(filename, oldfile) && errno != ENOENT) || |
| 138 | rename(newfile, filename)) |
| 139 | { |
| 140 | cupsdLogMessage(CUPSD_LOG_ERROR, "Unable to finalize \"%s\": %s", |
| 141 | filename, strerror(errno)); |
| 142 | return (-1); |
| 143 | } |
| 144 | |
| 145 | return (0); |
| 146 | } |
no test coverage detected