write(2) System Calls Manual write(2) NAZWA write - zapisuje do deskryptora pliku BIBLIOTEKA Standardowa biblioteka C (libc, -lc) SKLADNIA #include ssize_t write(int fd, const void buf[.liczba], size_t liczba); OPIS write() writes up to count bytes from the buffer starting at buf to the file referred to by the file descriptor fd. Liczba bajtow zapisanych moze byc mniejsza niz liczba jezeli, na przyklad nie ma wystarczajacej ilosci wolnej przestrzeni na urzadzeniu fizycznym, lub zasoby RLIMIT_FSIZE zostaly wyczerpane (patrz setrlimit(2)), wywolanie zostalo przerwane przez obsluge sygnalu po zapisaniu mniej niz liczba bajtow (spojrz rowniez na pipe(7)). Dla przeszukiwalnych plikow (np. takie na ktorych mozna uzyc lseek(2), na przyklad, pliki zwykle (regular)) zapis ma miejsce w danym przesunieciu pliku (offsecie), i to przesuniecie jest zwiekszane o ilosc aktualnie zapisanych danych. Jezeli plik zostal otwarty (open(2)) z O_APPEND, wtedy przesunieciem (offsetem) jest koniec pliku przed zapisem. Dostosowanie przesuniecia pliku i operacja zapisu sa wykonywane jako nierozdzielne (atomowe). POSIX requires that a read(2) that can be proved to occur after a write() has returned will return the new data. Note that not all filesystems are POSIX conforming. According to POSIX.1, if count is greater than SSIZE_MAX, the result is implementation-defined; see NOTES for the upper limit on Linux. WARTOSC ZWRACANA On success, the number of bytes written is returned. On error, -1 is returned, and errno is set to indicate the error. Note that a successful write() may transfer fewer than count bytes. Such partial writes can occur for various reasons; for example, because there was insufficient space on the disk device to write all of the requested bytes, or because a blocked write() to a socket, pipe, or similar was interrupted by a signal handler after it had transferred some, but before it had transferred all of the requested bytes. In the event of a partial write, the caller can make another write() call to transfer the remaining bytes. The subsequent call will either transfer further bytes or may result in an error (e.g., if the disk is now full). If count is zero and fd refers to a regular file, then write() may return a failure status if one of the errors below is detected. If no errors are detected, or error detection is not performed, 0 is returned without causing any other effect. If count is zero and fd refers to a file other than a regular file, the results are not specified. BLEDY EAGAIN Deskryptor pliku fd odwoluje sie do gniazda i zostal oznaczony jako nieblokujacy (O_NONBLOCK), a zapis go zablokuje. Zob. open(2) aby dowiedziec sie wiecej o fladze O_NONBLOCK. EAGAIN lub EWOULDBLOCK Deskryptor pliku fd odwoluje sie do gniazda i zostal oznaczony jako nieblokujacy (O_NONBLOCK), a zapis go zablokuje. POSIX.1-2001 pozwala w tej sytuacji na zwrocenie bledu ale nie wymaga aby ta stala miala taka sama wartosc, portowalna aplikacja powinna sprawdzac obie mozliwosci. EBADF fd nie jest prawidlowym deskryptorem pliku lub nie jest otwarty do zapisu EDESTADDRREQ fd odwoluje sie do gniazda datagramowego dla ktorego adres nie zostal ustalony przy uzyciu connect(2). EDQUOT Kwota blokow dyskowych uzytkownika dotyczaca systemu plikow zawierajacego plik wskazany przez fd zostala wyczerpana. EFAULT buf jest poza dostepna przestrzenia adresowa. EFBIG Dokonano proby zapisu pliku ktory przekracza zdefiniowane w implementacji maksymalne rozmiary pliku, rozmiary pliku procesu lub zapis na pozycje wykraczajaca poza maksymalne dozwolone przesuniecie (offset). EINTR Wywolanie zostalo przerwane przez sygnal przed zapisaniem jakichkolwiek danych, patrz signal(7). EINVAL fd jest dolaczony do obiektu nieodpowiedniego do zapisu, plik zostal otwarty z flaga O_DIRECT i adres podany w buf badz wartosc liczba lub przesuniecie (offset) nie zostaly odpowiednio dopasowane. EIO A low-level I/O error occurred while modifying the inode. This error may relate to the write-back of data written by an earlier write(), which may have been issued to a different file descriptor on the same file. Since Linux 4.13, errors from write-back come with a promise that they may be reported by subsequent. write() requests, and will be reported by a subsequent fsync(2) (whether or not they were also reported by write()). An alternate cause of EIO on networked filesystems is when an advisory lock had been taken out on the file descriptor and this lock has been lost. See the Lost locks section of fcntl(2) for further details. ENOSPC Urzadzenie zawierajace plik wskazany przez fd nie ma miejsca na dane. EPERM Operacja zablokowana przez zakluczenie pliku (ang. file seal); zob. fcntl(2). EPIPE fd jest podlaczony do potoku (pipe) lub gniazda (socket) ktorego koncowka odczytujaca jest zamknieta. Gdy taka sytuacja nastepuje, proces zapisujacy rowniez otrzyma sygnal SIGPIPE. (Wiec wartosc zwracana przez write() jest widziana tylko wowczas gdy program obsluguje, blokuje lub ignoruje ten sygnal). Zaleznie od obiektu podlaczonego do fd, moga takze zajsc inne (nieopisane) bledy. STANDARDY POSIX.1-2008. HISTORIA SVr4, 4.3BSD, POSIX.1-2001. Pod SVr4 EINTR jest zwracane zarowno gdy po przerwaniu wywolania, dane zostaly zapisane czesciowo lub przed jakimkolwiek zapisem. UWAGI A successful return from write() does not make any guarantee that data has been committed to disk. On some filesystems, including NFS, it does not even guarantee that space has successfully been reserved for the data. In this case, some errors might be delayed until a future write(), fsync(2), or even close(2). The only way to be sure is to call fsync(2) after you are done writing all your data. Jezeli write() zostanie przerwany przez obsluge sygnalu przed zapisaniem jakichkolwiek danych, wtedy wywolanie nie powiedzie sie i zwracany jest blad EINTR; jezeli przerwanie nastapi po zapisaniu co najmniej jednego bajtu danych, wywolanie powiedzie sie i zwroci ilosc zapisanych bajtow danych. W Linuksie, write() (i podobne wywolania systemowe) moga dokonac transferu co najwyzej 0x7ffff000 (2 147 479 552) bajtow, zwracajac liczbe bajtow rzeczywiscie przetransferowanych (jest to prawdziwe zarowno dla systemow 32 jak i 64-bitowych). An error return value while performing write() using direct I/O does not mean the entire write has failed. Partial data may be written and the data at the file offset on which the write() was attempted should be considered inconsistent. USTERKI Zgodnie z POSIX.1-2008/SUSv4 Section XSI 2.9.7 ("Thread Interactions with Regular File Operations"): Wszystkie ponizsze funkcje powinny byc atomowe w odniesieniu do innych biorac pod uwage wyniki okreslone w POSIX.1-2008, gdy dzialaja na zwyklych plikach lub dowiazaniach symbolicznych: ... Among the APIs subsequently listed are write() and writev(2). And among the effects that should be atomic across threads (and processes) are updates of the file offset. However, before Linux 3.14, this was not the case: if two processes that share an open file description (see open(2)) perform a write() (or writev(2)) at the same time, then the I/O operations were not atomic with respect to updating the file offset, with the result that the blocks of data output by the two processes might (incorrectly) overlap. This problem was fixed in Linux 3.14. ZOBACZ TAKZE close(2), fcntl(2), fsync(2), ioctl(2), lseek(2), open(2), pwrite(2), read(2), select(2), writev(2), fwrite(3) TLUMACZENIE Autorami polskiego tlumaczenia niniejszej strony podrecznika sa: Artur Kruszewski , Michal Kulach i Robert Luberda Niniejsze tlumaczenie jest wolna dokumentacja. Blizsze informacje o warunkach licencji mozna uzyskac zapoznajac sie z GNU General Public License w wersji 3 lub nowszej. Nie przyjmuje sie ZADNEJ ODPOWIEDZIALNOSCI. Bledy w tlumaczeniu strony podrecznika prosimy zglaszac na adres listy dyskusyjnej . Linux man-pages 6.06 31 pazdziernika 2023 r. write(2)