libtinysmb example?

Post Reply
kron
Posts: 1
Joined: Wed Jul 23, 2008 8:19 am

libtinysmb example?

Post by kron » Wed Jul 23, 2008 8:28 am

Hi,

I'm looking for some libtinysmb example code. I'm using libogc-20080602 with devkitPPC (Wii) and so far my only issue has been when I started playing with libtinysmb. I'm guessing I'm doing something wrong (getting link errors from ld) so some example code would likely get me past that. :)

Any help much appreciated.

--kron

DrTwox
Posts: 9
Joined: Thu Aug 21, 2008 10:41 am

Re: libtinysmb example?

Post by DrTwox » Fri Aug 22, 2008 10:47 am

I see your post is almost a month old - but just in case you're still here, or haven't got it working yet....

If you're having link errors make sure your LIBS line in your Makefile includes -ltinysmb. Mine looks like this:
LIBS := -lwiiuse -lbte -lfat -logc -ltinysmb -lm -lz

As for example code, try this: (But read the comments at the end. The short version now: libtinysmb doesn't work)

Code: Select all

/* Example only - not tested and might have some typos */
#include <smb.h>
...
#define READSIZE 4096
#define FILESIZE 2097152 
...
/* Copy infileName from a SMB share, write it to outfileName */
/* Returns 0 if successful */ 
unsigned int SMB_CopyFile(unsigned char *infileName, unsigned char *outfileName) 
{
	/* Connect to the SMB share */
	SMBCONN server = NULL;
	unsigned int rc = 0;
	rc = SMB_Connect(&server, "username", "password", NULL, "\\server", "share", "ipaddress"); /* Replace the strings with your settings */
	if (rc != SMB_SUCCESS) {
		return -1;
	}

	/* Create the SMB file handle for reading */
	SMBFILE in = SMB_OpenFile(fileName, SMB_OPEN_READING, SMB_OF_OPEN, server);
	if (in == NULL) {
	        return -1;
	}

	/* Create a file handle for writing */
	FILE *out = fopen(outfileName, "wb");
	if (out == NULL) {
		return -1;
	}

	/* Start reading from "in", writing to "out", stop when some condition is met */
	/* You really should do some extra checks in here */
	unsigned int bytesRead = 0; 
	unsigned int offset = 0; 	/* Each SMB_ReadFile request must specify from where to start reading */
	unsigned char buffer[READSIZE];

        /* This wont read FILESIZE exactly, but it will do for this example */
	while (offset < FILESIZE) {
		bytesRead = SMB_ReadFile(buffer, READSIZE, offset, in);
		fwrite(buffer, 1, bytesRead, out);
		fflush(out); /* Complete the write before the next read */
		offset += bytesRead;
	}

	/* Done! */
	SMB_CloseFile(in);
	SMB_Close(server);
	fclose(out);
	return 0;
}

/* Copy a file from the share to the front SD card */
SMB_CopyFile("\\path\to\read\file", "fat3:/path/to/write/file"); 
The above will work, but I've found some major problems with tinysmb (as it is now, it might be fixed later) that make it almost useless for practical applications.
* A READSIZE greater than 4096 can cause (and often does) SMB_ReadFile to return crazy values, even though smb.c supports a MAX_BUFFERSIZE of 63488.
* Reading more than approx 122000 bytes will crash the Wii (I haven't been able to determine the exact number)
* Looping over SMB_ReadFile 50-70 times often crashes the Wii
* Don't even think of using the SMB_Findxxxx functions to list directories - immediate Wii crash.

Post Reply

Who is online

Users browsing this forum: No registered users and 12 guests