BUG: nvcc causes 'stat' command to return bad values File sizes reported as 0

I believe I’ve found a bug with nvcc that kills the ‘stat’ functionality on OS X (but not Linux).

I’m using the following test snippet:

#include <sys/stat.h>

#include <stdio.h>

long int file_size(const char *path) {

  struct stat file_status;

if(stat(path, &file_status) != 0){

	printf("Unable to stat %s\n", path);

	return -1;

  }

  printf("File size of %s is %d\n", path, (int)file_status.st_size);

  return file_status.st_size;

}

int main() {

char filename[] = "foo";

printf("Size: %d\n", (int)file_size(filename));

}

The following happens:

$ gcc -o test test.c; ./test

File size of foo is 10

Size: 10

$ nvcc -o test test.c; ./test

File size of foo is 0

Size: 0

Foo is, of course, 10 bytes.

$ nvcc --version

nvcc: NVIDIA (R) Cuda compiler driver

Copyright (c) 2005-2010 NVIDIA Corporation

Built on Thu_Jun_17_16:11:55_PDT_2010

Cuda compilation tools, release 3.1, V0.2.1221

Ideas? Suggestions?

//EDIT: Realized I was using 3.0, updated to 3.1, same issue.

Just found the same error; OS X Cuda 3.1. Going to have to do a workaround for the file info.

Just found the same error; OS X Cuda 3.1. Going to have to do a workaround for the file info.

Still fails on NVCC 4.0.2. The problem appears to be that ‘long’ is 4 bytes under nvcc, and 8 bytes under Apple’s gcc. As a result, nvcc thinks the struct timespec parts of struct stat are 8 bytes, not 16, and the st_size field is found at the wrong place. NVCC sees struct stat as 112 bytes, when 144 is actually correct. The failure appear’s to be caused by Apple: their <sys/_struct.h> uses long and off_t for the fields in struct timespec, rather than exact types such as int64_t. Try the following program (test1.c) under gcc and nvcc:

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>

int main() {
printf(“char: %ld\n”, sizeof(char));
printf(“short: %ld\n”, sizeof(short));
printf(“int: %ld\n”, sizeof(int));
printf(“long: %ld\n”, sizeof(long));
printf(“long long: %ld\n\n”, sizeof(long long));

printf(“float: %ld\n”, sizeof(float));
printf(“double: %ld\n”, sizeof(double));
printf(“long double: %ld\n\n”, sizeof(long double));

printf(“timespec: %ld\n”, sizeof(struct timespec));
printf(“stat: %ld\n”, sizeof(struct stat));
printf(“stat64: %ld\n\n”, sizeof(struct stat64));

printf(“off_t: %ld\n\n”, sizeof(off_t));
struct stat s;
lstat(“test1.c”, &s);
printf(“size: %ld\n”, s.st_size);
return EXIT_SUCCESS;
}