1
0
mirror of https://github.com/gryf/wmaker.git synced 2025-12-19 04:20:27 +01:00

Switch file ops to stdio

- Does away with the O_BINARY abomination
- as a byproduct, plugs an fd leak in wcolorpanel.c:fetchFile()
- sprinkle some fsync()s to files that have been written to (this
  needs to be done everywhere)

+ fix brown paper bag thinko in configure.ac
This commit is contained in:
Tamas TEVESZ
2010-03-26 20:25:27 +01:00
committed by Carlos R. Mafra
parent ea4645bc09
commit 71aa4f2884
8 changed files with 108 additions and 62 deletions

View File

@@ -1603,6 +1603,7 @@ Bool WMWritePropListToFile(WMPropList * plist, char *path)
wfree(desc); wfree(desc);
(void)fsync(fileno(theFile));
if (fclose(theFile) != 0) { if (fclose(theFile) != 0) {
wsyserror(_("fclose (%s) failed"), thePath); wsyserror(_("fclose (%s) failed"), thePath);
goto failure; goto failure;

View File

@@ -26,6 +26,8 @@
#include "wconfig.h" #include "wconfig.h"
#include "WINGsP.h" #include "WINGsP.h"
#include "rgb.h" #include "rgb.h"
#include <errno.h>
#include <math.h> #include <math.h>
#include <unistd.h> #include <unistd.h>
#include <ctype.h> #include <ctype.h>
@@ -33,7 +35,10 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <dirent.h> #include <dirent.h>
#include <errno.h>
#define RETRY( x ) do { \
x; \
} while (errno == EINTR);
/* BUG There's something fishy with shaped windows */ /* BUG There's something fishy with shaped windows */
/* Whithout shape extension the magnified image is completely broken -Dan */ /* Whithout shape extension the magnified image is completely broken -Dan */
@@ -270,11 +275,6 @@ enum {
#define M_PI 3.14159265358979323846 #define M_PI 3.14159265358979323846
#endif #endif
/* Silly hack for Windows systems with cygwin */
#ifndef O_BINARY
# define O_BINARY 0
#endif
static int fetchFile(char *toPath, char *imageSrcFile, char *imageDestFileName); static int fetchFile(char *toPath, char *imageSrcFile, char *imageDestFileName);
char *generateNewFilename(char *curName); char *generateNewFilename(char *curName);
void convertCPColor(CPColor * color); void convertCPColor(CPColor * color);
@@ -3343,35 +3343,45 @@ static void hsbInit(W_ColorPanel * panel)
static int fetchFile(char *toPath, char *srcFile, char *destFile) static int fetchFile(char *toPath, char *srcFile, char *destFile)
{ {
int src, dest; FILE *src, *dst;
int n; size_t nread, nwritten;
char *tmp; char *dstpath;
char buf[BUFSIZE]; char buf[BUFSIZE];
if ((src = open(srcFile, O_RDONLY | O_BINARY)) == 0) { RETRY( src = fopen(srcFile, "rb") )
if (src == NULL) {
wsyserror(_("Could not open %s"), srcFile); wsyserror(_("Could not open %s"), srcFile);
return -1; return -1;
} }
tmp = wstrconcat(toPath, destFile); dstpath = wstrconcat(toPath, destFile);
if ((dest = open(tmp, O_RDWR | O_CREAT | O_BINARY, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) RETRY( dst = fopen(dstpath, "wb") )
== 0) { if (dst == NULL) {
wsyserror(_("Could not create %s"), tmp); wsyserror(_("Could not create %s"), dstpath);
wfree(tmp); wfree(dstpath);
RETRY( fclose(src) )
return -1; return -1;
} }
wfree(tmp);
/* Copy the file */ do {
while ((n = read(src, buf, BUFSIZE)) > 0) { RETRY( nread = fread(buf, 1, sizeof(buf), src) )
if (write(dest, buf, n) != n) { if (ferror(src))
wsyserror(_("Write error on file %s"), destFile); break;
return -1;
}
}
close(src); RETRY( nwritten = fwrite(buf, 1, nread, dst) )
close(dest); if (ferror(dst) || feof(src))
break;
} while (1);
if (ferror(src) || ferror(dst))
unlink(dstpath);
RETRY( fclose(src) )
fsync(fileno(dst));
RETRY( fclose(dst) )
wfree(dstpath);
return 0; return 0;
} }

View File

@@ -402,6 +402,7 @@ static void dumpRImage(char *path, RImage * image)
fwrite(image->data, 1, image->width * image->height * channels, f); fwrite(image->data, 1, image->width * image->height * channels, f);
fsync(fileno(f));
if (fclose(f) < 0) { if (fclose(f) < 0) {
wsyserror(path); wsyserror(path);
} }

View File

@@ -699,6 +699,7 @@ static void storeCommandInScript(char *cmd, char *line)
fputs(line, fo); fputs(line, fo);
fputs("\n", fo); fputs("\n", fo);
} }
fsync(fileno(fo));
fclose(fo); fclose(fo);
if (rename(tmppath, path) != 0) { if (rename(tmppath, path) != 0) {
@@ -711,8 +712,10 @@ static void storeCommandInScript(char *cmd, char *line)
end: end:
wfree(path); wfree(path);
if (f) if (f) {
fsync(fileno(f)); /* this may be rw */
fclose(f); fclose(f);
}
} }
static void storeData(_Panel * panel) static void storeData(_Panel * panel)

View File

@@ -41,8 +41,7 @@ dnl Platform-specific Makefile setup
dnl ================================ dnl ================================
case "${host}" in case "${host}" in
*-*-cygwin*) *-*-linux*|*-*-cygwin*)
*-*-linux*)
WM_OSDEP="linux" WM_OSDEP="linux"
;; ;;
*-*-freebsd*) *-*-freebsd*)

View File

@@ -13,6 +13,10 @@
#include "wconfig.h" #include "wconfig.h"
#define RETRY( x ) do { \
x; \
} while (errno == EINTR);
/* /*
* copy argc and argv for an existing process identified by `pid' * copy argc and argv for an existing process identified by `pid'
* into suitable storage given in ***argv and *argc. * into suitable storage given in ***argv and *argc.
@@ -34,6 +38,8 @@ Bool GetCommandForPid(int pid, char ***argv, int *argc)
/* cmdline is a flattened series of null-terminated strings */ /* cmdline is a flattened series of null-terminated strings */
snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid); snprintf(buf, sizeof(buf), "/proc/%d/cmdline", pid);
while (1) { while (1) {
/* not switching this to stdio yet, as this does not need
* to be portable, and i'm lazy */
if ((fd = open(buf, O_RDONLY)) != -1) if ((fd = open(buf, O_RDONLY)) != -1)
break; break;
if (errno == EINTR) if (errno == EINTR)
@@ -46,12 +52,10 @@ Bool GetCommandForPid(int pid, char ***argv, int *argc)
break; break;
if (errno == EINTR) if (errno == EINTR)
continue; continue;
RETRY( close(fd) )
return False; return False;
} }
RETRY( close(fd) )
do {
close(fd);
} while (errno == EINTR);
/* count args */ /* count args */
for (i = 0; i < count; i++) for (i = 0; i < count; i++)

View File

@@ -25,6 +25,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
@@ -37,6 +38,10 @@
#include <libgen.h> #include <libgen.h>
#include <WINGs/WUtil.h> #include <WINGs/WUtil.h>
#define RETRY( x ) do { \
x; \
} while (errno == EINTR);
#ifndef PATH_MAX #ifndef PATH_MAX
#define PATH_MAX 1024 #define PATH_MAX 1024
#endif #endif
@@ -161,17 +166,21 @@ static Bool isFontOption(char *option)
return False; return False;
} }
/*
* copy a file specified by `file' into `directory'. name stays.
*/
/* /*
* it is more or less assumed that this function will only * it is more or less assumed that this function will only
* copy reasonably-sized files * copy reasonably-sized files
*/ */
/* XXX: is almost like WINGs/wcolodpanel.c:fetchFile() */
void copyFile(char *dir, char *file) void copyFile(char *dir, char *file)
{ {
int from_fd, to_fd; FILE *src, *dst;
size_t block, len; size_t nread, nwritten, len;
char buf[4096]; char buf[4096];
struct stat st; struct stat st;
char *dst; char *dstpath;
/* only to a directory */ /* only to a directory */
if (stat(dir, &st) != 0 || !S_ISDIR(st.st_mode)) if (stat(dir, &st) != 0 || !S_ISDIR(st.st_mode))
@@ -181,28 +190,44 @@ void copyFile(char *dir, char *file)
return; return;
len = strlen(dir) + 1 /* / */ + strlen(file) + 1 /* '\0' */; len = strlen(dir) + 1 /* / */ + strlen(file) + 1 /* '\0' */;
dst = wmalloc(len); dstpath = wmalloc(len);
snprintf(dst, len, "%s/%s", dir, basename(file)); snprintf(dstpath, len, "%s/%s", dir, basename(file));
buf[len] = '\0'; buf[len] = '\0';
if ((to_fd = open(dst, O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) == -1) { RETRY( dst = fopen(dstpath, "wb") )
wfree(dst); if (dst == NULL) {
return; wsyserror(_("Could not create %s"), dstpath);
} goto err;
wfree(dst);
if ((from_fd = open(file, O_RDONLY)) == -1) {
(void)close(to_fd);
return;
} }
/* XXX: signal handling */ RETRY( src = fopen(file, "rb") )
while ((block = read(from_fd, &buf, sizeof(buf))) > 0) if (src == NULL) {
write(to_fd, &buf, block); wsyserror(_("Could not open %s"), file);
goto err;
}
(void)fsync(to_fd); do {
(void)fchmod(to_fd, st.st_mode); RETRY( nread = fread(buf, 1, sizeof(buf), src) )
(void)close(to_fd); if (ferror(src))
(void)close(from_fd); break;
RETRY( nwritten = fwrite(buf, 1, nread, dst) )
if (ferror(dst) || feof(src))
break;
} while (1);
if (ferror(src) || ferror(dst))
unlink(dstpath);
fchmod(fileno(dst), st.st_mode);
fsync(fileno(dst));
RETRY( fclose(dst) )
err:
RETRY( fclose(src) )
wfree(dstpath);
return;
} }
void findCopyFile(char *dir, char *file) void findCopyFile(char *dir, char *file)

View File

@@ -21,6 +21,7 @@
#include <config.h> #include <config.h>
#include <errno.h>
#include <X11/Xlib.h> #include <X11/Xlib.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
@@ -37,10 +38,9 @@
#include "wraster.h" #include "wraster.h"
/* Silly hack for Windows systems with cygwin */ #define RETRY( x ) do { \
#ifndef O_BINARY x; \
# define O_BINARY 0 } while (errno == EINTR);
#endif
typedef struct RCachedImage { typedef struct RCachedImage {
RImage *image; RImage *image;
@@ -299,22 +299,25 @@ char *RGetImageFileFormat(char *file)
static int identFile(char *path) static int identFile(char *path)
{ {
int fd; FILE *file;
unsigned char buffer[32]; unsigned char buffer[32];
size_t nread;
assert(path != NULL); assert(path != NULL);
fd = open(path, O_RDONLY | O_BINARY); RETRY( file = fopen(path, "rb") )
if (fd < 0) { if (file == NULL) {
RErrorCode = RERR_OPEN; RErrorCode = RERR_OPEN;
return IM_ERROR; return IM_ERROR;
} }
if (read(fd, buffer, 32) < 1) {
close(fd); RETRY( nread = fread(buffer, 1, sizeof(buffer), file) )
if (nread < sizeof(buffer) || ferror(file)) {
RETRY( fclose(file) )
RErrorCode = RERR_READ; RErrorCode = RERR_READ;
return IM_ERROR; return IM_ERROR;
} }
close(fd); RETRY( fclose(file) )
/* check for XPM */ /* check for XPM */
if (strncmp((char *)buffer, "/* XPM */", 9) == 0) if (strncmp((char *)buffer, "/* XPM */", 9) == 0)