Преглед изворни кода

Some files can not be sent using sendfile

It seems some Linux pseudo-files can not be sent using sendfile,
e.g., files within the sysfs like /sys/class/net/eth0/address

See #190
bel пре 10 година
родитељ
комит
cba00b4b61
1 измењених фајлова са 10 додато и 0 уклоњено
  1. 10 0
      src/civetweb.c

+ 10 - 0
src/civetweb.c

@@ -5431,6 +5431,7 @@ static void send_file_data(struct mg_connection *conn,
 {
 	char buf[MG_BUF_LEN];
 	int to_read, num_read, num_written;
+	int loop_cnt = 0;
 	int64_t size;
 
 	if (!filep || !conn) {
@@ -5467,8 +5468,15 @@ static void send_file_data(struct mg_connection *conn,
 					conn->num_bytes_sent += sf_sent;
 					len -= sf_sent;
 					offset += sf_sent;
+				} else if (loop_cnt == 0) {
+					/* This file can not be sent using sendfile.
+					 * This might be the case for pseudo-files in the
+					 * /sys/ and /proc/ file system.
+					 * Use the regular user mode copy code instead. */
+					break;
 				}
 
+				loop_cnt++;
 			} while ((len > 0) && (sf_sent >= 0));
 
 			if (sf_sent > 0) {
@@ -5512,6 +5520,8 @@ static void send_file_data(struct mg_connection *conn,
 				/* Both read and were successful, adjust counters */
 				conn->num_bytes_sent += num_written;
 				len -= num_written;
+
+				loop_cnt++;
 			}
 		}
 	}