21

I am having trouble finding this information, and trial and error is telling me that the value is very high. I figured I would ask the community to see if anyone knows and can point me to an apple page that confirms the length for Lion. All I know is it is larger that Snow Leopard.

Jasperan
  • 2,154
  • 1
  • 16
  • 40
Rodney S. Foley
  • 10,190
  • 12
  • 48
  • 66
  • 2
    I'm going to go out on a limb and say that any application that remotely approaches this limit is a really bad idea. – jonmorgan Aug 21 '11 at 19:42
  • @spookyjon: +1. Likely to depend on the filesystem too. – Donal Fellows Aug 21 '11 at 19:44
  • 6
    @spookyjon You are missing the point, in Windows its 248, in 10.6 its 250, and in Lion it seems much much larger. It is about testing the limits to make sure you don't violate them, but to know for you need to know what the boundaries are. You are making assumptions about something you have no idea about and shouldn't matter WHY I ask a question but to down vote it because you personally disagree on what you have made as the assumptions is what is really bad. – Rodney S. Foley Aug 21 '11 at 19:46
  • @Donal the filesystem would be Lion's default which is Mac OS Extended (Journaled) – Rodney S. Foley Aug 21 '11 at 19:47
  • maximum filename leght was in 10.6 255 and maxumem path length was "unlimited" - wouldn't expect that it got smaller with Lion... – Yahia Aug 21 '11 at 19:48
  • @Yahia do you know where this is in the Lion documentation on Apple's site? – Rodney S. Foley Aug 21 '11 at 19:53
  • no - I do not... btw: this is also dependent on the used filesystem, namely HFS Plus (see http://developer.apple.com/library/mac/#technotes/tn/tn1150.html for the 255 limit per filename)... – Yahia Aug 21 '11 at 19:57
  • another hint seems to be wikipedia - http://en.wikipedia.org/wiki/Comparison_of_file_systems – Yahia Aug 21 '11 at 20:04
  • @Yahia which is why I answered the filesystem issue stating it was Lion's default with the exact one in a previous comment. – Rodney S. Foley Aug 21 '11 at 20:21
  • @Yahia you seem to have the doc's I was looking for since I didn't know Mac OS Extended (Journaled) = HFS Plus. Why don't you make the technotes link an answer as it has the answers to my question for both this on and the other. – Rodney S. Foley Aug 21 '11 at 20:24
  • as requested see my answer below – Yahia Aug 21 '11 at 20:29
  • Datapoint, stardate 3rd August 2022 (hello from the future): Snow Leopard, HFSPlus, maximum _path_ length 1024, maximum length of any _single component_ of the path, 255. I think that may apply universally, but more testing is needed. Also, beware symlinks. – Paul Sanders Aug 03 '22 at 13:56

4 Answers4

13

Old, but I found an answer:

#include <sys/syslimits.h>

and then it'll have a PATH_MAX constant as a #define. In my case,

char filenameBuffer [PATH_MAX];

You could hardcode 1024 as the maximum path, but using a constant like that makes your code scalable with new releases

Joe Plante
  • 6,308
  • 2
  • 30
  • 23
  • 3
    Also keep in mind `NAME_MAX` is the limit for individual file names as well. – CyberSkull Aug 26 '13 at 01:15
  • 1
    From experimentation, I'm finding that for a given file, using a path of length 1016 works and a path of length 1017 doesn't, which seems to contradict that constant as well as all documentation. – Hakanai Oct 01 '13 at 02:45
9

The limits depend on the used filesystem - OSX uses HFS Plus by default...

The only official documents I can point to are the HFS Plus spec which document the limit of 255 for filename length.

Wikipedia hints that the max path length on HFS Plus is "unlimited".

Perhaps contacting Apple Dev support is the most reliable way to get exact statements about limits.

Yahia
  • 69,653
  • 9
  • 115
  • 144
8

From actual testing on Mac OS X Yosemite, the max path length is 1016 characters. 1017 fails.

wisbucky
  • 33,218
  • 10
  • 150
  • 101
  • Tested on Sierra as well, max path length does not seem to be unlimited – Protongun Jun 09 '17 at 12:16
  • 4
    Note that /tmp, /etc and /var are symlinks to /private/tmp, /private/etc and /private/var. If you try to create files in those directories, you'll be able to create paths of size (1024 - len(/private)), which is 1016 – Harichandan Pulagam Jun 22 '17 at 19:58
  • @HarichandanPulagam That's a really good point. symlinks change the picture radically here. – Paul Sanders Aug 03 '22 at 13:25
5

Copy and paste this command into MacOSX's Terminal app (or iTerm2, xterm or the like)

bash$ cc -dM -E -xc - <<< '#include <sys/syslimits.h>' | grep -i ' [NP]A.._MAX'

Press the ⟨return⟩ or ⟨enter⟩ key to run it and get the result:

#define NAME_MAX 255
#define PATH_MAX 1024

These maximum name and path lengths are defined in the system header file sys/syslimits.h which cc (the C compiler) reads from some default location such as /usr/include/ or somewhere in the Xcode app. The cryptic switches are documented in man cc but essentially this example compiles a one line program and prints all the "macro" definitions into a pipe to grep which should filter out all but the lines we want to see. Follow man grep down the rabbit hole for details on pattern matching with regular expressions. Similarly,

bash$ cc -dM -E -xc - <<< ''

compiles an empty program and prints all the standard "macro" definitions peculiar to this particular system and compiler — definitely worth a glance under the hood.

Devon
  • 1,019
  • 9
  • 20
  • 2
    Whilst this code snippet is welcome, and may provide some help, it would be [greatly improved if it included an explanation](//meta.stackexchange.com/q/114762) of *how* and *why* this solves the problem. Remember that you are answering the question for readers in the future, not just the person asking now! Please [edit] your answer to add explanation, and give an indication of what limitations and assumptions apply. – Aniket G Mar 13 '19 at 02:54
  • bash$ `(cd /tmp && cc -xc - && (./a.out; echo \$? = $?)) <<< 'int main(){return sizeof (long);}'` prints the number of bytes in a **long** integer. – Devon Mar 13 '19 at 06:41