mirror of
https://github.com/gryf/wmaker.git
synced 2025-12-22 05:48:01 +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:
committed by
Carlos R. Mafra
parent
833128385f
commit
badecc244b
@@ -837,7 +837,7 @@ char* WMGetPropListDescription(WMPropList *plist, Bool indented);
|
||||
|
||||
WMPropList* WMReadPropListFromFile(char *file);
|
||||
|
||||
Bool WMWritePropListToFile(WMPropList *plist, char *path, Bool atomically);
|
||||
Bool WMWritePropListToFile(WMPropList *plist, char *path);
|
||||
|
||||
/*......................................................................*/
|
||||
|
||||
|
||||
@@ -115,7 +115,7 @@ typedef WMPropList* proplist_t;
|
||||
#define PLGetDataDescription(pl) WMGetPropListDescription(pl, False)
|
||||
|
||||
#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 */
|
||||
|
||||
@@ -1551,47 +1551,41 @@ WMPropList *WMReadPropListFromFile(char *file)
|
||||
|
||||
/* TODO: review this function's code */
|
||||
|
||||
Bool WMWritePropListToFile(WMPropList * plist, char *path, Bool atomically)
|
||||
Bool WMWritePropListToFile(WMPropList * plist, char *path)
|
||||
{
|
||||
char *thePath = NULL;
|
||||
char *desc;
|
||||
FILE *theFile;
|
||||
#ifdef HAVE_MKSTEMP
|
||||
int fd, mask;
|
||||
#endif
|
||||
|
||||
if (!WMMkDirHier(path))
|
||||
return False;
|
||||
|
||||
if (atomically) {
|
||||
#ifdef HAVE_MKSTEMP
|
||||
int fd, mask;
|
||||
#endif
|
||||
|
||||
/* 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");
|
||||
/* 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
|
||||
if ((fd = mkstemp(thePath)) < 0) {
|
||||
wsyserror(_("mkstemp (%s) failed"), thePath);
|
||||
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");
|
||||
if ((fd = mkstemp(thePath)) < 0) {
|
||||
wsyserror(_("mkstemp (%s) failed"), thePath);
|
||||
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
|
||||
|
||||
if (theFile == NULL) {
|
||||
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
|
||||
* real file. Also, we need to try to retain the file attributes of
|
||||
* the original file we are overwriting (if we are) */
|
||||
if (atomically) {
|
||||
if (rename(thePath, path) != 0) {
|
||||
wsyserror(_("rename ('%s' to '%s') failed"), thePath, path);
|
||||
goto failure;
|
||||
}
|
||||
if (rename(thePath, path) != 0) {
|
||||
wsyserror(_("rename ('%s' to '%s') failed"), thePath, path);
|
||||
goto failure;
|
||||
}
|
||||
|
||||
wfree(thePath);
|
||||
return True;
|
||||
|
||||
failure:
|
||||
if (atomically) {
|
||||
unlink(thePath);
|
||||
wfree(thePath);
|
||||
}
|
||||
|
||||
unlink(thePath);
|
||||
wfree(thePath);
|
||||
return False;
|
||||
}
|
||||
|
||||
|
||||
@@ -152,10 +152,10 @@ void WMSynchronizeUserDefaults(WMUserDefaults * database)
|
||||
} else {
|
||||
/* something happened with the file. just overwrite it */
|
||||
wwarning(_("cannot read domain from file '%s' when syncing"), path);
|
||||
WMWritePropListToFile(database->appDomain, path, True);
|
||||
WMWritePropListToFile(database->appDomain, path);
|
||||
}
|
||||
} else if (database->dirty) {
|
||||
WMWritePropListToFile(database->appDomain, path, True);
|
||||
WMWritePropListToFile(database->appDomain, path);
|
||||
} else if (fileIsNewer) {
|
||||
plF = WMReadPropListFromFile(path);
|
||||
if (plF) {
|
||||
@@ -167,7 +167,7 @@ void WMSynchronizeUserDefaults(WMUserDefaults * database)
|
||||
} else {
|
||||
/* something happened with the file. just overwrite it */
|
||||
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 {
|
||||
path = database->path;
|
||||
}
|
||||
WMWritePropListToFile(database->appDomain, path, True);
|
||||
WMWritePropListToFile(database->appDomain, path);
|
||||
database->dirty = 0;
|
||||
if (stat(path, &stbuf) >= 0)
|
||||
database->timestamp = stbuf.st_mtime;
|
||||
|
||||
@@ -1677,7 +1677,7 @@ static void storeData(_Panel * panel)
|
||||
|
||||
menu = buildPLFromMenu(panel);
|
||||
|
||||
WMWritePropListToFile(menu, panel->menuPath, True);
|
||||
WMWritePropListToFile(menu, panel->menuPath);
|
||||
|
||||
WMReleasePropList(menu);
|
||||
}
|
||||
|
||||
@@ -149,7 +149,7 @@ static void save(WMWidget * w, void *data)
|
||||
WMReleasePropList(keyList);
|
||||
/* puts("storing data"); */
|
||||
|
||||
WMWritePropListToFile(WindowMakerDB, WindowMakerDBPath, True);
|
||||
WMWritePropListToFile(WindowMakerDB, WindowMakerDBPath);
|
||||
|
||||
memset(&ev, 0, sizeof(XEvent));
|
||||
|
||||
|
||||
@@ -225,7 +225,7 @@ static void SaveHistory(WMArray * history, char *filename)
|
||||
for (i = 0; i < WMGetArrayItemCount(history); ++i)
|
||||
WMAddToPLArray(plhistory, WMCreatePLString(WMGetFromArray(history, i)));
|
||||
|
||||
WMWritePropListToFile(plhistory, (char *)filename, True);
|
||||
WMWritePropListToFile(plhistory, (char *)filename);
|
||||
WMReleasePropList(plhistory);
|
||||
}
|
||||
|
||||
|
||||
@@ -1080,7 +1080,7 @@ Bool UpdateDomainFile(WDDomain * domain)
|
||||
}
|
||||
}
|
||||
|
||||
result = WMWritePropListToFile(dict, domain->path, True);
|
||||
result = WMWritePropListToFile(dict, domain->path);
|
||||
|
||||
if (freeDict) {
|
||||
WMReleasePropList(dict);
|
||||
|
||||
@@ -967,7 +967,7 @@ void wScreenSaveState(WScreen * scr)
|
||||
snprintf(buf, sizeof(buf), "WMState.%i", scr->screen);
|
||||
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);
|
||||
}
|
||||
wfree(str);
|
||||
|
||||
@@ -135,7 +135,7 @@ int main(int argc, char **argv)
|
||||
WMReleasePropList(key);
|
||||
}
|
||||
|
||||
WMWritePropListToFile(style, file, True);
|
||||
WMWritePropListToFile(style, file);
|
||||
|
||||
exit(0);
|
||||
}
|
||||
|
||||
@@ -118,7 +118,7 @@ int main(int argc, char **argv)
|
||||
}
|
||||
|
||||
if (argc == 2) {
|
||||
WMWritePropListToFile(iconset, argv[1], False);
|
||||
WMWritePropListToFile(iconset, argv[1]);
|
||||
} else {
|
||||
puts(WMGetPropListDescription(iconset, True));
|
||||
}
|
||||
|
||||
@@ -612,11 +612,11 @@ int main(int argc, char **argv)
|
||||
path = wmalloc(strlen(ThemePath) + 32);
|
||||
strcpy(path, ThemePath);
|
||||
strcat(path, "/style");
|
||||
WMWritePropListToFile(style, path, False);
|
||||
WMWritePropListToFile(style, path);
|
||||
wfree(path);
|
||||
} else {
|
||||
if (style_file) {
|
||||
WMWritePropListToFile(style, style_file, False);
|
||||
WMWritePropListToFile(style, style_file);
|
||||
} else {
|
||||
puts(WMGetPropListDescription(style, True));
|
||||
}
|
||||
|
||||
@@ -135,7 +135,7 @@ int main(int argc, char **argv)
|
||||
}
|
||||
}
|
||||
|
||||
WMWritePropListToFile(all_windows, path, True);
|
||||
WMWritePropListToFile(all_windows, path);
|
||||
|
||||
exit(0);
|
||||
}
|
||||
|
||||
@@ -536,7 +536,7 @@ int main(int argc, char **argv)
|
||||
|
||||
WMMergePLDictionaries(prop, style, True);
|
||||
|
||||
WMWritePropListToFile(prop, path, True);
|
||||
WMWritePropListToFile(prop, path);
|
||||
{
|
||||
XEvent ev;
|
||||
|
||||
|
||||
@@ -124,7 +124,7 @@ int main(int argc, char **argv)
|
||||
WMPutInPLDictionary(dict, key, value);
|
||||
}
|
||||
|
||||
WMWritePropListToFile(dict, path, True);
|
||||
WMWritePropListToFile(dict, path);
|
||||
wfree(path);
|
||||
|
||||
return 0;
|
||||
|
||||
Reference in New Issue
Block a user