From 623c2fc3b52e3818513e83a31e5204b9c8cb568a Mon Sep 17 00:00:00 2001 From: pavel Date: Sat, 21 Sep 2002 09:47:38 +0000 Subject: [PATCH] r23: Automagically switch to readonly mode when device is readonly. Patch by Alberto Marmodoro. --- nbd-server.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/nbd-server.c b/nbd-server.c index 4d8c1e9..93448ac 100644 --- a/nbd-server.c +++ b/nbd-server.c @@ -19,9 +19,10 @@ * Version 2.0 - Version synchronised with client * Version 2.1 - Reap zombie client processes when they exit. Removed * (uncommented) the _IO magic, it's no longer necessary. + * Version 2.2 - Auto switch to read-only mode (usefull for floppies). */ -#define VERSION "2.1" +#define VERSION "2.2" #define GIGA (1*1024*1024*1024) #include @@ -101,6 +102,7 @@ void set_peername(int net,char *clientname); #define LINELEN 256 char difffilename[256]; unsigned int timeout = 0; +int autoreadonly = 0; int authorized_client(char *name) /* 0 - authorization refused, 1 - OK @@ -481,7 +483,7 @@ int mainloop(int net) DEBUG("wr: net->buf, "); readit(net, buf, len); DEBUG("buf->exp, "); - if (expwrite(request.from, buf, len)) { + if ((autoreadonly == 1) || expwrite(request.from, buf, len)) { DEBUG("Write failed: %m" ); ERROR; continue; @@ -581,8 +583,13 @@ void serveconnection(int net) sprintf(exportname3, exportname2, i/hunksize); printf( "Opening %s\n", exportname3 ); - if ((export[i/hunksize] = open(exportname3, (flags & F_READONLY) ? O_RDONLY : O_RDWR)) == -1) - err("Could not open exported file: %m"); + if ((export[i/hunksize] = open(exportname3, (flags & F_READONLY) ? O_RDONLY : O_RDWR)) == -1) { + /* Read WRITE ACCESS was requested by media is only read only */ + autoreadonly = 1; + flags |= F_READONLY; + if ((export[i/hunksize] = open(exportname3, O_RDONLY)) == -1) + err("Could not open exported file: %m"); + } } if (exportsize == (fsoffset_t)-1) { -- 1.7.10.4