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 | |
| 87 | #if !__EFUN_DEFINED__(copy_file) |
| 88 | #define MAXLEN 50000 |
| 89 | public nomask int copy_file(string source, string dest) |
| 90 | { |
| 91 | |
| 92 | int ptr; |
| 93 | string bytes; |
| 94 | |
| 95 | set_this_object(previous_object()); |
| 96 | if (!sizeof(source)||!sizeof(dest)||source==dest||(file_size(source)==-1)|| |
| 97 | (!call_other(master(),"valid_read",source, |
| 98 | getuid(this_interactive()|| |
| 99 | previous_object()),"read_file",previous_object()))|| |
| 100 | (!call_other(master(),"valid_read",source, |
| 101 | getuid(this_interactive()|| |
| 102 | previous_object()),"write_file",previous_object()))) |
| 103 | return 1; |
| 104 | switch (file_size(dest)) |
| 105 | { |
| 106 | case -1: |
| 107 | break; |
| 108 | case -2: |
| 109 | if (dest[<1]!='/') dest+="/"; |
| 110 | dest+=efun::explode(source,"/")[<1]; |
| 111 | if (file_size(dest)==-2) return 1; |
| 112 | if (file_size(dest)!=-1) break; |
| 113 | default: |
| 114 | if (!rm(dest)) return 1; |
| 115 | break; |
| 116 | } |
| 117 | do |
| 118 | { |
| 119 | bytes = read_bytes(source, ptr, MAXLEN); ptr += MAXLEN; |
| 120 | if (!bytes) bytes=""; |
| 121 | write_file(dest, bytes); |
| 122 | } |
| 123 | while(sizeof(bytes) == MAXLEN); |
| 124 | return 0; |
| 125 | } |
| 126 | #endif //!__EFUN_DEFINED__(copy_file) |
| 127 | |
Zesstra | 0b3538a | 2017-01-29 22:17:18 +0100 | [diff] [blame] | 128 | #undef PO |
Zesstra | 0fbf95d | 2017-01-29 21:48:02 +0100 | [diff] [blame] | 129 | |