--- sourceforge.net/trunk/rdesktop/disk.c 2005/03/03 08:48:24 827 +++ sourceforge.net/trunk/rdesktop/disk.c 2006/07/02 13:03:22 1240 @@ -18,7 +18,6 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ -#include "rdesktop.h" #include "disk.h" #include @@ -28,6 +27,7 @@ #include /* opendir, closedir, readdir */ #include #include /* errno */ +#include #include #include /* ctime */ @@ -38,51 +38,105 @@ #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 +/* TODO: Fix mntent-handling for solaris + * #include */ +#if (defined(HAVE_MNTENT_H) && defined(HAVE_SETMNTENT)) +#include +#define MNTENT_PATH "/etc/mtab" +#define USE_SETMNTENT #endif -#if (defined(SOLARIS) || defined (__hpux) || defined(__BEOS__)) -#include /* solaris statvfs */ -/* TODO: Fix mntent-handling for solaris/hpux - * #include */ -#undef HAVE_MNTENT_H -#define MNTENT_PATH "/etc/mnttab" -#define STATFS_FN(path, buf) (statvfs(path,buf)) -#define STATFS_T statvfs -#define F_NAMELEN(buf) ((buf).f_namemax) +#ifdef HAVE_SYS_VFS_H +#include +#endif + +#ifdef HAVE_SYS_STATVFS_H +#include +#endif -#elif (defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__APPLE__)) +#ifdef HAVE_SYS_STATFS_H +#include +#endif + +#ifdef HAVE_SYS_PARAM_H #include +#endif + +#ifdef HAVE_SYS_MOUNT_H #include -#define STATFS_FN(path, buf) (statfs(path,buf)) +#endif + +#include "rdesktop.h" + +#ifdef STAT_STATFS3_OSF1 +#define STATFS_FN(path, buf) (statfs(path,buf,sizeof(buf))) #define STATFS_T statfs -#define F_NAMELEN(buf) (NAME_MAX) +#define USE_STATFS +#endif -#elif (defined(__SGI_IRIX__)) -#include -#include +#ifdef STAT_STATVFS #define STATFS_FN(path, buf) (statvfs(path,buf)) #define STATFS_T statvfs -#define F_NAMELEN(buf) ((buf).f_namemax) +#define USE_STATVFS +#endif -#elif (defined(__alpha) && !defined(linux)) -#include /* osf1 statfs */ -#define STATFS_FN(path, buf) (statfs(path,buf,sizeof(buf))) -#define STATFS_T statfs -#define F_NAMELEN(buf) (255) +#ifdef STAT_STATVFS64 +#define STATFS_FN(path, buf) (statvfs64(path,buf)) +#define STATFS_T statvfs64 +#define USE_STATVFS +#endif -#else -#include /* linux statfs */ -#include -#define HAVE_MNTENT_H -#define MNTENT_PATH "/etc/mtab" +#if (defined(STAT_STATFS2_FS_DATA) || defined(STAT_STATFS2_BSIZE) || defined(STAT_STATFS2_FSIZE)) #define STATFS_FN(path, buf) (statfs(path,buf)) #define STATFS_T statfs +#define USE_STATFS +#endif + +#ifdef STAT_STATFS4 +#define STATFS_FN(path, buf) (statfs(path,buf,sizeof(buf),0)) +#define STATFS_T statfs +#define USE_STATFS +#endif + +#if ((defined(USE_STATFS) && defined(HAVE_STRUCT_STATFS_F_NAMEMAX)) || (defined(USE_STATVFS) && defined(HAVE_STRUCT_STATVFS_F_NAMEMAX))) +#define F_NAMELEN(buf) ((buf).f_namemax) +#endif + +#if ((defined(USE_STATFS) && defined(HAVE_STRUCT_STATFS_F_NAMELEN)) || (defined(USE_STATVFS) && defined(HAVE_STRUCT_STATVFS_F_NAMELEN))) #define F_NAMELEN(buf) ((buf).f_namelen) #endif +#ifndef F_NAMELEN +#define F_NAMELEN(buf) (255) +#endif + +/* Dummy statfs fallback */ +#ifndef STATFS_T +struct dummy_statfs_t +{ + long f_bfree; + long f_bsize; + long f_blocks; + int f_namelen; + int f_namemax; +}; + +static int +dummy_statfs(struct dummy_statfs_t *buf) +{ + buf->f_blocks = 262144; + buf->f_bfree = 131072; + buf->f_bsize = 512; + buf->f_namelen = 255; + buf->f_namemax = 255; + + return 0; +} + +#define STATFS_T dummy_statfs_t +#define STATFS_FN(path,buf) (dummy_statfs(buf)) +#endif + extern RDPDR_DEVICE g_rdpdr_device[]; FILEINFO g_fileinfo[MAX_OPEN_FILES]; @@ -90,10 +144,10 @@ typedef struct { - char name[256]; - char label[256]; + char name[PATH_MAX]; + char label[PATH_MAX]; unsigned long serial; - char type[256]; + char type[PATH_MAX]; } FsInfoType; static NTSTATUS NotifyInfo(NTHANDLE handle, uint32 info_class, NOTIFY * p); @@ -147,7 +201,7 @@ { int ret; off_t pos; - static const char zero; + static const char zero = 0; /* Try the simple method first */ if ((ret = ftruncate(fd, length)) != -1) @@ -155,7 +209,7 @@ return ret; } - /* + /* * Some kind of error. Perhaps we were trying to grow. Retry * in a safe way. */ @@ -260,7 +314,7 @@ char *pos2; int count = 0; - // skip the first colon + /* skip the first colon */ optarg++; while ((pos = next_arg(optarg, ',')) && *id < RDPDR_MAX_DEVICES) { @@ -271,7 +325,7 @@ 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); + g_rdpdr_device[*id].local_path = (char *) xmalloc(strlen(pos2) + 1); strcpy(g_rdpdr_device[*id].local_path, pos2); g_rdpdr_device[*id].device_type = DEVICE_TYPE_DISK; count++; @@ -290,7 +344,7 @@ NTHANDLE handle; DIR *dirp; int flags, mode; - char path[256]; + char path[PATH_MAX]; struct stat filestat; handle = 0; @@ -306,38 +360,38 @@ { case CREATE_ALWAYS: - // Delete existing file/link. + /* Delete existing file/link. */ unlink(path); flags |= O_CREAT; break; case CREATE_NEW: - // If the file already exists, then fail. + /* If the file already exists, then fail. */ flags |= O_CREAT | O_EXCL; break; case OPEN_ALWAYS: - // Create if not already exists. + /* Create if not already exists. */ flags |= O_CREAT; break; case OPEN_EXISTING: - // Default behaviour + /* Default behaviour */ break; case TRUNCATE_EXISTING: - // If the file does not exist, then fail. + /* If the file does not exist, then fail. */ flags |= O_TRUNC; break; } - //printf("Open: \"%s\" flags: %X, accessmask: %X sharemode: %X create disp: %X\n", path, flags_and_attributes, accessmask, sharemode, create_disposition); + /*printf("Open: \"%s\" flags: %X, accessmask: %X sharemode: %X create disp: %X\n", path, flags_and_attributes, accessmask, sharemode, create_disposition); */ - // Get information about file and set that flag ourselfs + /* Get information about file and set that flag ourselfs */ if ((stat(path, &filestat) == 0) && (S_ISDIR(filestat.st_mode))) { if (flags_and_attributes & FILE_NON_DIRECTORY_FILE) @@ -437,7 +491,7 @@ g_fileinfo[handle].device_id = device_id; g_fileinfo[handle].flags_and_attributes = flags_and_attributes; g_fileinfo[handle].accessmask = accessmask; - strncpy(g_fileinfo[handle].path, path, 255); + strncpy(g_fileinfo[handle].path, path, PATH_MAX - 1); g_fileinfo[handle].delete_on_close = False; g_notify_stamp = True; @@ -568,7 +622,7 @@ path = g_fileinfo[handle].path; - // Get information about file + /* Get information about file */ if (fstat(handle, &filestat) != 0) { perror("stat"); @@ -576,7 +630,7 @@ return STATUS_ACCESS_DENIED; } - // Set file attributes + /* Set file attributes */ file_attributes = 0; if (S_ISDIR(filestat.st_mode)) file_attributes |= FILE_ATTRIBUTE_DIRECTORY; @@ -591,25 +645,25 @@ if (!(filestat.st_mode & S_IWUSR)) file_attributes |= FILE_ATTRIBUTE_READONLY; - // Return requested data + /* Return requested data */ switch (info_class) { case FileBasicInformation: seconds_since_1970_to_filetime(get_create_time(&filestat), &ft_high, &ft_low); - out_uint32_le(out, ft_low); //create_access_time + out_uint32_le(out, ft_low); /* create_access_time */ out_uint32_le(out, ft_high); seconds_since_1970_to_filetime(filestat.st_atime, &ft_high, &ft_low); - out_uint32_le(out, ft_low); //last_access_time + out_uint32_le(out, ft_low); /* last_access_time */ out_uint32_le(out, ft_high); seconds_since_1970_to_filetime(filestat.st_mtime, &ft_high, &ft_low); - out_uint32_le(out, ft_low); //last_write_time + out_uint32_le(out, ft_low); /* last_write_time */ out_uint32_le(out, ft_high); seconds_since_1970_to_filetime(filestat.st_ctime, &ft_high, &ft_low); - out_uint32_le(out, ft_low); //last_change_time + out_uint32_le(out, ft_low); /* last_change_time */ out_uint32_le(out, ft_high); out_uint32_le(out, file_attributes); @@ -617,13 +671,13 @@ case FileStandardInformation: - out_uint32_le(out, filestat.st_size); //Allocation size + out_uint32_le(out, filestat.st_size); /* Allocation size */ out_uint32_le(out, 0); - out_uint32_le(out, filestat.st_size); //End of file + out_uint32_le(out, filestat.st_size); /* End of file */ out_uint32_le(out, 0); - out_uint32_le(out, filestat.st_nlink); //Number of links - out_uint8(out, 0); //Delete pending - out_uint8(out, S_ISDIR(filestat.st_mode) ? 1 : 0); //Directory + out_uint32_le(out, filestat.st_nlink); /* Number of links */ + out_uint8(out, 0); /* Delete pending */ + out_uint8(out, S_ISDIR(filestat.st_mode) ? 1 : 0); /* Directory */ break; case FileObjectIdInformation: @@ -644,7 +698,7 @@ disk_set_information(NTHANDLE handle, uint32 info_class, STREAM in, STREAM out) { uint32 length, file_attributes, ft_high, ft_low, delete_on_close; - char newname[256], fullpath[256]; + char newname[PATH_MAX], fullpath[PATH_MAX]; struct fileinfo *pfinfo; int mode; struct stat filestat; @@ -663,23 +717,23 @@ in_uint8s(in, 4); /* Handle of root dir? */ in_uint8s(in, 24); /* unknown */ - // CreationTime + /* CreationTime */ in_uint32_le(in, ft_low); in_uint32_le(in, ft_high); - // AccessTime + /* AccessTime */ in_uint32_le(in, ft_low); in_uint32_le(in, ft_high); if (ft_low || ft_high) access_time = convert_1970_to_filetime(ft_high, ft_low); - // WriteTime + /* WriteTime */ in_uint32_le(in, ft_low); in_uint32_le(in, ft_high); if (ft_low || ft_high) write_time = convert_1970_to_filetime(ft_high, ft_low); - // ChangeTime + /* ChangeTime */ in_uint32_le(in, ft_low); in_uint32_le(in, ft_high); if (ft_low || ft_high) @@ -718,7 +772,7 @@ } if (!file_attributes) - break; // not valid + break; /* not valid */ mode = filestat.st_mode; @@ -835,7 +889,7 @@ if (memcmp(&pfinfo->notify, ¬ify, sizeof(NOTIFY))) { - //printf("disk_check_notify found changed event\n"); + /*printf("disk_check_notify found changed event\n"); */ memcpy(&pfinfo->notify, ¬ify, sizeof(NOTIFY)); status = STATUS_NOTIFY_ENUM_DIR; } @@ -906,7 +960,7 @@ if (!strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..")) continue; p->num_entries++; - fullname = xmalloc(strlen(pfinfo->path) + strlen(dp->d_name) + 2); + fullname = (char *) xmalloc(strlen(pfinfo->path) + strlen(dp->d_name) + 2); sprintf(fullname, "%s/%s", pfinfo->path, dp->d_name); if (!stat(fullname, &buf)) @@ -925,9 +979,9 @@ FsVolumeInfo(char *fpath) { - FILE *fdfs; static FsInfoType info; -#ifdef HAVE_MNTENT_H +#ifdef USE_SETMNTENT + FILE *fdfs; struct mntent *e; #endif @@ -936,14 +990,14 @@ strcpy(info.label, "RDESKTOP"); strcpy(info.type, "RDPFS"); -#ifdef HAVE_MNTENT_H +#ifdef USE_SETMNTENT fdfs = setmntent(MNTENT_PATH, "r"); if (!fdfs) return &info; while ((e = getmntent(fdfs))) { - if (strncmp(fpath, e->mnt_dir, strlen(fpath)) == 0) + if (str_startswith(e->mnt_dir, fpath)) { strcpy(info.type, e->mnt_type); strcpy(info.name, e->mnt_fsname); @@ -962,15 +1016,15 @@ info.serial = (buf[42] << 24) + (buf[41] << 16) + (buf[40] << 8) + buf[39]; - strncpy(info.label, buf + 43, 10); + strncpy(info.label, (char*)buf + 43, 10); info.label[10] = '\0'; } else if (lseek(fd, 32767, SEEK_SET) >= 0) /* ISO9660 */ { read(fd, buf, sizeof(buf)); - strncpy(info.label, buf + 41, 32); + strncpy(info.label, (char*)buf + 41, 32); info.label[32] = '\0'; - //info.Serial = (buf[128]<<24)+(buf[127]<<16)+(buf[126]<<8)+buf[125]; + /* info.Serial = (buf[128]<<24)+(buf[127]<<16)+(buf[126]<<8)+buf[125]; */ } close(fd); } @@ -1058,7 +1112,7 @@ disk_query_directory(NTHANDLE handle, uint32 info_class, char *pattern, STREAM out) { uint32 file_attributes, ft_low, ft_high; - char *dirname, fullpath[256]; + char *dirname, fullpath[PATH_MAX]; DIR *pdir; struct dirent *pdirent; struct stat fstat; @@ -1073,14 +1127,14 @@ { case FileBothDirectoryInformation: - // If a search pattern is received, remember this pattern, and restart search + /* If a search pattern is received, remember this pattern, and restart search */ if (pattern[0] != 0) { - strncpy(pfinfo->pattern, 1 + strrchr(pattern, '/'), 64); + strncpy(pfinfo->pattern, 1 + strrchr(pattern, '/'), PATH_MAX - 1); rewinddir(pdir); } - // find next dirent matching pattern + /* find next dirent matching pattern */ pdirent = readdir(pdir); while (pdirent && fnmatch(pfinfo->pattern, pdirent->d_name, 0) != 0) pdirent = readdir(pdir); @@ -1088,7 +1142,7 @@ if (pdirent == NULL) return STATUS_NO_MORE_FILES; - // Get information for directory entry + /* Get information for directory entry */ sprintf(fullpath, "%s/%s", dirname, pdirent->d_name); if (stat(fullpath, &fstat)) @@ -1119,34 +1173,34 @@ if (!(fstat.st_mode & S_IWUSR)) file_attributes |= FILE_ATTRIBUTE_READONLY; - // Return requested information - out_uint8s(out, 8); //unknown zero + /* Return requested information */ + out_uint8s(out, 8); /* unknown zero */ seconds_since_1970_to_filetime(get_create_time(&fstat), &ft_high, &ft_low); - out_uint32_le(out, ft_low); // create time + out_uint32_le(out, ft_low); /* create time */ out_uint32_le(out, ft_high); seconds_since_1970_to_filetime(fstat.st_atime, &ft_high, &ft_low); - out_uint32_le(out, ft_low); //last_access_time + out_uint32_le(out, ft_low); /* last_access_time */ out_uint32_le(out, ft_high); seconds_since_1970_to_filetime(fstat.st_mtime, &ft_high, &ft_low); - out_uint32_le(out, ft_low); //last_write_time + out_uint32_le(out, ft_low); /* last_write_time */ out_uint32_le(out, ft_high); seconds_since_1970_to_filetime(fstat.st_ctime, &ft_high, &ft_low); - out_uint32_le(out, ft_low); //change_write_time + out_uint32_le(out, ft_low); /* change_write_time */ out_uint32_le(out, ft_high); - out_uint32_le(out, fstat.st_size); //filesize low - out_uint32_le(out, 0); //filesize high - out_uint32_le(out, fstat.st_size); //filesize low - out_uint32_le(out, 0); //filesize high + out_uint32_le(out, fstat.st_size); /* filesize low */ + out_uint32_le(out, 0); /* filesize high */ + out_uint32_le(out, fstat.st_size); /* filesize low */ + out_uint32_le(out, 0); /* filesize high */ out_uint32_le(out, file_attributes); - out_uint8(out, 2 * strlen(pdirent->d_name) + 2); //unicode length - out_uint8s(out, 7); //pad? - out_uint8(out, 0); //8.3 file length - out_uint8s(out, 2 * 12); //8.3 unicode length + out_uint8(out, 2 * strlen(pdirent->d_name) + 2); /* unicode length */ + out_uint8s(out, 7); /* pad? */ + out_uint8(out, 0); /* 8.3 file length */ + out_uint8s(out, 2 * 12); /* 8.3 unicode length */ rdp_out_unistr(out, pdirent->d_name, 2 * strlen(pdirent->d_name)); break; @@ -1178,8 +1232,8 @@ switch (request) { - case 25: // ? - case 42: // ? + case 25: /* ? */ + case 42: /* ? */ default: unimpl("DISK IOCTL %d\n", request); return STATUS_INVALID_PARAMETER;