1
0
mirror of https://github.com/gryf/wmaker.git synced 2026-02-14 12:55:47 +01:00

simplify WMWritePropListToFile()

remove the choice of atomic/non-atomic writes. firstly, the only users
of non-atomic writes were getstyle and geticonset; secondly, who in their
right minds would ever want non-atomic writes; thirdly, the file system
will screw you anyway *G*.
This commit is contained in:
Tamas TEVESZ
2010-03-15 23:12:24 +01:00
committed by Carlos R. Mafra
parent 833128385f
commit badecc244b
15 changed files with 47 additions and 58 deletions

View File

@@ -837,7 +837,7 @@ char* WMGetPropListDescription(WMPropList *plist, Bool indented);
WMPropList* WMReadPropListFromFile(char *file); WMPropList* WMReadPropListFromFile(char *file);
Bool WMWritePropListToFile(WMPropList *plist, char *path, Bool atomically); Bool WMWritePropListToFile(WMPropList *plist, char *path);
/*......................................................................*/ /*......................................................................*/

View File

@@ -115,7 +115,7 @@ typedef WMPropList* proplist_t;
#define PLGetDataDescription(pl) WMGetPropListDescription(pl, False) #define PLGetDataDescription(pl) WMGetPropListDescription(pl, False)
#define PLGetProplistWithPath(file) WMReadPropListFromFile(file) #define PLGetProplistWithPath(file) WMReadPropListFromFile(file)
#define PLSave(pl, file, atm) WMWritePropListToFile(pl, file, atm) #define PLSave(pl, file, atm) WMWritePropListToFile(pl, file)
/* Unsupported functions. Do not ask for them. They're evil :P */ /* Unsupported functions. Do not ask for them. They're evil :P */

View File

@@ -1551,47 +1551,41 @@ WMPropList *WMReadPropListFromFile(char *file)
/* TODO: review this function's code */ /* TODO: review this function's code */
Bool WMWritePropListToFile(WMPropList * plist, char *path, Bool atomically) Bool WMWritePropListToFile(WMPropList * plist, char *path)
{ {
char *thePath = NULL; char *thePath = NULL;
char *desc; char *desc;
FILE *theFile; FILE *theFile;
#ifdef HAVE_MKSTEMP
int fd, mask;
#endif
if (!WMMkDirHier(path)) if (!WMMkDirHier(path))
return False; return False;
if (atomically) { /* Use the path name of the destination file as a prefix for the
#ifdef HAVE_MKSTEMP * mkstemp() call so that we can be sure that both files are on
int fd, mask; * the same filesystem and the subsequent rename() will work. */
#endif thePath = wstrconcat(path, ".XXXXXX");
/* Use the path name of the destination file as a prefix for the
* mkstemp() call so that we can be sure that both files are on
* the same filesystem and the subsequent rename() will work. */
thePath = wstrconcat(path, ".XXXXXX");
#ifdef HAVE_MKSTEMP #ifdef HAVE_MKSTEMP
if ((fd = mkstemp(thePath)) < 0) { if ((fd = mkstemp(thePath)) < 0) {
wsyserror(_("mkstemp (%s) failed"), thePath); wsyserror(_("mkstemp (%s) failed"), thePath);
goto failure; goto failure;
}
mask = umask(0);
umask(mask);
fchmod(fd, 0644 & ~mask);
if ((theFile = fdopen(fd, "wb")) == NULL) {
close(fd);
}
#else
if (mktemp(thePath) == NULL) {
wsyserror(_("mktemp (%s) failed"), thePath);
goto failure;
}
theFile = fopen(thePath, "wb");
#endif
} else {
thePath = wstrdup(path);
theFile = fopen(thePath, "wb");
} }
mask = umask(0);
umask(mask);
fchmod(fd, 0644 & ~mask);
if ((theFile = fdopen(fd, "wb")) == NULL) {
close(fd);
}
#else
if (mktemp(thePath) == NULL) {
wsyserror(_("mktemp (%s) failed"), thePath);
goto failure;
}
theFile = fopen(thePath, "wb");
#endif
if (theFile == NULL) { if (theFile == NULL) {
wsyserror(_("open (%s) failed"), thePath); wsyserror(_("open (%s) failed"), thePath);
@@ -1616,22 +1610,17 @@ Bool WMWritePropListToFile(WMPropList * plist, char *path, Bool atomically)
/* If we used a temporary file, we still need to rename() it be the /* If we used a temporary file, we still need to rename() it be the
* real file. Also, we need to try to retain the file attributes of * real file. Also, we need to try to retain the file attributes of
* the original file we are overwriting (if we are) */ * the original file we are overwriting (if we are) */
if (atomically) { if (rename(thePath, path) != 0) {
if (rename(thePath, path) != 0) { wsyserror(_("rename ('%s' to '%s') failed"), thePath, path);
wsyserror(_("rename ('%s' to '%s') failed"), thePath, path); goto failure;
goto failure;
}
} }
wfree(thePath); wfree(thePath);
return True; return True;
failure: failure:
if (atomically) { unlink(thePath);
unlink(thePath); wfree(thePath);
wfree(thePath);
}
return False; return False;
} }

View File

@@ -152,10 +152,10 @@ void WMSynchronizeUserDefaults(WMUserDefaults * database)
} else { } else {
/* something happened with the file. just overwrite it */ /* something happened with the file. just overwrite it */
wwarning(_("cannot read domain from file '%s' when syncing"), path); wwarning(_("cannot read domain from file '%s' when syncing"), path);
WMWritePropListToFile(database->appDomain, path, True); WMWritePropListToFile(database->appDomain, path);
} }
} else if (database->dirty) { } else if (database->dirty) {
WMWritePropListToFile(database->appDomain, path, True); WMWritePropListToFile(database->appDomain, path);
} else if (fileIsNewer) { } else if (fileIsNewer) {
plF = WMReadPropListFromFile(path); plF = WMReadPropListFromFile(path);
if (plF) { if (plF) {
@@ -167,7 +167,7 @@ void WMSynchronizeUserDefaults(WMUserDefaults * database)
} else { } else {
/* something happened with the file. just overwrite it */ /* something happened with the file. just overwrite it */
wwarning(_("cannot read domain from file '%s' when syncing"), path); wwarning(_("cannot read domain from file '%s' when syncing"), path);
WMWritePropListToFile(database->appDomain, path, True); WMWritePropListToFile(database->appDomain, path);
} }
} }
@@ -199,7 +199,7 @@ void WMSaveUserDefaults(WMUserDefaults * database)
} else { } else {
path = database->path; path = database->path;
} }
WMWritePropListToFile(database->appDomain, path, True); WMWritePropListToFile(database->appDomain, path);
database->dirty = 0; database->dirty = 0;
if (stat(path, &stbuf) >= 0) if (stat(path, &stbuf) >= 0)
database->timestamp = stbuf.st_mtime; database->timestamp = stbuf.st_mtime;

View File

@@ -1677,7 +1677,7 @@ static void storeData(_Panel * panel)
menu = buildPLFromMenu(panel); menu = buildPLFromMenu(panel);
WMWritePropListToFile(menu, panel->menuPath, True); WMWritePropListToFile(menu, panel->menuPath);
WMReleasePropList(menu); WMReleasePropList(menu);
} }

View File

@@ -149,7 +149,7 @@ static void save(WMWidget * w, void *data)
WMReleasePropList(keyList); WMReleasePropList(keyList);
/* puts("storing data"); */ /* puts("storing data"); */
WMWritePropListToFile(WindowMakerDB, WindowMakerDBPath, True); WMWritePropListToFile(WindowMakerDB, WindowMakerDBPath);
memset(&ev, 0, sizeof(XEvent)); memset(&ev, 0, sizeof(XEvent));

View File

@@ -225,7 +225,7 @@ static void SaveHistory(WMArray * history, char *filename)
for (i = 0; i < WMGetArrayItemCount(history); ++i) for (i = 0; i < WMGetArrayItemCount(history); ++i)
WMAddToPLArray(plhistory, WMCreatePLString(WMGetFromArray(history, i))); WMAddToPLArray(plhistory, WMCreatePLString(WMGetFromArray(history, i)));
WMWritePropListToFile(plhistory, (char *)filename, True); WMWritePropListToFile(plhistory, (char *)filename);
WMReleasePropList(plhistory); WMReleasePropList(plhistory);
} }

View File

@@ -1080,7 +1080,7 @@ Bool UpdateDomainFile(WDDomain * domain)
} }
} }
result = WMWritePropListToFile(dict, domain->path, True); result = WMWritePropListToFile(dict, domain->path);
if (freeDict) { if (freeDict) {
WMReleasePropList(dict); WMReleasePropList(dict);

View File

@@ -967,7 +967,7 @@ void wScreenSaveState(WScreen * scr)
snprintf(buf, sizeof(buf), "WMState.%i", scr->screen); snprintf(buf, sizeof(buf), "WMState.%i", scr->screen);
str = wdefaultspathfordomain(buf); str = wdefaultspathfordomain(buf);
} }
if (!WMWritePropListToFile(scr->session_state, str, True)) { if (!WMWritePropListToFile(scr->session_state, str)) {
wsyserror(_("could not save session state in %s"), str); wsyserror(_("could not save session state in %s"), str);
} }
wfree(str); wfree(str);

View File

@@ -135,7 +135,7 @@ int main(int argc, char **argv)
WMReleasePropList(key); WMReleasePropList(key);
} }
WMWritePropListToFile(style, file, True); WMWritePropListToFile(style, file);
exit(0); exit(0);
} }

View File

@@ -118,7 +118,7 @@ int main(int argc, char **argv)
} }
if (argc == 2) { if (argc == 2) {
WMWritePropListToFile(iconset, argv[1], False); WMWritePropListToFile(iconset, argv[1]);
} else { } else {
puts(WMGetPropListDescription(iconset, True)); puts(WMGetPropListDescription(iconset, True));
} }

View File

@@ -612,11 +612,11 @@ int main(int argc, char **argv)
path = wmalloc(strlen(ThemePath) + 32); path = wmalloc(strlen(ThemePath) + 32);
strcpy(path, ThemePath); strcpy(path, ThemePath);
strcat(path, "/style"); strcat(path, "/style");
WMWritePropListToFile(style, path, False); WMWritePropListToFile(style, path);
wfree(path); wfree(path);
} else { } else {
if (style_file) { if (style_file) {
WMWritePropListToFile(style, style_file, False); WMWritePropListToFile(style, style_file);
} else { } else {
puts(WMGetPropListDescription(style, True)); puts(WMGetPropListDescription(style, True));
} }

View File

@@ -135,7 +135,7 @@ int main(int argc, char **argv)
} }
} }
WMWritePropListToFile(all_windows, path, True); WMWritePropListToFile(all_windows, path);
exit(0); exit(0);
} }

View File

@@ -536,7 +536,7 @@ int main(int argc, char **argv)
WMMergePLDictionaries(prop, style, True); WMMergePLDictionaries(prop, style, True);
WMWritePropListToFile(prop, path, True); WMWritePropListToFile(prop, path);
{ {
XEvent ev; XEvent ev;

View File

@@ -124,7 +124,7 @@ int main(int argc, char **argv)
WMPutInPLDictionary(dict, key, value); WMPutInPLDictionary(dict, key, value);
} }
WMWritePropListToFile(dict, path, True); WMWritePropListToFile(dict, path);
wfree(path); wfree(path);
return 0; return 0;