shared folders problem: IO to overwritten file

Discussions about using Linux guests in VirtualBox.
Post Reply
krthie
Posts: 13
Joined: 22. Aug 2011, 13:37
Primary OS: MS Windows XP
VBox Version: OSE other
Guest OSses: ubuntu

shared folders problem: IO to overwritten file

Post by krthie »

Hi
I encounter problems in a rather specific set of circumstances:

My program creates a new file, writes to the file, and then reads what it wrote itself. This fails if the file already exists (on a shared folder), but works fine if the file didn't exist. This is probably best illustrated with an example C program:

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
  char buffer[1024];
  int ret;

  /* Open file test.txt in current dir as "read-write new binary file" */
  FILE *fptr= fopen("test.txt","wb+");
  if (fptr==NULL)
    {
      fprintf(stderr, "Error opening file\n"); 
      exit(1);
    }

  memset(buffer,'a',1024);

  ret = fwrite(buffer, 1, 1024, fptr);
  printf("fwrite return (should be 1024): %d\n", ret);
  
  ret=fseek(fptr, 512, 0);
  printf("fseek return (should be 0): %d\n", ret);

  ret=fread(buffer, 1, 512, fptr);
  printf("fread return (should be 512): %d\n", ret);

  /* return from main checking if fread did read 512 bytes */
  return ret==512 ? 0 : 1;
}
When I run this program somewhere in a new directory on a shared-folder, the first time it works fine and reports:
write return (should be 1024): 1024
fseek return (should be 0): 0
fread return (should be 512): 512
This creates a file test.txt (of size 1024). Rerunning the program now fails as test.txt already exists:
fwrite return (should be 1024): 1024
fseek return (should be 0): 0
fread return (should be 512): 0
It works fine again once I remove test.txt, or of course if I run this program creating a file in a folder in the disk image. (It also fails if the existing file test.txt has zero size).

Note that the use of the "w+" flag for fopen is crucial for the bug to appear. If I use "r+", the program always succeeds.

System details: host Windows XP3, guest Ubuntu 10.4 and 11.4 (same behaviour), virtualbox 2.1.4
Contents of /etc/fstab to mount the shared folders on my ubuntu 11.4 guest:

Code: Select all

krisaaaaaa /home/kris/WINFILS vboxsf uid=1000,gid=1000 0 0 
Can anyone help?

Thanks!

Kris
Perryg
Site Moderator
Posts: 34369
Joined: 6. Sep 2008, 22:55
Primary OS: Linux other
VBox Version: OSE self-compiled
Guest OSses: *NIX

Re: shared folders problem: IO to overwritten file

Post by Perryg »

First I would not put this in the fstab but rather rc.local.
Next you should add the options command and add rw to it.
Finally make sure that if you are going to manually mount this like you are doing to *NOT use the automount feature.
krthie
Posts: 13
Joined: 22. Aug 2011, 13:37
Primary OS: MS Windows XP
VBox Version: OSE other
Guest OSses: ubuntu

Re: shared folders problem: IO to overwritten file

Post by krthie »

Hi Perryg
thanks for lightning fast reply. I'll use rc.local in the future, but to test if this'll change anything related to this post I unmounted from the command line and hand-mounted instead:
sudo mount -t vboxsf -o uid=1000,gid=1000,rw krisaaaaaa /home/kris/WINFILS
Same behaviour. Is this what you meant?

Also, I didn't select the auto-mount option for this share in the VBox interface (which runs on windows).

So, seems I'm still stuck.

Kris
Perryg
Site Moderator
Posts: 34369
Joined: 6. Sep 2008, 22:55
Primary OS: Linux other
VBox Version: OSE self-compiled
Guest OSses: *NIX

Re: shared folders problem: IO to overwritten file

Post by Perryg »

Where is "krisaaaaaa" exactly? Is it in your Windows user space? If so try creating a folder one level below and make sure that the permissions are proper for everyone to write to. Shared folders are pretty much like a network share and Windows treats it the same.
krthie
Posts: 13
Joined: 22. Aug 2011, 13:37
Primary OS: MS Windows XP
VBox Version: OSE other
Guest OSses: ubuntu

Re: shared folders problem: IO to overwritten file

Post by krthie »

right. forgot to mention that. sorry.

krisaaaaaa is c:\Documents And Settings\kris

I see now in the FAQ that you don't like that, so I created a subdir ../kris/tmp in windows. It doesn't help. (Of course, I really wouldn't like to have to create a read/write-all directory in XP for serious work, but let's ignore that while debugging). Below's the detail.

At this point, I'll also have to confess that my host runs XP Home Edition (you can see why I need VB :? ), which doesn't have an interface to set permissions unless you reboot to Safe Mode. To solve that, I use Cygwin under XP (this is getting too complicated I guess). So, on cygwin I do
$ mkdir tmp; chmod ug+rwx tmp; getfacl tmp
# file: tmp
# owner: kris
# group: Users
user::rwx
group::rwx
mask:rwx
other:rwx
default:user::rwx
default:group::---
default:other:---
I then made this available (as transient) on VB, and mounted in Ubuntu guest:
$mkdir /home/kris/tmpmnt ; sudo mount -t vboxsf -o uid=1000,gid=1000,rw kristmp /home/kris/tmpmnt
$ getfacl ../tmpmnt/
# file: ../tmpmnt/
# owner: kris
# group: kris
user::rwx
group::rwx
other::rwx
Do you want me to use Safe Mode to check the Windows permissions via the Windows interface?

Kris

PS: please note that I can read/write files on the mounted folder(s). The failure only occurs when using the "w+" option to effectively truncate an existing file.
Perryg
Site Moderator
Posts: 34369
Joined: 6. Sep 2008, 22:55
Primary OS: Linux other
VBox Version: OSE self-compiled
Guest OSses: *NIX

Re: shared folders problem: IO to overwritten file

Post by Perryg »

I must admit that I have never used nor supported XP Home Edition. In all my years that one just never seemed to be requested, so if this is specific to that version (which it is starting to look like) I would not be the person that could answer your questions. I know that from what you are describing this looks like a permissions issue.

If you believe this to be a bug in VBox you would need to open a ticket in bugtracker as the DEVs are probably the only ones that could resolve this issue.
krthie
Posts: 13
Joined: 22. Aug 2011, 13:37
Primary OS: MS Windows XP
VBox Version: OSE other
Guest OSses: ubuntu

Re: shared folders problem: IO to overwritten file

Post by krthie »

ok. a small update: I checked file permissions after Safe Boot and they seemed to be alright, but didn't grant full access anyway, so I just ticked the "Full Access" box for £Everyone" (e.g. also in a new subdir of d:\). It doesn't help.

I'll post it to the tracker then.

Many many thanks for your help!

Kris

PS: I do have other strange things going on when writing on my shared folders. It seems unusable at the moment. Sigh
hakan
Posts: 3
Joined: 12. Nov 2012, 08:59

Re: shared folders problem: IO to overwritten file

Post by hakan »

Was there ever a ticket created for this in the bugtracker? I am failing one of the GNU MP library tests (t-inp_str.c) and I tracked it down this exact issue. I have also reproduced the issue with some code of my own:

Code: Select all

#include <stdio.h>

int main(void)
{
	char c = '0';
	FILE *fp = fopen("file.tmp", "w+");

	if(!fp)
	{
		printf("Error opening file\n");
	}		
	else
	{	
		printf("File position indicator: %ld\n", ftell(fp));
		printf("Write: %c\n", c);
		fputc(c, fp);
		printf("File position indicator: %ld\n", ftell(fp));
		printf("Flush\n");
		fflush(fp);
		printf("File position indicator: %ld\n", ftell(fp));
		printf("Rewind file position indicator\n");
		fseek(fp, 0L, SEEK_SET);
		printf("File position indicator: %ld\n", ftell(fp));
		c = fgetc(fp);

		if (c >= 0)
			printf("Read:  %c\n", c);
		else
			printf("Error\n");
	}
	return 0;
}
If the file "file.tmp" already exists, the code above prints "Error".
hakan
Posts: 3
Joined: 12. Nov 2012, 08:59

Re: shared folders problem: IO to overwritten file

Post by hakan »

Found the ticket: https://www.virtualbox.org/ticket/9485

Seems like there's been no activity yet.
hakan
Posts: 3
Joined: 12. Nov 2012, 08:59

Re: shared folders problem: IO to overwritten file

Post by hakan »

I think I may have found the root cause and I have updated the ticket. Is there anything I can do to flag this to get a developer to go look at the ticket?
krthie
Posts: 13
Joined: 22. Aug 2011, 13:37
Primary OS: MS Windows XP
VBox Version: OSE other
Guest OSses: ubuntu

Re: shared folders problem: IO to overwritten file

Post by krthie »

hi,
good investigational work! I have no idea how to get this fixed, but you certainly made it a lot easier!
Thanks
Kris
Post Reply