static void Nova_DumpSlots(void) { #define MAX_KEY_FILE_SIZE 16384 /* usually around 4000, cannot grow much */ char filename[CF_BUFSIZE]; int i; snprintf(filename, CF_BUFSIZE - 1, "%s%cts_key", GetStateDir(), FILE_SEPARATOR); char file_contents_new[MAX_KEY_FILE_SIZE] = {0}; for (i = 0; i < CF_OBSERVABLES; i++) { char line[CF_MAXVARSIZE]; if (NovaHasSlot(i)) { snprintf(line, sizeof(line), "%d,%s,%s,%s,%.3lf,%.3lf,%d\n", i, NULLStringToEmpty((char*)NovaGetSlotName(i)), NULLStringToEmpty((char*)NovaGetSlotDescription(i)), NULLStringToEmpty((char*)NovaGetSlotUnits(i)), NovaGetSlotExpectedMinimum(i), NovaGetSlotExpectedMaximum(i), NovaIsSlotConsolidable(i) ? 1 : 0); } else { snprintf(line, sizeof(line), "%d,spare,unused\n", i); } strlcat(file_contents_new, line, sizeof(file_contents_new)); } bool contents_changed = true; Writer *w = FileRead(filename, MAX_KEY_FILE_SIZE, NULL); if (w) { if(strcmp(StringWriterData(w), file_contents_new) == 0) { contents_changed = false; } WriterClose(w); } if(contents_changed) { Log(LOG_LEVEL_VERBOSE, "Updating %s with new slot information", filename); if(!FileWriteOver(filename, file_contents_new)) { Log(LOG_LEVEL_ERR, "Nova_DumpSlots: Could not write file '%s'. (FileWriteOver: %s)", filename, GetErrorStr()); } } chmod(filename, 0600); }
void test_file_write(void) { bool res = FileWriteOver(FILE_NAME, FILE_CONTENTS); assert_true(res); }