--- sourceforge.net/trunk/rdesktop/disk.c 2004/05/11 12:46:39 696 +++ sourceforge.net/trunk/rdesktop/disk.c 2004/10/21 08:28:03 787 @@ -20,16 +20,6 @@ #include "disk.h" -#if (defined(sun) && (defined(__svr4__) || defined(__SVR4))) -#define SOLARIS -#endif - -#if (defined(SOLARIS) || defined(__hpux)) -#define DIRFD(a) ((a)->dd_fd) -#else -#define DIRFD(a) (dirfd(a)) -#endif - #include #include #include @@ -41,6 +31,16 @@ #include #include /* ctime */ +#if (defined(HAVE_DIRFD) || (HAVE_DECL_DIRFD == 1)) +#define DIRFD(a) (dirfd(a)) +#else +#define DIRFD(a) ((a)->DIR_FD_MEMBER_NAME) +#endif + +/* TODO: let autoconf figure out everything below... */ +#if (defined(sun) && (defined(__svr4__) || defined(__SVR4))) +#define SOLARIS +#endif #if (defined(SOLARIS) || defined (__hpux) || defined(__BEOS__)) #include /* solaris statvfs */ @@ -52,13 +52,20 @@ #define STATFS_T statvfs #define F_NAMELEN(buf) ((buf).f_namemax) -#elif (defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__)) +#elif (defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__APPLE__)) #include #include #define STATFS_FN(path, buf) (statfs(path,buf)) #define STATFS_T statfs #define F_NAMELEN(buf) (NAME_MAX) +#elif (defined(__SGI_IRIX__)) +#include +#include +#define STATFS_FN(path, buf) (statvfs(path,buf)) +#define STATFS_T statvfs +#define F_NAMELEN(buf) ((buf).f_namemax) + #else #include /* linux statfs */ #include @@ -125,6 +132,65 @@ } +/* A wrapper for ftruncate which supports growing files, even if the + native ftruncate doesn't. This is needed on Linux FAT filesystems, + for example. */ +static int +ftruncate_growable(int fd, off_t length) +{ + int ret; + off_t pos; + static const char zero; + + /* Try the simple method first */ + if ((ret = ftruncate(fd, length)) != -1) + { + return ret; + } + + /* + * Some kind of error. Perhaps we were trying to grow. Retry + * in a safe way. + */ + + /* Get current position */ + if ((pos = lseek(fd, 0, SEEK_CUR)) == -1) + { + perror("lseek"); + return -1; + } + + /* Seek to new size */ + if (lseek(fd, length, SEEK_SET) == -1) + { + perror("lseek"); + return -1; + } + + /* Write a zero */ + if (write(fd, &zero, 1) == -1) + { + perror("write"); + return -1; + } + + /* Truncate. This shouldn't fail. */ + if (ftruncate(fd, length) == -1) + { + perror("ftruncate"); + return -1; + } + + /* Restore position */ + if (lseek(fd, pos, SEEK_SET) == -1) + { + perror("lseek"); + return -1; + } + + return 0; +} + /* Enumeration of devices from rdesktop.c */ /* returns numer of units found and initialized. */ @@ -142,16 +208,14 @@ while ((pos = next_arg(optarg, ',')) && *id < RDPDR_MAX_DEVICES) { pos2 = next_arg(optarg, '='); - strcpy(g_rdpdr_device[*id].name, optarg); - toupper_str(g_rdpdr_device[*id].name); - - /* add trailing colon to name. */ - strcat(g_rdpdr_device[*id].name, ":"); + strncpy(g_rdpdr_device[*id].name, optarg, sizeof(g_rdpdr_device[*id].name)); + if (strlen(optarg) > 8) + fprintf(stderr, "share name %s truncated to %s\n", optarg, + g_rdpdr_device[*id].name); g_rdpdr_device[*id].local_path = xmalloc(strlen(pos2) + 1); strcpy(g_rdpdr_device[*id].local_path, pos2); - printf("DISK %s to %s\n", g_rdpdr_device[*id].name, g_rdpdr_device[*id].local_path); g_rdpdr_device[*id].device_type = DEVICE_TYPE_DISK; count++; (*id)++; @@ -164,9 +228,9 @@ /* Opens or creates a file or directory */ static NTSTATUS disk_create(uint32 device_id, uint32 accessmask, uint32 sharemode, uint32 create_disposition, - uint32 flags_and_attributes, char *filename, HANDLE * phandle) + uint32 flags_and_attributes, char *filename, NTHANDLE * phandle) { - HANDLE handle; + NTHANDLE handle; DIR *dirp; int flags, mode; char path[256]; @@ -177,8 +241,7 @@ flags = 0; mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH; - - if (filename[strlen(filename) - 1] == '/') + if (*filename && filename[strlen(filename) - 1] == '/') filename[strlen(filename) - 1] = 0; sprintf(path, "%s%s", g_rdpdr_device[device_id].local_path, filename); @@ -320,7 +383,7 @@ } static NTSTATUS -disk_close(HANDLE handle) +disk_close(NTHANDLE handle) { struct fileinfo *pfinfo; @@ -340,7 +403,7 @@ } static NTSTATUS -disk_read(HANDLE handle, uint8 * data, uint32 length, uint32 offset, uint32 * result) +disk_read(NTHANDLE handle, uint8 * data, uint32 length, uint32 offset, uint32 * result) { int n; @@ -377,7 +440,7 @@ } static NTSTATUS -disk_write(HANDLE handle, uint8 * data, uint32 length, uint32 offset, uint32 * result) +disk_write(NTHANDLE handle, uint8 * data, uint32 length, uint32 offset, uint32 * result) { int n; @@ -404,7 +467,7 @@ } NTSTATUS -disk_query_information(HANDLE handle, uint32 info_class, STREAM out) +disk_query_information(NTHANDLE handle, uint32 info_class, STREAM out) { uint32 file_attributes, ft_high, ft_low; struct stat filestat; @@ -485,9 +548,9 @@ } NTSTATUS -disk_set_information(HANDLE handle, uint32 info_class, STREAM in, STREAM out) +disk_set_information(NTHANDLE handle, uint32 info_class, STREAM in, STREAM out) { - uint32 device_id, length, file_attributes, ft_high, ft_low; + uint32 length, file_attributes, ft_high, ft_low; char newname[256], fullpath[256]; struct fileinfo *pfinfo; @@ -653,11 +716,8 @@ if (stat_fs.f_bsize * stat_fs.f_bfree < length) return STATUS_DISK_FULL; - /* FIXME: Growing file with ftruncate doesn't - work with Linux FAT fs */ - if (ftruncate(handle, length) != 0) + if (ftruncate_growable(handle, length) != 0) { - perror("ftruncate"); return STATUS_DISK_FULL; } @@ -739,7 +799,7 @@ NTSTATUS -disk_query_volume_information(HANDLE handle, uint32 info_class, STREAM out) +disk_query_volume_information(NTHANDLE handle, uint32 info_class, STREAM out) { struct STATFS_T stat_fs; struct fileinfo *pfinfo; @@ -804,7 +864,7 @@ } NTSTATUS -disk_query_directory(HANDLE handle, uint32 info_class, char *pattern, STREAM out) +disk_query_directory(NTHANDLE handle, uint32 info_class, char *pattern, STREAM out) { uint32 file_attributes, ft_low, ft_high; char *dirname, fullpath[256]; @@ -840,13 +900,23 @@ // Get information for directory entry sprintf(fullpath, "%s/%s", dirname, pdirent->d_name); - /* JIF - printf("Stat: %s\n", fullpath); */ if (stat(fullpath, &fstat)) { - perror("stat"); - out_uint8(out, 0); - return STATUS_ACCESS_DENIED; + switch (errno) + { + case ENOENT: + case ELOOP: + case EACCES: + /* These are non-fatal errors. */ + memset(&fstat, 0, sizeof(fstat)); + break; + default: + /* Fatal error. By returning STATUS_NO_SUCH_FILE, + the directory list operation will be aborted */ + perror(fullpath); + out_uint8(out, 0); + return STATUS_NO_SUCH_FILE; + } } if (S_ISDIR(fstat.st_mode)) @@ -904,10 +974,8 @@ static NTSTATUS -disk_device_control(HANDLE handle, uint32 request, STREAM in, STREAM out) +disk_device_control(NTHANDLE handle, uint32 request, STREAM in, STREAM out) { - uint32 result; - if (((request >> 16) != 20) || ((request >> 16) != 9)) return STATUS_INVALID_PARAMETER;