mirror of
https://github.com/gryf/wmaker.git
synced 2026-02-10 18:45: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:
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;
|
||||
|
||||
Reference in New Issue
Block a user