Zesstra | 19d657d | 2017-01-29 21:40:38 +0100 | [diff] [blame] | 1 | #include "/sys/files.h" |
| 2 | |
Zesstra | 0b3538a | 2017-01-29 22:17:18 +0100 | [diff] [blame] | 3 | #define PO efun::previous_object(0) |
| 4 | |
| 5 | public varargs string read_data(string file, int start, int anzahl) |
Zesstra | 19d657d | 2017-01-29 21:40:38 +0100 | [diff] [blame] | 6 | { |
| 7 | if (!stringp(file)) |
| 8 | raise_error("Bad arg 1 to read_data(): expected 'string'.\n"); |
| 9 | |
| 10 | // Wenn Pfad nicht absolut, ist das nen Fehler, daher wird das nicht |
| 11 | // korrigiert. |
| 12 | |
| 13 | // wenn kein /data/ vorn steht, wird es vorangestellt. |
| 14 | if (strstr(file,"/"LIBDATADIR"/") != 0) |
| 15 | { |
| 16 | file = "/"LIBDATADIR + file; |
| 17 | } |
| 18 | // read_file() nicht direkt rufen, sondern vorher als closure ans |
| 19 | // aufrufende Objekt binden. Sonst laeuft die Rechtepruefung in |
| 20 | // valid_write() im Master unter der Annahme, dass die simul_efun.c mit |
| 21 | // ihrer root id was will. |
| 22 | return funcall(bind_lambda(#'efun::read_file, previous_object()), |
| 23 | file, start, anzahl); |
| 24 | } |
| 25 | |
| 26 | public varargs int write_data(string file, string str, int flags) |
| 27 | { |
| 28 | if (!stringp(file)) |
| 29 | raise_error("Bad arg 1 to write_data(): expected 'string'.\n"); |
| 30 | if (!stringp(str)) |
| 31 | raise_error("Bad arg 2 to write_data(): expected 'string'.\n"); |
| 32 | |
| 33 | // Wenn Pfad nicht absolut, ist das nen Fehler, daher wird das nicht |
| 34 | // korrigiert. |
| 35 | |
| 36 | // wenn kein /data/ vorn steht, wird es vorangestellt. |
| 37 | if (strstr(file,"/"LIBDATADIR"/") != 0) |
| 38 | { |
| 39 | file = "/"LIBDATADIR + file; |
| 40 | } |
| 41 | // write_file() nicht direkt rufen, sondern vorher als closure ans |
| 42 | // aufrufende Objekt binden. Sonst laeuft die Rechtepruefung in |
| 43 | // valid_write() im Master unter der Annahme, dass die simul_efun.c mit |
| 44 | // ihrer root id was will. |
| 45 | return funcall(bind_lambda(#'efun::write_file, previous_object()), |
| 46 | file, str, flags); |
| 47 | } |
Zesstra | 0fbf95d | 2017-01-29 21:48:02 +0100 | [diff] [blame] | 48 | |
| 49 | // * Bei 50k Groesse Log-File rotieren |
| 50 | varargs int log_file(string file, string txt, int size_to_break) |
| 51 | { |
| 52 | mixed *st; |
Zesstra | fa572c7 | 2017-01-29 22:09:30 +0100 | [diff] [blame] | 53 | // Wenn file schon mit /log/ anfaengt, schreiben wir nicht noch eins |
| 54 | // davor. |
| 55 | if (strstr(file,"/"LIBLOGDIR"/") != 0) |
| 56 | { |
| 57 | file="/log/"+file; |
| 58 | } |
Zesstra | 0fbf95d | 2017-01-29 21:48:02 +0100 | [diff] [blame] | 59 | |
Zesstra | fa572c7 | 2017-01-29 22:09:30 +0100 | [diff] [blame] | 60 | // Achtung: es ist verfuehrerisch, diese Pruefung zu entfernen und |
| 61 | // stattdessen, den Aufruf von write_file() an den Aufrufer zu binden. |
| 62 | // Dies funktioniert aber nicht, weil log_file gesonderte Behandlung in |
| 63 | // valid_write() erfaehrt. |
Zesstra | 0fbf95d | 2017-01-29 21:48:02 +0100 | [diff] [blame] | 64 | file=implode((efun::explode(file,"/")-({".."})),"/"); |
Zesstra | 0fbf95d | 2017-01-29 21:48:02 +0100 | [diff] [blame] | 65 | if (!funcall(bind_lambda(#'efun::call_other,PO),"secure/master",//') |
| 66 | "valid_write",file,geteuid(PO),"log_file",PO)) |
| 67 | return 0; |
Zesstra | fa572c7 | 2017-01-29 22:09:30 +0100 | [diff] [blame] | 68 | |
Zesstra | df15746 | 2018-07-31 21:49:59 +0200 | [diff] [blame] | 69 | // pruefen, ob das Verzeichnis existiert - sonst anlegen |
| 70 | string dir=file[..strrstr(file,"/")-1]; |
| 71 | if (file_size(dir) != -2) // FSIZE_DIR |
| 72 | { |
| 73 | if (!mkdirp(dir)) |
| 74 | return 0; |
| 75 | } |
| 76 | |
Zesstra | fa572c7 | 2017-01-29 22:09:30 +0100 | [diff] [blame] | 77 | // Wenn zu gross, rotieren. |
Zesstra | 0fbf95d | 2017-01-29 21:48:02 +0100 | [diff] [blame] | 78 | if ( size_to_break >= 0 & ( |
| 79 | sizeof(st = get_dir(file,2) ) && st[0] >= (size_to_break|MAX_LOG_SIZE))) |
| 80 | catch(rename(file, file + ".old");publish); /* No panic if failure */ |
| 81 | |
Zesstra | fa572c7 | 2017-01-29 22:09:30 +0100 | [diff] [blame] | 82 | // Die Zugriffspruefung hier laeuft mit Rechten der simul_efuns... |
| 83 | // Rechtepruefung oben |
Zesstra | 0fbf95d | 2017-01-29 21:48:02 +0100 | [diff] [blame] | 84 | return(write_file(file,txt)); |
| 85 | } |
| 86 | |
Zesstra | 0b3538a | 2017-01-29 22:17:18 +0100 | [diff] [blame] | 87 | #undef PO |
Zesstra | 0fbf95d | 2017-01-29 21:48:02 +0100 | [diff] [blame] | 88 | |