UNIX/DOS newline translation for Samba
Posted December 4, 1998.
This document describes a patch to Samba version 1.9.16 which adds support for automatic newline translation between the UNIX and DOS worlds. The user should have experience with building Samba from a binary release, patching source code, and running Samba.
Back to davideous.com home.
1. UsageWhat this patch does
This patch modifies Samba version 1.9.16 (and possibly later patchlevel releases) for automatic newline translation between the UNIX and DOS worlds.
The problem is that UNIX and DOS use different binary codes to encode a newline, LF and CRLF, respectively. So, a DOS file in the UNIX world has a CR at the end of every line and a UNIX file in the DOS world is all just one line.
When transferring a text file between the UNIX and DOS worlds, one wants to do a newline translation. This is what the ASCII mode in FTP does. However, one does not want to modify binary files, such as jpegs, because that will mess up the binary format. This is what BINARY mode in FTP does.
So, if Samba is going to do this translation it needs to know if a file is a text file or a binary file. Then, if it is a text file the newline format must be detected and the change made.
My patch to Samba does exactly this. It assumes that the local text file format is UNIX and the remote text file format is DOS. As a part of every file transfer, it detects the file format, and, if needed, changes it. Therefore, the user does not have to worry about messing up the newline formats.
Annoyances & Bugs
There are a few annoyances and bugs in this patch. However, I believe it remains quite usable.
2. Theory of operationHere is my comment out of the servers.c giving an overview of what I did:
/* * This program was modified by David R. Harris (DRH) on and slightly * before the date 7/17/97. The ability to convert between Unix and * Dos file formats was added. The following modifications were made: * * Changes in server.c: * - Modified the function open_file to become open_file_wrapped * - Modified the function close_file to become close_file_wrapped * - Created the function open_file. * - Created the function open_file. * - Slightly modified the function write_file. * * Changes in smb.h: * - Added to the structure files_struct. * * Changes in Makefile: * - Added newlinelib.o to the dependencies of server.o. * * New files: * - newlinelib.c to do the newline format translation and detection. * - newlinelib.h header file for newlinelib.c */Now the details:
Whenever samba gets a request to open a file I catch it. If the file is being opened for reading and no newline translation needs to be done, the open request is just passed on normally. But if it is being opened for reading, a newline translation needs to be done, and the file is translated into a temporary file and samba open that tmp file for the user. If the file is being opened for writing or readwrite, then I have a tmp file opened for writing.
I also catch file close requests. If a temp file was created and written to, then its newline format is detected and copied it (with possible newline translations) to the real file. If the temp file was only read from, it is deleted.
I catch these close file requests and open file requests with the
I modified the function
I also modified
3. Distribution & InstallationI'm making this available in a few formats to make things easy. (Because I just had a horrible time applying a patch from someone-else.)
Here they are:
4. Request from authorIf you use this, I'd love to hear about it.